赞
踩
在进行图片转视频文件时遇到的主要报错类型有一下三种情况:
case1:
OpenCV: FFMPEG: tag 0x5634504d/‘MP4V’ is not supported with codec id 13 and format ‘mp4 / MP4 (MPEG-4 Part 14)’
OpenCV: FFMPEG: fallback to use tag 0x00000020/’ ???
case2:
OpenCV: FFMPEG: tag 0x5634504d/‘MP4V’ is not supported with codec id 13 and format ‘avi / AVI (Audio Video Interleaved)’
OpenCV: FFMPEG: fallback to use tag 0x34504d46/‘FMP4’
case3:
OpenCV: FFMPEG: tag 0x47504a4d/‘MJPG’ is not supported with codec id 8 and format ‘mp4 / MP4 (MPEG-4 Part 14)’
OpenCV: FFMPEG: fallback to use tag 0x0000006c/‘l???’
导致上面报错类型的原因是合成视频时,编码类型错误
#cv2.VideoWriter_fourcc('X', '2', '6', '4'), 该参数是较新的MPEG-4编码,产生的文件较小,文件扩展名应为.mp4 #cv2.VideoWriter_fourcc('P', 'I', 'M', 'I'), 该参数是较旧的MPEG-1编码,文件名后缀为.avi #cv2.VideoWriter_fourcc('M', 'P', '4', '2'), 该参数是MPEG-2编码,产生的文件不会特别大,文件名后缀为.avi #cv2.VideoWriter_fourcc('D', 'I', 'V', '3'), 该参数是MPEG-3编码,产生的文件不会特别大,文件名后缀为.avi #cv2.VideoWriter_fourcc('D', 'I', 'V', 'X'), 该参数是MPEG-4编码,产生的文件不会特别大,文件名后缀为.avi #cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'), 该参数是较旧的MPEG-4编码,产生的文件不会特别大,文件名后缀为.avi #cv2.VideoWriter_fourcc('M', 'P', '4', 'V'), 该参数也是较旧的MPEG-4编码,产生的文件不会特别大,文件扩展名应为.m4v #cv2.VideoWriter_fourcc('T', 'H', 'E', 'O'), 该参数是Ogg Vorbis,产生的文件相对较大,文件名后缀为.ogv #cv2.VideoWriter_fourcc('F', 'L', 'V', '1'), 该参数是Flash视频,产生的文件相对较大,文件名后缀为.flv #cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 该参数是motion-jpeg编码,产生的文件较大,文件名后缀为.avi #cv2.VideoWriter_fourcc('I', '4', '2', '0'),该参数是未压缩的YUV编码,4:2:0色度子采样,这种编码广泛兼容,但会产生特别大的文件,文件扩展名应为.avi ```
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
这里测试了原始视频大小为48M,转换后视频大小如下图所示:
情况一:
import cv2 video_dir = "./videos/test.mp4" save_dir = "./test_videos/" video = cv2.VideoCapture(video_dir) images_list = [] while video.isOpened(): ret, frame = video.read() if frame is None: break ... ... ... images_list.append(frame) cap = cv2.VideoCapture(video_dir) fps = cap.get(cv2.CAP_PROP_FPS) # 帧率 frame_counter = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) # 总帧数 fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V') //或者改为 //fourcc = cv2.VideoWriter_fourcc('D', 'I', 'V', 'X') save_video_dir = save_dir + video_dir.split('/')[-1] img_size = (1280,720) videoWriter = cv2.VideoWriter(save_video_dir, fourcc, fps, img_size, isColor=True) for i in range(1, frame_counter): videoWriter.write(images_list[i]) videoWriter.release()
视频保存格式为.mp4
会报如下错误,在所有图片没有完全保存为视频文件时,视频文件不可以正常播放,所有图片完全保存为视频文件后可以正常播放
OpenCV: FFMPEG: tag 0x5634504d/‘MP4V’ is not supported with codec id 13 and format ‘mp4 / MP4 (MPEG-4 Part 14)’
OpenCV: FFMPEG: fallback to use tag 0x00000020/’ ???
情况二:
如果报上面代码改为:
import cv2 video_dir = "./videos/test.mp4" save_dir = "./test_videos/" video = cv2.VideoCapture(video_dir) images_list = [] while video.isOpened(): ret, frame = video.read() if frame is None: break ... ... ... images_list.append(frame) cap = cv2.VideoCapture(video_dir) fps = cap.get(cv2.CAP_PROP_FPS) # 帧率 frame_counter = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) # 总帧数 fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V') save_video_dir = save_dir + video_dir.split('/')[-1].split(".")[0] + '.avi img_size = (1280,720) videoWriter = cv2.VideoWriter(save_video_dir, fourcc, fps, img_size, isColor=True) for i in range(1, frame_counter): videoWriter.write(images_list[i]) videoWriter.release()
视频保存格式为.avi
会报如下错误,但是在所有图片没有完全保存为视频文件时,就可以正常播放,所有图片完全保存为视频文件后也可以正常播放OpenCV: FFMPEG: tag 0x5634504d/‘MP4V’ is not supported with codec id 13 and format ‘avi / AVI (Audio Video Interleaved)’
OpenCV: FFMPEG: fallback to use tag 0x34504d46/‘FMP4’
情况三:
如果报上面代码改为:
import cv2 video_dir = "./videos/test.mp4" save_dir = "./test_videos/" video = cv2.VideoCapture(video_dir) images_list = [] while video.isOpened(): ret, frame = video.read() if frame is None: break ... ... ... images_list.append(frame) cap = cv2.VideoCapture(video_dir) fps = cap.get(cv2.CAP_PROP_FPS) # 帧率 frame_counter = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) # 总帧数 fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') save_video_dir = save_dir + video_dir.split('/')[-1] img_size = (1280,720) videoWriter = cv2.VideoWriter(save_video_dir, fourcc, fps, img_size, isColor=True) for i in range(1, frame_counter): videoWriter.write(images_list[i]) videoWriter.release()
视频保存格式为.mp4
会报如下错误,在所有图片没有完全保存为视频文件时,视频文件不可以正常播放,所有图片完全保存为视频文件后可以正常播放
OpenCV: FFMPEG: tag 0x47504a4d/‘MJPG’ is not supported with codec id 8 and format ‘mp4 / MP4 (MPEG-4 Part 14)’
OpenCV: FFMPEG: fallback to use tag 0x0000006c/‘l???’
情况四:
如果报上面代码改为:
import cv2 video_dir = "./videos/test.mp4" save_dir = "./test_videos/" video = cv2.VideoCapture(video_dir) images_list = [] while video.isOpened(): ret, frame = video.read() if frame is None: break ... ... ... images_list.append(frame) cap = cv2.VideoCapture(video_dir) fps = cap.get(cv2.CAP_PROP_FPS) # 帧率 frame_counter = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) # 总帧数 fourcc = 0x00000021 save_video_dir = save_dir + video_dir.split('/')[-1] img_size = (1280,720) videoWriter = cv2.VideoWriter(save_video_dir, fourcc, fps, img_size, isColor=True) for i in range(1, frame_counter): videoWriter.write(images_list[i]) videoWriter.release()
视频保存格式为.mp4
不会报错,在所有图片没有完全保存为视频文件时,视频文件不可以正常播放,所有图片完全保存为视频文件后可以正常播放
情况五:
如果报上面代码改为:
import cv2 video_dir = "./videos/test.mp4" save_dir = "./test_videos/" video = cv2.VideoCapture(video_dir) images_list = [] while video.isOpened(): ret, frame = video.read() if frame is None: break ... ... ... images_list.append(frame) cap = cv2.VideoCapture(video_dir) fps = cap.get(cv2.CAP_PROP_FPS) # 帧率 frame_counter = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) # 总帧数 fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') //或者改为 //fourcc = cv2.VideoWriter_fourcc('D', 'I', 'V', 'X') save_video_dir = save_dir + video_dir.split('/')[-1].split(".")[0] + '.avi img_size = (1280,720) videoWriter = cv2.VideoWriter(save_video_dir, fourcc, fps, img_size, isColor=True) for i in range(1, frame_counter): videoWriter.write(images_list[i]) videoWriter.release()
视频保存格式为.avi
代码不会报错,在所有图片没有完全保存为视频文件时,就可以正常播放,所有图片完全保存为视频文件后也可以正常播放
# import cv2, os
# image_dir = "./test_images/"
# save_dir = "./test_videos/"
# file_list = os.listdir(image_dir)
# fps = 25
# fourcc = cv2.VideoWriter_fourcc('X', '2', '6', '4')
# save_video_dir = save_dir + "video"+ '.avi'
# img_size = (1280,720)
# videoWriter = cv2.VideoWriter(save_video_dir, fourcc, fps, img_size,isColor=True)
#
# for img in file_list:
# videoWriter.write(cv2.imread(os.path.join(image_dir, img)))
#
# videoWriter.release()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。