使用OpenCV输出摄像头视频要注意输出路径必需添加视频格式的后缀名,如output.mp4。
import cv2 capture = cv2.VideoCapture('test.mp4') #cv2.VideoCapture.get(CV_CAP_PROP_FORMAT) fps = capture.get(cv2.CAP_PROP_FPS) print 'fps is ' + str(fps) codec = int(capture.get(cv2.CAP_PROP_FOURCC)) print ('codec is %x'%(codec)) print ('codec is ' + chr(codec&0xFF) + chr((codec>>8)&0xFF) + chr((codec>>16)&0xFF) + chr((codec>>24)&0xFF))
cv2.CAP_PROP_FOURCC 用来得到原视频的编码格式。
如果是MPEG4编码,打印结果为 “codec is 7634706d”,十六进制对应的ASCII为 'v' '4' 'p' 'm'
如果是H264编码,打印的结果为 "codec is 31637661", 十六进制对应的ASCII为 '1' 'c' 'v' 'a'
查询Video Codecs by FOURCC(http://www.fourcc.org/codecs.php)
1. 搜索"mp4v" (和得到的16进制倒序排列),信息如下
Eval download (MP4V) info
Homepage: Eval downloadOwner: Media Excel
MPEG-4 Video. LEAD's MCMP codec also supports this format.
2. 搜索“avc1”(和得到的16进制倒序排列),信息如下
AVC1 info
Owner: AppleApple's version of the MPEG4 part 10/H.264 standard apparently.
出现OpenCV: FFMPEG: tag 0x00000021/'!???' is not found (format 'mp4 / MP4 (MPEG-4 Part 14)')'报错解决方法:
将fourcc = cv2.VideoWriter_fourcc(*"MJPG")的"MJPG"改成"mp4v"即可。
现在编码用到了CV_FOURCC获取编码格式:
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
CV_FOURCC('A', 'V', 'C', '1') = H264 codec