简体   繁体   English

在Linux上使用FFmpeg从OpenCV 3编写x264

[英]Writing x264 from OpenCV 3 with FFmpeg on Linux

I'm having trouble writing h264 video with OpenCV 3 via FFmpeg ("'X','2','6','4'" FOURCC). 我在通过FFmpeg(“'X','2','6','4'”FOURCC)用OpenCV 3编写h264视频时遇到问题。 I've seen all the related posts so far on SO, but nothing helps. 到目前为止,我已经看过所有相关帖子,但没有任何帮助。 Code: 码:

cv::VideoWriter writer(output_path.string(),    CV_FOURCC('X','2','6','4'), 60, frame_size);

Output: 输出:

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/'!???' OpenCV:FFMPEG:标签0x34363258 /'X264'不支持编解码器ID 28和格式'mp4 / MP4(MPEG-4 Part 14)'OpenCV:FFMPEG:回退使用标签0x00000021 /'!???'

The resulting video is extremely small (byte-wise) and unreadable. 生成的视频非常小(按字节)并且不可读。 Setting the four_cc to -1 results in "unknown tag" from FFmpeg, I don't get any prompt to choose codec as others suggested. 将four_cc设置为-1会导致来自FFmpeg的“未知标记”,我没有像其他人建议的那样提示选择编解码器。

The OpenCV 3 doc states: 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. 具有MP4容器的FFMPEG后端本身使用其他值作为fourcc代码:请参阅ObjectType ,因此您可能会收到OpenCV关于fourcc代码转换的警告消息。

That page they refrence doesn't have h264 / x264 listed, and I'm not sure how to interpret that statement, since earlier SO posts seem to all list X.2.6.4 as the appropriate code. 他们引用的那个页面没有列出h264 / x264,我不确定如何解释该语句,因为早期的SO帖子似乎都将X.2.6.4列为适当的代码。 Using H.2.6.4 actually gives identical output. 使用H.2.6.4实际上提供相同的输出。

Any suggestions / workarounds? 有什么建议/解决方法吗?

PS the ffmpeg is most up-to-date from Ubuntu maintainers, it lists that it was configured with --enable-libx264 PS ffmpeg是最新的Ubuntu维护者,它列出它配置了--enable-libx264

EDIT: I tried to use the mkv container instead of mp4. 编辑:我试图使用mkv容器而不是mp4。 The warning about tag not being supported went away, but the resulting video is still unreadable. 关于标记不受支持的警告消失了,但生成的视频仍然无法读取。

I think your finding here is key: 我认为你的发现是关键:

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. 具有MP4容器的FFMPEG后端本身使用其他值作为fourcc代码:请参阅ObjectType,因此您可能会收到OpenCV关于fourcc代码转换的警告消息。

The mp4 tag values implemented for ffmpeg confirm this, and are in the ff_mp4_obj_type[] , in isom.c . 对于实施了MP4标签值ffmpeg确认这一点,并且在ff_mp4_obj_type[]isom.c The code in OpenCV's cap_ffmpeg_impl.hpp likely needs to be updated to support this. 可能需要更新OpenCV的cap_ffmpeg_impl.hpp的代码以支持此操作。 I poked around for an hour or two, realized it was non-trivial, and bailed. 我捅了一两个小时,意识到这是非平凡的,并且保释。

One work-around is to output to an .avi file. 一种解决方法是输出到.avi文件。 There are numerous examples of people having trouble with OpenCV and mp4, and being told to use .mov or .avi . 有许多人在使用OpenCV和mp4时遇到问题,并被告知要使用.mov.avi ( Here's one .) 这是一个 。)

@Greg Kramida: setting isColor = false did not help for me: the message remained, and my output file became only ~48 bytes. @Greg Kramida:设置isColor = false对我没有帮助:消息仍然存在,我的输出文件只变成了~48个字节。 According to the documentation it is a Windows flag -- whatever it is doing for me on Linux, it isn't good. 根据文档,它是一个Windows标志 - 无论它在Linux上为我做什么,它都不好。

Did you confirm that you could generate x264 with ffmpeg, by itself? 你确认你可以用ffmpeg生成x264吗?

After confirming that libx264 has value 0x21 when I invoke this on the command line: 在我在命令行上调用它时确认libx264的值为0x21:

ffmpeg -i x264-input.mp4 -vcodec libx264 -f mp4 x264-output.mp4

I decided to use 0x21 directly into VideoWriter.open() . 我决定直接在VideoWriter.open()使用0x21 That generates a valid and interpretable video file. 这会生成一个有效且可解释的视频文件。

For reference, my software is ffmpeg 3.0: 作为参考,我的软件是ffmpeg 3.0:

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

And OpenCV 3.1.0 configured with: 和OpenCV 3.1.0配置:

cmake \
    -D WITH_IPP=ON \
    -D INSTALL_CREATE_DISTRIB=ON \
    -D CMAKE_BUILD_TYPE=Release \
    -D CMAKE_INSTALL_PREFIX=/usr/local ..

x264 is the libx264-142:amd64 release for Debian Jessie. x264是Debian Jessie的libx264-142:amd64版本。

The problem had nothing to do with the displayed warning. 问题与显示的警告无关。 I was trying to write single-channel images, while the VideoWriter was expecting a 3-channel color image (default value of isColor, the 5-th argument to VideoWriter's constructor, is "true"). 我试图编写单通道图像,而VideoWriter期待一个3通道彩色图像(默认值为isColor,VideoWriter的构造函数的第5个参数,是“真”)。 The solution was setting isColor to false. 解决方案是将isColor设置为false。

The problem on OpenCV which message is: 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/'!???' OpenCV:FFMPEG:标签0x34363248 /'H264'不支持编解码器ID 28和格式'mp4 / MP4(MPEG-4 Part 14)'OpenCV:FFMPEG:回退使用标签0x00000021 /'!???'

It is produced by definitions of CV_FOURCC('H','2','6','4') this value do not corresponds with the value defined on isom.c 它由CV_FOURCC('H','2','6','4')的定义产生,该值与isom.c上定义的值不对应。

{ AV_CODEC_ID_H264 , 0x21 } {AV_CODEC_ID_H264,0x21}

Then defining fourcc as 0x21 cv::VideoWriter works smooth, 然后将fourcc定义为0x21 cv :: VideoWriter工作顺利,

cv::VideoWriter VF;
VF.open(filename,0x21,fps,frameSize,true);

as Jameson comments above, 正如詹姆森在上面的评论,

"I decided to use 0x21 directly into VideoWriter.open(). That generates a valid and interpretable video file." “我决定直接在VideoWriter.open()中使用0x21。这会生成一个有效且可解释的视频文件。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM