转换工具层出不穷,ffmpeg才是全能的转换工具,只是不支持图形操作。
没有关系,命令行方式,在freebsd/linux下直接来
我们的思路是,设定一个文件夹存放源视频文件,python读取该文件夹下的全部文件,并对文件通过ffmpeg进行分析,根据需要,修改目标文件的编码、分辨率等等,调用ffmpeg转换。
我这次的需求是,我家液晶电视只支持分辨来,长宽均小于720,编码只支持divx/xvid的avi文件,且fps只能小于25——多次实践,才总结出来的,电视说明书也没说!!
下面的程序将
1
|
/ root / / root2 / video / origin
|
下存在的全部文件转换成液晶电视需要的avi格式电影
以下是最新的修改,引入了optionparser 参数分析工具。能指定最大宽度,音视频编码,视频质量,原路径,目的路径,工作路径等
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# 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( '[' , '''\[''' ) #对文件名中的[进行转义
newname = name[ 0 : name.rindex( '.' )]
#运行一次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存储16:9的,将其转换为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
#运行
(si, so, se) = os.popen3(cmd)
for line in se.readlines() : #打印输出
print line
for line in so.readlines() : #打印输出
print line
#print cmd,' finish'#再显示一次命令
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/zeeeitch/article/details/6864030