美文网首页每日爬虫
FFmpeg分段切割视频

FFmpeg分段切割视频

作者: 一只失去梦想的程序猿 | 来源:发表于2021-02-23 00:20 被阅读0次

    首先 获取视频总时长

    cmdLine='ffprobe temp.mp4 -show_entries format=duration -of compact=p=0:nk=1 -v 0'
    gettime = subprocess.check_output(cmdLine, shell=True)
    timeT = int(float(gettime.strip()))
    print(timeT)
    

    而视频切割 用到的命令

    cmdLine='ffmpeg -ss 0 -i temp.mp4 -c copy -t 60 cut.mp4 -y'
    returnCmd = subprocess.call(cmdLine, shell=True)
    print(returnCmd)
    

    获取视频总时长之后 按定好的时间段切割 直接循环就好
    完整代码

    import subprocess
    
    def getVideoTime(path):
        cmdline = 'ffprobe "%s" -show_entries format=duration -of compact=p=0:nk=1 -v 0'%path
        gettime=subprocess.check_output(cmdline, shell=True)
        timeT=int(float(gettime.strip()))
        return timeT
    
    videoPath='temp.mp4'
    cutTime=60
    timeT=getVideoTime(videoPath)
    firstTime=0
    index=1
    while firstTime<timeT:
        cmdLine = 'ffmpeg -ss %s -i %s -c copy -t %s %s.mp4 -loglevel quiet -y'%(firstTime,videoPath,cutTime,'cut_%s'%index)
        print(cmdLine)
        returnCmd = subprocess.call(cmdLine, shell=True)
        firstTime+=cutTime
        index+=1
    

    这里加了个-loglevel quiet 参数 去除了ffmpeg多余的输出信息
    于是 视频按照60秒一切割


    image.png

    相关文章

      网友评论

        本文标题:FFmpeg分段切割视频

        本文链接:https://www.haomeiwen.com/subject/khmlfltx.html