python整合ffmpeg實(shí)現(xiàn)視頻文件的批量轉(zhuǎn)換
轉(zhuǎn)換工具層出不窮,ffmpeg才是全能的轉(zhuǎn)換工具,只是不支持圖形操作。
沒有關(guān)系,命令行方式,在freebsd/linux下直接來(lái)
我們的思路是,設(shè)定一個(gè)文件夾存放源視頻文件,python讀取該文件夾下的全部文件,并對(duì)文件通過(guò)ffmpeg進(jìn)行分析,根據(jù)需要,修改目標(biāo)文件的編碼、分辨率等等,調(diào)用ffmpeg轉(zhuǎn)換。
我這次的需求是,我家液晶電視只支持分辨來(lái),長(zhǎng)寬均小于720,編碼只支持divx/xvid的avi文件,且fps只能小于25——多次實(shí)踐,才總結(jié)出來(lái)的,電視說(shuō)明書也沒說(shuō)?。?/p>
下面的程序?qū)?/p>
/root//root2/video/origin
下存在的全部文件轉(zhuǎn)換成液晶電視需要的avi格式電影
以下是最新的修改,引入了OptionParser 參數(shù)分析工具。能指定最大寬度,音視頻編碼,視頻質(zhì)量,原路徑,目的路徑,工作路徑等
# coding=gb2312
import string
import os
import time
import re
import sys
from optparse import OptionParser
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="/usr/disk2/root/video/origin")
parser.add_option("-t", "--target", dest="targetD",action="store",help="target dir",default="/usr/disk2/root/video/ok")
parser.add_option("-w", "--workdir", dest="workdir",action="store",help="work dir",default="/root/root2/video")
(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()
#遍歷origin下的文件
for root,dirs,files in os.walk(options.srcD):
for name in files:
name= name.replace('[','''\[''')#對(duì)文件名中的[進(jìn)行轉(zhuǎn)義
newname =name[0: name.rindex('.')]
#運(yùn)行一次ffmpeg,獲取分辨率
(si, so, se) = os.popen3('cd '+options.workdir+';mkdir -p ffm; rm -f ffm/ffm.txt ; csh -c "(ffmpeg -i '+options.srcD+'/' +name+ ' >& ffm/ffm.txt)"; grep Stream ffm/ffm.txt')
t=so.readlines()
ti=0
for line in se.readlines() :
print line
width=0
height=0
reg='''^\s*Stream.*,\s*(\d+)x(\d+)(?: \[SAR|,)'''
#Stream #0.0: Video: RV40 / 0x30345652, 1020x572, 23 fps, 23 tbr, 23 tbn, 23 tbc
for line in t:
result = re.compile(reg).findall(line)
for c in result:
print name+' '+c[0] + 'x' + c[1]
width=string.atoi(c[0])
height=string.atoi(c[1])
if name[0:3]=='M2U' and width==720 and height==576:#m2U開頭的,寬度是720x576的,是4:3存儲(chǔ)16:9的,將其轉(zhuǎn)換為16:9
width=1024
if width==0:
print 'error parsing width and height'
exit()
vc=''
qstr=''
astr=''
vpre=''
s=''
if options.maxWidth!='':
if width>string.atoi(options.maxWidth):
height = height * string.atoi(options.maxWidth) / width
width = string.atoi(options.maxWidth)
padStr=''
if options.pad==True:
if height*16/9 - width>10:#寬度不夠
padStr=' -vf "pad='+str(height*16/9)+':'+str(height)+':'+str((height*16/9 - width)/2)+':0:black"'
elif width - height*16/9 >10:#高度不夠
padStr=' -vf "pad='+str(width)+':'+str(width*9/16)+':0:'+str((width - height*16/9)/2)+':black"'
s=' -s '+str(width)+'x'+str(height)+padStr
print 'adjust',s
if options.preset!='':
vpre=' -vpre '+options.preset
if options.an==True:
astr=' -an'
elif options.ogg==True:
astr=' -acodec libvorbis -ar 44100 -ab 64K'
elif options.mp3==True:
astr=' -acodec libmp3lame -ar 44100 -ab 64K'
else:
astr=' -acodec libfaac -ar 44100 -ab 64K'
if options.vcodec=='vp8':
vc='libvpx'
qstr=" -qmin "+options.q+" -qmax "+options.q
elif options.vcodec=='x264':
vc='libx264'
qstr=" -crf "+options.q
elif options.vcodec=='xvid':
vc='libxvid'
qstr=" -qmin "+options.q+" -qmax "+options.q
cmd ='csh -c "' + "cd "+options.workdir+";touch ffm/output.log;(ffmpeg -y -i "+options.srcD+"/"+name+astr+" -vcodec "+vc+vpre+qstr+s+" -r 25 -threads 8 "+options.targetD+"/"+newname+"."+options.fileType + ' >>& ffm/output.log)"'
print cmd
#運(yùn)行
(si, so, se) = os.popen3(cmd)
for line in se.readlines() :#打印輸出
print line
for line in so.readlines() :#打印輸出
print line
#print cmd,' finish'#再顯示一次命令
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ffmpeg+Python實(shí)現(xiàn)B站MP4格式音頻與視頻的合并示例代碼
- Python3 ffmpeg視頻轉(zhuǎn)換工具使用方法解析
- python ffmpeg任意提取視頻幀的方法
- python opencv 讀取本地視頻文件 修改ffmpeg的方法
- python利用ffmpeg進(jìn)行錄制屏幕的方法
- Python3.6.2調(diào)用ffmpeg的方法
- python+ffmpeg批量去視頻開頭的方法
- 利用python和ffmpeg 批量將其他圖片轉(zhuǎn)換為.yuv格式的方法
- python+ffmpeg視頻并發(fā)直播壓力測(cè)試
- python調(diào)用系統(tǒng)ffmpeg實(shí)現(xiàn)視頻截圖、http發(fā)送
- Python調(diào)用ffmpeg開源視頻處理庫(kù),批量處理視頻
相關(guān)文章
對(duì)dataframe進(jìn)行列相加,行相加的實(shí)例
今天小編就為大家分享一篇對(duì)dataframe進(jìn)行列相加,行相加的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
python實(shí)習(xí)總結(jié)(yeild,async,azwait和協(xié)程)
今天是Python實(shí)習(xí)的第一天,熟悉了環(huán)境,第一次使用macbook,氛圍還不錯(cuò),努力學(xué)習(xí)新知識(shí),希望本片文章能給你帶來(lái)幫助2021-10-10
Android申請(qǐng)相機(jī)權(quán)限和讀寫權(quán)限實(shí)例
大家好,本篇文章主要講的是Android申請(qǐng)相機(jī)權(quán)限和讀寫權(quán)限實(shí)例,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02
Python高級(jí)過(guò)濾器之filter函數(shù)詳解
在Python中,filter()是一個(gè)非常有用的內(nèi)置函數(shù),它能夠根據(jù)指定的函數(shù)來(lái)篩選出可迭代對(duì)象中滿足條件的元素,本文將從入門到精通,全面介紹filter()函數(shù)的用法和相關(guān)知識(shí)點(diǎn)2023-08-08
Python3實(shí)現(xiàn)自定義比較排序/運(yùn)算符
這篇文章主要介紹了Python3實(shí)現(xiàn)自定義比較排序/運(yùn)算符,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
python實(shí)現(xiàn)代碼行數(shù)統(tǒng)計(jì)示例分享
這篇文章主要介紹了python實(shí)現(xiàn)代碼行數(shù)統(tǒng)計(jì)的示例,需要的朋友可以參考下2014-02-02
django 發(fā)送郵件和緩存的實(shí)現(xiàn)代碼
這篇文章主要介紹了django 發(fā)送郵件和緩存的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Python代碼解決RenderView窗口not found問(wèn)題
這篇文章主要介紹了Python代碼解決RenderView窗口not found問(wèn)題,需要的朋友可以參考下2016-08-08

