解决openCV生成的视频无法播放问题 + 减短视频时长功能

OpenCV生成的视频,遇到了无法播放的问题,百思不得解;

show the code:

def cut_video(src_path,dst_path,name,inv):
    vid = cv2.VideoCapture(os.path.join(src_path,name))
    fps = vid.get(cv2.CAP_PROP_FPS)
    width = vid.get(cv2.CAP_PROP_FRAME_WIDTH)
    height = vid.get(cv2.CAP_PROP_FRAME_HEIGHT)
    print('fps is ', fps,width,height)
    if not os.path.exists(dst_path):
        os.makedirs(dst_path,exist_ok=True)
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    video_writer = cv2.VideoWriter(filename=os.path.join(dst_path, name), fourcc=fourcc, fps=fps,
                                    frameSize = (int(height), int(width)))
    num = 0
    while vid.isOpened() and num < int(fps * inv):
        is_read, frame = vid.read()
        if is_read:
            video_writer.write(frame)
            num += 1
        else:
            break
    video_writer.release()
    print('process ok!')
    return

好像没查到有哪里错误,直到 frameSize的顺序;

图像的维度是这样的:height ,width ,channel = img.shape;

但是frameSize的顺序是相反的:width,height;

#src_path 对应原始视频的路径;
#dst_path 对应生成的视频路径,name为视频的文件名;
#inv == 改变之后的视频时长;

def cut_video(src_path,dst_path,name,inv):
    vid = cv2.VideoCapture(os.path.join(src_path,name))
    fps = vid.get(cv2.CAP_PROP_FPS)
    width = vid.get(cv2.CAP_PROP_FRAME_WIDTH)
    height = vid.get(cv2.CAP_PROP_FRAME_HEIGHT)
    print('fps is ', fps,width,height)
    if not os.path.exists(dst_path):
        os.makedirs(dst_path,exist_ok=True)
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    video_writer = cv2.VideoWriter(filename=os.path.join(dst_path, name), fourcc=fourcc, fps=fps,
    #Warning-----------------注意顺序是,width,height,一定要对应原始图像的大小
                                   frameSize=(int(width), int(height)))
    ###############################
                                    # frameSize = (int(height), int(width)))
    num = 0
    while vid.isOpened() and num < int(fps * inv):
        is_read, frame = vid.read()
        if is_read:
            video_writer.write(frame)
            num += 1
        else:
            break
    video_writer.release()
    print('process ok!')
    return

上面函数实现减短视频时长的功能,有问题欢迎随时交流!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值