我认为你的发现是关键:
使用MP4容器的FFMPEG后端本身使用其他值作为fourcc代码:请参阅ObjectType,因此您可能会收到OpenCV关于fourcc代码转换的警告消息 .
为 ffmpeg 实现的mp4标记值确认了这一点,并且在isom.c中的 ff_mp4_obj_type[] 中 . 可能需要更新OpenCV的cap_ffmpeg_impl.hpp中的代码以支持这一点 . 我捅了一两个小时,意识到这是非平凡的,并且保释 .
一种解决方法是输出到 .avi 文件 . 有许多人在使用OpenCV和mp4时遇到问题,被告知要使用 .mov 或 .avi . (Here's one . )
@Greg Kramida:设置 isColor = false 对我没有帮助:消息仍然存在,我的输出文件只变为~48个字节 . 根据文档,它是一个Windows标志 - 无论它在Linux上为我做什么,它都不好 .
你确认你可以用ffmpeg生成x264吗?
在我在命令行上调用它时确认libx264的值为0x21:
ffmpeg -i x264-input.mp4 -vcodec libx264 -f mp4 x264-output.mp4
我决定直接将 0x21 用于VideoWriter.open() . 这会生成一个有效且可解释的视频文件 .
作为参考,我的软件是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
和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是Debian Jessie的 libx264-142:amd64 版本 .