在OSX我可以从我的摄像头记录和写的视频文件有以下简单的脚本:如何用H264编解码器编写mp4视频文件?
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
,将4cc编解码器改为H264
。写入视频文件,它的工作原理,但给了我以下错误:
$ 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/'!???'
因为我以为我失踪了H264编解码器的ffmpeg,我决定卸载的ffmpeg和OpenCV,并与H264支持重新安装它们。为此,我使用以下命令:
# 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
此我再次运行脚本,使用下列组合后:
output.mp4
与H264
output.mp4
与X264
不幸我仍然得到OpenCV警告/错误。该文件是可读的,但它仍然让我感到我得到这些错误。有谁知道我怎样才能让OpenCV用H264编解码器编写mp4视频文件?
欢迎所有提示!
您尝试重新启动吗? –
@StavrosAvramidis - 不,重新启动没有帮助 – kramer65
确保你有ffdshow(x32和x64) –