我正在开发一个代码来保存网络摄像头的视频,我需要使用 4 个不同的编解码器保存相同的视频,但我无法保存为 h.265,我收到以下错误:
[ERROR:[email protected]] global cap_ffmpeg_impl.hpp:3133 open Could not find encoder for codec_id=173, error: Encoder not found
[ERROR:[email protected]] global cap_ffmpeg_impl.hpp:3211 open VIDEOIO/FFMPEG: Failed to initialize VideoWriter", follow too the code that i use to create cv2.videowrite:
我只收到 h.265 的此错误,其他编解码器运行完美
out = []
out.append(cv2.VideoWriter("output_30_mp4v.mp4", cv2.VideoWriter_fourcc(*"mp4v"), 30.0, (width, height)))
out.append(cv2.VideoWriter("output_30_xvid.avi", cv2.VideoWriter_fourcc(*"xvid"), 30.0, (width, height)))
out.append(cv2.VideoWriter("output_30_mjpeg.avi", cv2.VideoWriter_fourcc(*"MJPG"), 30.0, (width, height)))
out.append(cv2.VideoWriter("output_30_h265.mp4", cv2.VideoWriter_fourcc(*"hev1"), 30.0, (width, height)))
有人知道我能做什么吗?
就这样做:
pip 安装 ffmpeg
这是一个示例代码,只需输入您的视频路径即可,您也可以下载 ffmeg 尝试您自己的代码。
代码: 导入CV2
input_video_path = 'C:/Users/abad_/Downloads/k.mp4'
output_video_path = 'C:/Users/abad_/Downloads/output_video.mp4'
enter code here
cap = cv2.VideoCapture(input_video_path)
如果不是 cap.isOpened(): print("错误:无法打开视频。") 退出()
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*'H264') 输出= cv2.VideoWriter(输出视频路径,fourcc,fps,(帧宽度,帧高度))
当 cap.isOpened() 时: ret, 框架 = cap.read() 如果不转: 休息 # 将帧写入输出文件 输出.write(帧)
cap.release() 输出.release() print("视频成功保存为H.264格式。")