黑牛儿 2024-01-08 14:53 采纳率: 0%
浏览 197

opencv无法生成h264编码的视频文件

opencv+ffmpeg 再剪切视频时报错,

OpenCV: FFMPEG: tag 0x34363258/'X264' 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

软件版本:python2.7 + ffmpeg4.8.5 + opencv4.9.0
python代码

#!/usr/bin/python
#-*- coding: UTF-8 -*-

import cv2
# 打开原始视频文件
filepath = "/data/cut_video/header_v.mp4"
video = cv2.VideoCapture(filepath)

# 获取视频的基本信息(帧数、宽度、高度等)
fps = video.get(cv2.CAP_PROP_FPS) #帧速率
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
#总帧数
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) 
#视频总时长
duration = total_frames / fps
# 设置要保存的新视频名称及编写器参数
output_file = '/data/output_video.mp4'
fourcc = cv2.VideoWriter_fourcc(*'h264')
out = cv2.VideoWriter(output_file, fourcc, fps, (width, height), isColor=True)
 
# 定义需要剪切的起始时间点和结束时间点(单位为秒)
start_time = 4.73 # 从第10秒开始剪切
end_time = 64.18   # 到第30秒结束剪切
start_time = fps * float(start_time)
end_time = fps * float(end_time)
num = 0
while True:
    success, frame = video.read()
    if int(start_time) <= int(num) <= int(end_time):
        if success:
            out.write(frame)
        else:
            break
    num += 1
    if num > total_frames:
        break
# 关闭所有相关的对象
video.release()
out.release()
cv2.destroyAllWindows()

要如何才能解决这个问题

  • 写回答

2条回答 默认 最新

  • IT工程师_二师兄 2024-01-08 15:06
    关注

    生成264的文件是需要设置的你把你的代码发给我,我看一下你设置的对不对

    评论

报告相同问题?

问题事件

  • 创建了问题 1月8日