python截取固定长度视频

class FFmpeg:

    def __init__(self, editvdo, addlogo=None, addmusic=None,
                 addvdohead=None, addvdotail=None):
        self.editvdo = editvdo
        self.addlogo = addlogo
        self.addmusic = addmusic
        self.addvdohead = addvdohead
        self.addvdotail = addvdotail
        # self.vdo_time, self.vdo_width, self.vdo_height, self.attr_dict = self.get_attr()
        self.editvdo_path = os.path.dirname(editvdo)
        self.editvdo_name = os.path.basename(editvdo)

    def get_attr(self):
        """
        获取视频属性参数
        :return:
        """
        strcmd = r'ffprobe -print_format json -show_streams -i "{}"'.format(self.editvdo)
        status, output = subprocess.getstatusoutput(strcmd)
        agrs = eval(re.search('{.*}', output, re.S).group().replace("\n", "").replace(" ", ''))
        streams = agrs.get('streams', [])
        agrs_dict = dict()
        [agrs_dict.update(x) for x in streams]
        vdo_time = agrs_dict.get('duration')
        vdo_width = agrs_dict.get('width')
        vdo_height = agrs_dict.get('height')
        attr = (vdo_time, vdo_width, vdo_height, agrs_dict)
        return attr
    def edit_head(self, start_time, end_time, deposit=None):
        """
        截取指定长度视频
        :param second: 去除开始的多少秒
        :param deposit: 另存为文件
        :return: True/Flase
        """
        if None == deposit:
            deposit = self.editvdo_path+'/'+'edit_head'+self.editvdo_name
        if not os.path.exists(deposit):
            os.mkdir(deposit)
        if deposit:
            deposit += self.editvdo_name
        start = time.strftime('%H:%M:%S', time.gmtime(start_time))
        end = time.strftime('%H:%M:%S', time.gmtime(end_time))
        strcmd = 'ffmpeg -i ' + self.editvdo + ' -ss ' + str(start) + ' -to ' + str(end) + ' ' + deposit
        result = subprocess.run(args=strcmd, stdout=subprocess.PIPE, shell=True)
        
def getFilesList(path):
    fileList = set()
    res = []
    for root, dirs, files in os.walk(path):
        for filespath in files:
            res.append(os.path.join(root, filespath))
    return res

def getMovFiles(root_dir):
    cur_path = root_dir
    ret = getFilesList(cur_path)
    listSpicalpath = set()
    for i in ret:
        # change to any types by changing ".MOV" to other type :-)
        if os.path.splitext(i)[1] == ".mov":
            listSpicalpath.add(os.path.splitext(i)[0])
    return listSpicalpath

if __name__=="__main__":

    import os
    import subprocess
    import datetime
    import json, pprint
    import re, time
    import threading
    import random
    import shutil

    new_dir = ""
    # Get the root dir
    root_dir = ""
    mov_list = getMovFiles(root_dir)
    for mov in mov_list:
        test = FFmpeg(mov+'.mov')
        if test.get_attr()[0]:
            mov_time = float(test.get_attr()[0])
            start_time = mov_time - 1
            test.edit_head(start_time, mov_time, deposit=new_dir)
        else:
            continue

参考:

1、https://github.com/HeZhang1994/video-audio-tools/blob/master/VideoAudio_Editing/run_Audio_01Clip.py

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值