如何使用H264编解码器写入mp4视频文件? [英] How to write mp4 video file with H264 codec?
问题描述
在OSX上,我可以使用以下简单脚本从网络摄像头录制视频并编写视频文件:
On OSX I can record from my webcam and write a video file with the following simple script:
import cv2
camera = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object to save the video
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video_writer = cv2.VideoWriter('output.avi', fourcc, 25.0, (640, 480))
while True:
try:
(grabbed, frame) = camera.read() # grab the current frame
frame = cv2.resize(frame, (640, 480)) # resize the frame
video_writer.write(frame) # Write the video to the file system
except KeyboardInterrupt:
camera.release()
break
虽然生成的avi文件很大.我想要一个较小的文件,最好是mp4.因此,我将文件名更改为output.mp4
,并将fourcc编解码器更改为H264
.那会写一个有效的视频文件,但会给我以下错误:
The resulting avi file is quite big though. I want a smaller file, preferably an mp4. So I changed the filename to output.mp4
and the fourcc codec to H264
. That writes a video file which works, but gives me the following error:
$ python write_video_file.py
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/'!???'
由于我以为我在ffmpeg中缺少H264编解码器,因此我决定卸载ffmpeg和opencv,然后在支持H264的情况下重新安装它们.为此,我使用了以下命令:
Since I thought I'm missing the H264 codec in ffmpeg I decided to uninstall ffmpeg and opencv and reinstall them again with H264 support. For this I used the following commands:
# First ffmpeg
brew install ffmpeg --with-fdk-aac --with-libvidstab --with-openh264 \
--with-openjpeg --with-openssl --with-tools --with-webp --with-x265 --with-zeromq
# then opencv3
brew tap homebrew/science
brew install opencv3 --with-contrib --with-ffmpeg --with-tbb
此后,我使用以下组合再次运行了脚本:
After this I ran the script again, using the following combinations:
-
output.mp4
与H264
-
output.mp4
与X264
output.mp4
withH264
output.mp4
withX264
不幸的是,我仍然收到OpenCV警告/错误.该文件可读,但仍然让我感到烦恼,我得到了这些错误.有人知道我如何使OpenCV使用H264编解码器写入mp4视频文件吗?
Unfortunately I still get the OpenCV warnings/errors. The file is readable, but it still annoys me that I get these errors. Does anybody have any idea how I can make OpenCV write mp4 video file with the H264 codec?
欢迎所有提示!
推荐答案
我花了很长时间尝试在 macOS 上查找视频编解码器列表,并找到哪些编解码器可与哪些容器一起使用,然后 QuickTime 实际上可以读取生成的文件.
I spent ages trying to find a list of video codecs on macOS, and finding which codecs work with which containers, and then if QuickTime can actually read the resulting files.
我可以将我的发现总结如下:
I can summarise my findings as follows:
.mov container and fourcc('m','p','4','v') work and QuickTime can read it
.mov container and fourcc('a','v','c','1') work and QuickTime can read it
.avi container and fourcc('F','M','P','4') works but QuickTime cannot read it without conversion
我确实按照您的意愿在mp4
容器中编写了h264
视频,只是没有使用OpenCV的 VideoWriter 模块.取而代之的是,我更改了代码(我的碰巧是C ++),仅输出原始的bgr24
格式数据-这就是OpenCV仍然喜欢存储像素的方式:
I did manage to write h264
video in an mp4
container, as you wanted, just not using OpenCV's VideoWriter module. Instead I changed the code (mine happens to be C++) to just output raw bgr24
format data - which is how OpenCV likes to store pixels anyway:
因此,存储在名为Frame
的Mat
中的视频帧的输出变为:
So the output of a frame of video stored in a Mat
called Frame
becomes:
cout.write(reinterpret_cast<const char *>(Frame.data),width*height*3);
,然后将程序的输出通过管道输送到ffmpeg
中,如下所示:
and then you pipe the output of your program into ffmpeg
like this:
./yourProgram | ffmpeg -y -f rawvideo -pixel_format bgr24 -video_size 1024x768 -i - -c:v h264 -pix_fmt yuv420p video.mp4
是的,我知道我已经做了一些假设:
Yes, I know I have made some assumptions:
- 数据是CV8_UC3,
- 在 OpenCV 和 ffmpeg 和 中尺寸匹配
- OpenCV 数据中没有填充或大步前进,
- that the data are CV8_UC3,
- that the sizes match in OpenCV and ffmpeg, and
- that there is no padding or mad stride in the OpenCV data,
但是基本技术有效,您可以将其适应其他尺寸和情况.
but the basic technique works and you can adapt it for other sizes and situations.
这篇关于如何使用H264编解码器写入mp4视频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!