使用 H264 编解码器将视频保存在 opencv 中
Save video in opencv with H264 codec
我正在使用 opencv-python==4.5.1.48
和 python3.9
docker。我想以 h264 格式保存视频。这是我保存视频的功能:
import cv2
def save_video(frames):
fps = 30
video_path = '/home/save_test.mp4'
fourcc = cv2.VideoWriter_fourcc(*'h264')
video_writer = cv2.VideoWriter(video_path, fourcc, fps, (112, 112))
for frame in frames:
video_writer.write(frame)
video_writer.release()
当我使用.mp4 格式保存视频时,出现以下错误:
OpenCV: FFMPEG: tag 0x34363268/'h264' is not supported with codec id
27 and format 'mp4 / MP4 (MPEG-4 Part 14)' OpenCV: FFMPEG: fallback to
use tag 0x31637661/'avc1' Could not find encoder for codec id 27:
Encoder not found
我搜索并阅读了一些解决方案,但 none 解决了我的问题。
更新:
我也安装了 post 中推荐的 libx264-dev
,但没有用。
终于,我找到了解决办法。我可以在 ubuntu:20.04
docker 中解决我的问题。您应该注意的重要一点是您应该通过 apt-get install python3-opencv
安装 OpenCV 而不是使用 pip
.
我正在使用 opencv-python==4.5.1.48
和 python3.9
docker。我想以 h264 格式保存视频。这是我保存视频的功能:
import cv2
def save_video(frames):
fps = 30
video_path = '/home/save_test.mp4'
fourcc = cv2.VideoWriter_fourcc(*'h264')
video_writer = cv2.VideoWriter(video_path, fourcc, fps, (112, 112))
for frame in frames:
video_writer.write(frame)
video_writer.release()
当我使用.mp4 格式保存视频时,出现以下错误:
OpenCV: FFMPEG: tag 0x34363268/'h264' is not supported with codec id 27 and format 'mp4 / MP4 (MPEG-4 Part 14)' OpenCV: FFMPEG: fallback to use tag 0x31637661/'avc1' Could not find encoder for codec id 27: Encoder not found
我搜索并阅读了一些解决方案,但 none 解决了我的问题。
更新:
我也安装了 post 中推荐的 libx264-dev
,但没有用。
终于,我找到了解决办法。我可以在 ubuntu:20.04
docker 中解决我的问题。您应该注意的重要一点是您应该通过 apt-get install python3-opencv
安装 OpenCV 而不是使用 pip
.