Pyhton 裁剪视频尺寸 脚本

Pyhton 裁剪视频尺寸 脚本

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : cuntou0906
# @File    : ReshapeVideo.py
# @Time    : 2022/8/12 16:42

import cv2 as cv

class Reshapevideo():
    def __init__(self,filename,Newfilename,Resize):
        self.filename = filename
        self.Newfilename = Newfilename
        self.Resize = Resize
        pass

    def Process(self):
        cap = cv.VideoCapture(filename)
        # 获取读入视频的参数
        fps = cap.get(cv.CAP_PROP_FPS)
        # width = cap.get(cv.CAP_PROP_FRAME_WIDTH)
        # height = cap.get(cv.CAP_PROP_FRAME_HEIGHT)

        intx0 = self.Resize[2]
        intx1 = self.Resize[3]

        inty0 = self.Resize[0]
        inty1 = self.Resize[1]
        wid = range(intx0, intx1)
        hei = range(inty0, inty1)
        size = (int(hei[-1] - hei[0]) + 1, int(wid[-1] - wid[0]) + 1)

        # 创建视频写入对象
        format = self.Newfilename.split('.')
        format = format[1]
        print(format)
        if format=='mp4':
         fourcc = cv.VideoWriter_fourcc(*'mp4v')
        elif format=='avi':
            fourcc = cv.VideoWriter_fourcc(*'XVID')

        out = cv.VideoWriter(self.Newfilename, fourcc, fps, size)

        # 读取视频帧,然后写入文件并在窗口显示
        success, frame_src = cap.read()
        while success and not cv.waitKey(1) == 27:  # 读完退出或者按下 esc 退出
            frame_target = frame_src[intx0:intx1, inty0:inty1]
            # print(frame_target.shape)

            # 写入视频文件
            out.write(frame_target)

            # 显示裁剪的视频和原视频
            cv.imshow("video", frame_target)
            cv.imshow("Video_src", frame_src)

            # 不断读取
            success, frame_src = cap.read()

        print("视频裁剪完成")
        # 销毁窗口,释放资源
        cv.destroyWindow("video")
        cv.destroyWindow("Video_src")
        cv.destroyAllWindows()
        cap.release()
        out.release()
        pass


if __name__ == "__main__":
    # 参数说明
    # filename 原始文件路径
    # Newfilename 新文件路径,支持mp4和avi
    # ReSize:裁剪尺寸(左,右,上,下) 单位像素点

    filename = r"VideoTest.mp4"
    Newfilename = r"NewVideoTest.mp4"

    ReSize = (375,1450,10,1080)
    ReshapevideoObj = Reshapevideo(filename,Newfilename,ReSize)
    ReshapevideoObj.Process()

使用

  设置如下参数即可:
# filename :原始文件路径
# Newfilename :新文件路径,支持mp4和avi
# ReSize :裁剪尺寸(左,右,上,下) 单位像素点

效果

  裁剪前:
在这里插入图片描述
  裁剪后:在这里插入图片描述

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

cuntou0906

玛莎拉蒂是我的目标!

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

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

打赏作者

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

抵扣说明:

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

余额充值