解决:OpenCV: FFMPEG: tag 0x44495658/‘XVID‘ is not supported with codec id 12 and format ‘mp4 / MP4

解决:OpenCV: FFMPEG: tag 0x44495658/‘XVID’ is not supported with codec id 12 and format 'mp4 / MP4





背景

在使用之前的代码利用 python 的 opencv 包把图片合并为视频(mp4格式)的时候,报错:
OpenCV: FFMPEG: tag 0x44495658/‘XVID’ is not supported with codec id 12 and format ‘mp4 / MP4 (MPEG-4 Part 14)’



报错问题

OpenCV: FFMPEG: tag 0x44495658/'XVID' is not supported with codec id 12 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x7634706d/'mp4v'


报错翻译

主要报错信息内容翻译如下所示:

OpenCV: FFMPEG: tag 0x44495658/'XVID' is not supported with codec id 12 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x7634706d/'mp4v'

翻译:

OpenCV:FFMPEG:编解码器id 12和格式“mp4/mp4(MPEG-4第14部分)”不支持标记0x44495658/“XVID”
OpenCV:FFMPEG:回退以使用标记0x7634706d/'mp4v'


代码如下

import cv2

......
fourcc = cv2.VideoWriter_fourcc('M','J','P','G') # MJPG
img_size = (image_w, image_h)
videoWriter = cv2.VideoWriter(save_video_path, fourcc, fps, img_size, isColor=True)
 
for  image_path in xxx:
    image = cv2.imread(image_path)
    videoWriter.write(image)
videoWriter.release()
......

其中,VideoWriter()原为:

VideoWriter(filename, fourcc, fps, frameSize[, isColor]) -> <VideoWriter object>

各参数说明如下:

  1. filename 是要保存的文件的路径
  2. fourcc 指定编码器
  3. fps 要保存的视频的帧率
  4. frameSize 要保存的文件的画面尺寸
  5. isColor 指示是黑白画面还是彩色的画面

fourcc

fourcc 本身是一个 32 位的无符号数值,用 4 个字母表示采用的编码器。一般依据你的电脑环境安装了哪些编码器。 常用的有 “DIVX"、”MJPG"、“XVID”、“X264"。可用的列表在这里,点击Video Codecs by FOURCC



报错原因

经过查阅资料,发现主要是合成时采用的视频编码不对。

小伙伴们按下面的解决方法即可解决!!!



解决方法

合成时使用正确的视频编码。

修改前:

fourcc = cv2.VideoWriter_fourcc('M','J','P','G')

修改后:

fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V') 

代码总体结构大致如下:

import cv2

......
fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V') # mp4
img_size = (image_w, image_h)
videoWriter = cv2.VideoWriter(save_video_path, fourcc, fps, img_size, isColor=True)
 
for  image_path in xxx:
    image = cv2.imread(image_path)
    videoWriter.write(image)
videoWriter.release()
......



今天的分享就到此结束了

欢迎点赞评论关注三连

在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ninghes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值