日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python调用ffmpeg合并_用ffmpeg命令处理mp4剪切与合并

發布時間:2023/12/1 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python调用ffmpeg合并_用ffmpeg命令处理mp4剪切与合并 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 剪切:

./ffmpeg -ss 00:00:06 -t 00:00:12 -i input.mp4 -vcodec copy -acodec copy output.mp4

意思是從截取從6秒開始,時長為12秒的視頻,格式不變,

輸入為input.mp4

輸出為output.mp4

-vcodec copy -acodec copy : 編碼格式不變

2. 合并:

mp4的合并最好是先轉成無損質量的ts,再合并(如果直接合并,會有些小bug)

./ffmpeg -i 1.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 1.ts

./ffmpeg -i 2.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 2.ts

./ffmpeg -i "concat:1.ts|2.ts" -acodec copy -vcodec copy -absf aac_adtstoasc output.mp4

還有就是關于-s選項的解釋:

‘-ss position (input/output)’

When used as an input option (before -i), seeks in this input file to position. When used as an output option (before an output filename), decodes but discards input until the timestamps reach position. This is slower, but more accurate.

position may be either in seconds or in hh:mm:ss[.xxx] form.

意思就是如果要把-ss作為輸入選項的話要放在-i之前,當做輸出選項的話放在輸出文件之前。我們這是要截取一段視頻,應該當做輸入選項,所以-ss要在-i之前才有效,不然會花費很長一段時間來尋找-ss。

3. 批處理(python腳本實現)

主要是實現用自動化工具,將視頻切割成比如2分鐘的很多小視頻文件;

事情的由來是我看到一個youtube的視頻不錯,因為墻的原因不能分享,但是原文件比較大(有200多M)不能發微信(限定是20M以內),所以造了這個輪子;

1.png

# coding=gb2312

import string

import os

import time

import re

import math

import sys

from optparse import OptionParser

print "Test by gongjia start..."

parser = OptionParser()

parser.add_option("-i", "--input", dest="input",action="store_true",help="input x y for each file by user")

parser.add_option("-q", "--quality", dest="q",action="store",help="input xvid q arg",default="24")

parser.add_option("-v", "--vcodec", dest="vcodec",action="store",help="input video codec",default="x264")

parser.add_option("-n", "--noaudio", dest="an",action="store_true",help="no audio")

parser.add_option("-p", "--preset", dest="preset",action="store",help="",default="")

parser.add_option("-m", "--maxWidth", dest="maxWidth",action="store",help="input max width for output video",default="")

parser.add_option("-f", "--fileType", dest="fileType",action="store",help="",default="mp4")

parser.add_option("-o", "--ogg", dest="ogg",action="store_true",help="user ogg instead of aac",default="")

parser.add_option("-3", "--mp3", dest="mp3",action="store_true",help="user mp3 instead of aac",default="")

parser.add_option("-1", "--pad", dest="pad",action="store_true",help="pad to 16:9",default="")

parser.add_option("-s", "--src", dest="srcD",action="store",help="source dir",default="/home/gongjia/external/ffmpeg/ffmpeg-x86/videoin")

parser.add_option("-t", "--target", dest="targetD",action="store",help="target dir",default="/home/gongjia/external/ffmpeg/ffmpeg-x86/videoout")

parser.add_option("-w", "--workdir", dest="workdir",action="store",help="work dir",default="/home/gongjia/external/ffmpeg/ffmpeg-x86/video")

parser.add_option("-e", "--split", dest="split",action="store_true",help="split to multiple file with size")

parser.add_option("-d", "--splitsize", dest="splitsize",action="store",help="split to multiple file with size",default="2")#Minutes

parser.add_option("-j", "--prefix", dest="prefix",action="store",help="target file name prefix",default="")

(options, args) = parser.parse_args()

if options.srcD==None or options.srcD[0:1]=='-':

print 'srcD Err, quit'

exit()

if options.targetD==None or options.targetD[0:1]=='-':

print 'targetD Err, quit'

exit()

if options.fileType==None or options.fileType[0:1]=='-':

print 'fileType Err, quit'

exit()

if options.workdir==None or options.workdir[0:1]=='-':

print 'workdir Err, quit'

exit()

#遍歷videoin下的文件

for root,dirs,files in os.walk(options.srcD):

for name in files:

name= name.replace('[','''\[''')#對文件名中的[進行轉義

newname =name[0: name.rindex('.')]

print "Test newname: " + newname

print "Test name: " + name

#運行

cmd ='cd '+options.workdir+';mkdir -p ffm; rm -f ffm/ffm.txt ; csh -c "(ffmpeg -i '+options.srcD+'/' +name+ ' >& ffm/ffm.txt)"; grep Duration ffm/ffm.txt'

print cmd

(si, so, se) = os.popen3(cmd)

t=so.readlines()

reg='''Duration\:\s(\d+)\:(\d+)\:([\d\.]+)'''

duration=0#second

for line in t:

result = re.compile(reg).findall(line)

for c in result:

print 'split file to',options.splitsize,'minutes, Duration:',c[0],c[1],c[2]

duration = int(c[0])*3600 + int(c[1])*60+float(c[2])

nameLength=int(math.log(int(duration / (int(options.splitsize)*60)) )/math.log(10)) + 1

for i in range(int(duration / (int(options.splitsize)*60)) + 1):

print i

_t = ''

if duration>int(options.splitsize)*60*(i+1):

_t = str(int(options.splitsize)*60)

else:

_t = str(duration-int(options.splitsize)*60*i)

cmd ='csh -c "' + "cd "+options.workdir+";touch ffm/output.log;(ffmpeg -y -i "+options.srcD+"/"+name+" -codec: copy -ss "+str(i*int(options.splitsize)*60)+" -t "+_t+" "+options.targetD+"/"+options.prefix+newname+'_'+string.replace(('%'+str(nameLength)+'s')%str(i),' ','0')+"."+options.fileType + ' >>& ffm/output.log)"'

print cmd

(si, so, se) = os.popen3(cmd)

for line in se.readlines() :#打印輸出

print line

總結

以上是生活随笔為你收集整理的python调用ffmpeg合并_用ffmpeg命令处理mp4剪切与合并的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。