Writing x264 from OpenCV 3 with FFmpeg on Linux
我在通过FFmpeg("'X','2','6','4'" FOURCC)用OpenCV 3编写h264视频时遇到问题。到目前为止,我已经看过所有与SO相关的文章,但是没有任何帮助。码:
1 | cv::VideoWriter writer(output_path.string(), CV_FOURCC('X','2','6','4'), 60, frame_size); |
输出:
OpenCV: FFMPEG: tag 0x34363258/'X264' is not supported with codec id
28 and format 'mp4 / MP4 (MPEG-4 Part 14)' OpenCV: FFMPEG: fallback to
use tag 0x00000021/'!???'
产生的视频非常小(按字节),并且不可读。将four_cc设置为-1会导致FFmpeg产生"未知标签",我没有得到任何提示来选择编解码器,就像其他人建议的那样。
OpenCV 3文档指出:
FFMPEG backend with MP4 container natively uses other values as fourcc
code: see ObjectType, so you may receive a warning message from OpenCV
about fourcc code conversion.
他们所引用的页面没有列出h264 / x264,并且我不确定如何解释该语句,因为早期的SO帖子似乎都将X.2.6.4列为适当的代码。使用H.2.6.4实际上会提供相同的输出。
有任何建议/解决方法吗?
附言ffmpeg是Ubuntu维护者最新的信息,它列出了它是用
编辑:我试图使用mkv容器而不是mp4。关于不支持标签的警告消失了,但是仍无法读取生成的视频。
我认为您的发现很关键:
FFMPEG backend with MP4 container natively uses other values as fourcc code: see ObjectType, so you may receive a warning message from OpenCV about fourcc code conversion.
为
一种解决方法是输出到
@Greg Kramida:设置
您是否确定可以单独使用ffmpeg生成x264?
当我在命令行上调用libx264确认值为0x21后:
1 | ffmpeg -i x264-input.mp4 -vcodec libx264 -f mp4 x264-output.mp4 |
我决定直接在
供参考,我的软件是ffmpeg 3.0:
1 2 3 4 5 6 7 8 9 10 11 12 | ffmpeg -version ffmpeg version 3.0 Copyright (c) 2000-2016 the FFmpeg developers built with gcc 4.9.2 (Debian 4.9.2-10) configuration: --enable-libx264 --enable-gpl --prefix=/usr/local --enable-shared --cc=`gcc -fPIC` --enable-libfdk-aac --enable-libx265 --enable-nonfree --enable-libmp3lame libavutil 55. 17.103 / 55. 17.103 libavcodec 57. 24.102 / 57. 24.102 libavformat 57. 25.100 / 57. 25.100 libavdevice 57. 0.101 / 57. 0.101 libavfilter 6. 31.100 / 6. 31.100 libswscale 4. 0.100 / 4. 0.100 libswresample 2. 0.101 / 2. 0.101 libpostproc 54. 0.100 / 54. 0.100 |
并且OpenCV 3.1.0配置为:
1 2 3 4 5 | cmake \ -D WITH_IPP=ON \ -D INSTALL_CREATE_DISTRIB=ON \ -D CMAKE_BUILD_TYPE=Release \ -D CMAKE_INSTALL_PREFIX=/usr/local .. |
x264是Debian Jessie的
该问题与显示的警告无关。我正在尝试编写单通道图像,而VideoWriter则期望使用3通道彩色图像(VideoWriter的构造函数的第5个参数isColor的默认值为" true")。解决方案是将isColor设置为false。
OpenCV上的问题是:
OpenCV: FFMPEG: tag 0x34363248/'H264' is not supported with codec id 28 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x00000021/'!???'
它由CV_FOURCC('H','2','6','4')的定义产生,该值与isom.c上定义的值不对应
{ AV_CODEC_ID_H264 , 0x21 }
然后将fourcc定义为0x21 cv :: VideoWriter可以顺利进行,
1 2 | cv::VideoWriter VF; VF.open(filename,0x21,fps,frameSize,true); |
正如詹姆森(Jackson)所说,
"I decided to use 0x21 directly into VideoWriter.open(). That generates a valid and interpretable video file."