python截取固定长度视频

python截取固定长度视频,第1张

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/733854.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存