FFmpeg分段切割视频

首先 获取视频总时长

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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容