1、视频分帧
最近对视频人脸进行提取landmark,所以先把视频进行分帧图片处理
import cv2
def video2frame(video_path,frame_save_path,time_interval):
"""
:param video_path: 视频存放地址
:param frame_save_path: 生成图片存放地址
:param time_interval: 帧数的间隔,也就是说隔几帧去一次
:return:
"""
vidcap = cv2.VideoCapture(video_path)
success, image = vidcap.read() #读取视频
count = 0
while success:
success, image = vidcap.read()
count += 1
if count % time_interval == 0:
cv2.imwrite(frame_save_path + '/frame%d.png' % count, image)
print(count)
if __name__=="__main__":
video_path = r'F:\data\test\02-01-06-02-01-01-01.mp4'
save_path = r'F:/data/test/data'
video2frame(video_path, save_path, 1)
2、多张图片视频合成
最近做视频,因为生成的都是图片,所以把图片合成起来看生成的结果
import os
import cv2
def generate_video(path,size)
fps = 24 #帧率
#size = (640, 480)
videowriter = cv2.VideoWriter("F:/data/test.mp4",cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, size)
#path = r'F:/data/predict_landmark/'
for i in os.listdir(path):
img = cv2.imread(path + i)
videowriter.write(img)
videowriter.release()
generate_video('F:/data/predict_landmark/',(640, 480))
ps.文件路径和图片名都不要出现中文,否则视频可能合成错误。
视频编码参考:
CV_FOURCC('P','I','M','1') = MPEG-1 codec
CV_FOURCC('M','J','P','G') = motion-jpeg codec
CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec
CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec
CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec
CV_FOURCC('U', '2', '6', '3') = H263 codec
CV_FOURCC('I', '2', '6', '3') = H263I codec
CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec
注: 视频播放有问题的话可以从这里边选择不同的编码方式。