首先 獲取視頻總時長
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)
獲取視頻總時長之后 按定好的時間段切割 直接循環(huán)就好
完整代碼
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 參數(shù) 去除了ffmpeg多余的輸出信息
于是 視頻按照60秒一切割