OSDN Git Service

change x264 encode option.
[rec10/rec10-git.git] / rec10 / trunk / src / ts2x264.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
5 import commands
6 import os
7 import re
8 import time
9 import tv2avi
10 import recdblist
11 def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
12     """
13     pinで指定されたファイルをpoutにx264でエンコードして書き出す
14     """
15     dualpass = 0
16     isvfr=0
17     is24fps=0
18     islight=0
19     size="Tall"
20     quality=5
21     if re.search("2", opts):
22         dualpass = 1
23     if re.search("H", opts):
24         size = "HD"
25     if re.search("S", opts):
26         size = "SD"
27         quality=quality-2
28     if re.search("F", opts):
29         size = "FHD"
30     if re.search("L", opts):
31         size = "Short"
32         quality=quality-2
33     if re.search("G", opts):
34         size = "LowHD"
35     if re.search("v", opts):
36         is24fps=1
37     if re.search("a", opts):
38         is24fps=1
39         quality=quality+2
40     if re.search("l",opts):
41         quality=quality-2
42     if re.search("h",opts):
43         quality=quality+2
44     if re.search("d",opts):
45         encode_ffmpeg(pin, pout, size, dualpass ,is24fps , quality)
46     elif re.search("5",opts):
47         encode_ffmpeg(pin, pout, size, dualpass ,is24fps , quality)
48     else:
49         encode(pin, pout,size,dualpass,is24fps,quality)
50 def encode(pin,pout,size,is2pass,is24fps,quality):
51     encvf=""
52     txt=""
53     encvf="-vf yadif=0,pp=l5"
54     harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
55     ofps="-ofps 30000/1001"
56     fps="-fps 30000/1001"
57     bm=1
58     scale=""
59     bnum=2000
60     if size == "HD":
61         encvf = encvf + ",scale=1280:720::0:3"+harddup
62         bitrate = "bitrate=" + str(tv2avi.Bitrate_HD*bm)
63         bnum=tv2avi.Bitrate_HD*bm
64         crf="crf=20"
65     elif size == "SD":
66         encvf = encvf + ",scale=720:480::0:3"+harddup
67         bitrate = "bitrate=" + str(tv2avi.Bitrate_SD*bm)
68         bnum=tv2avi.Bitrate_SD*bm
69         crf="crf=20"
70     elif size == "FHD":
71         encvf = encvf + ",scale=1920:1080::0:3"+harddup
72         bitrate = "bitrate=" + str(tv2avi.Bitrate_FHD*bm)
73         bnum=tv2avi.Bitrate_FHD*bm
74         crf="crf=22"
75     elif size == "Short":
76         encvf = encvf +harddup
77         bitrate = "bitrate=" + str(tv2avi.Bitrate_Short*bm)
78         bnum=tv2avi.Bitrate_Short*bm
79         crf="crf=20"
80     elif size == "LowHD":
81         encvf = encvf + ",scale=1280:720::0:3"+harddup
82         bitrate = "bitrate=" + str(tv2avi.Bitrate_LowHD*bm)
83         bnum=tv2avi.Bitrate_LowHD*bm
84         crf="crf=22"
85     else:
86         encvf = encvf + ",scale=1280:720::0:3"+harddup
87         bitrate = "bitrate=" + str(tv2avi.Bitrate_HD*bm)
88         bnum=tv2avi.Bitrate_HD*bm
89         crf="crf=22"
90     x264enc=""
91     x264enc1=""
92     x264enc2=""
93
94     if quality<3:
95         x264enc=":me=dia:partitions=none:frameref=1:subq=2:scenecut=0:trellis=0"
96         x264enc1=x264enc
97         x264enc2=x264enc1
98     elif quality<5:
99         x264enc=":me=dia:partitions=i8x8,i4x4:frameref=1:subq=4:trellis=0"
100         x264enc1=x264enc
101         x264enc2=x264enc1
102     elif quality<7:
103         x264enc=":8x8dct:bframes=1"
104         x264enc1=x264enc
105         x264enc2=x264enc1
106     elif quality<9:
107         x264enc=":direct_pred=auto:frameref=5:subq=6:8x8dct:bframes=3:trellis=1"
108         x264enc1=x264enc
109         x264enc2=x264enc1
110     elif quality<11:
111         x264enc=":frameref=5:subq=7:trellis=2:partitions=all:8x8dct:direct_pred=auto:me=umh:bframes=3"
112         x264enc1=x264enc
113         x264enc2=x264enc1
114     crf=""
115     if is24fps==1:
116         ofps="-ofps 24000/1001"
117         fps="-fps 30000/1001"
118         encvf="-vf pullup,softskip"
119         harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
120         bm=1
121         if quality<3:
122             vbv=":vbv_maxrate="+str(bnum*2)+":vbv_bufsize="+str(bnum*4)
123             x264enc=":me=dia:aq-strength=0.6:deblock=1,1:psy-rd=0.4:partitions=none:frameref=1:subq=2:scenecut=0:trellis=0"
124             x264enc1=x264enc+vbv
125             x264enc2=x264enc1
126         elif quality<5:
127             vbv=":vbv_maxrate="+str(bnum*2)+":vbv_bufsize="+str(bnum*4)
128             x264enc=":me=dia:aq-strength=0.6:deblock=1,1:psy-rd=0.4:partitions=i8x8,i4x4:frameref=1:subq=4:trellis=0"
129             x264enc1=x264enc+vbv
130             x264enc2=x264enc1
131         elif quality<7:
132             vbv=":vbv_maxrate="+str(bnum*2)+":vbv_bufsize="+str(bnum*4)
133             x264enc=":aq-strength=0.6:8x8dct:deblock=1,1:psy-rd=0.4:bframes=1"
134             x264enc1=x264enc+vbv
135             x264enc2=x264enc1
136         elif quality<9:
137             vbv=":vbv_maxrate="+str(bnum*2)+":vbv_bufsize="+str(bnum*4)
138             x264enc=":direct_pred=auto:aq-strength=0.6:deblock=1,1:psy-rd=0.4:8x8dct:frameref=5:subq=6:bframes=3:trellis=1"
139             x264enc1=x264enc+vbv
140             x264enc2=x264enc1
141         elif quality<11:
142             vbv=":vbv_maxrate="+str(bnum*2)+":vbv_bufsize="+str(bnum*4)
143             x264enc=":frameref=5:subme=8:subq=7:trellis=2:aq-strength=0.6:deblock=1,1:psy-rd=0.4:partitions=all:8x8dct:direct_pred=auto:me=umh:bframes=3"
144             x264enc1=x264enc+vbv
145             x264enc2=x264enc1
146     if is2pass == 1:
147         pas1exe = "mencoder \'" + pin + "\' -vfm ffmpeg -quiet -nosound -sws 9 -ovc x264 " + encvf +" "+fps+" "+ofps+" -x264encopts " + bitrate +x264enc1+ ":threads=auto:level_idc=41:pass=1:turbo=2 -passlogfile \'" + pin + ".log\' " + " -o /dev/null"
148         pas2exe = "mencoder \'" + pin + "\' -vfm ffmpeg -quiet -sws 9 -ovc x264 " + encvf +" "+fps+ " "+ofps+" -x264encopts " + bitrate +x264enc2+ ":level_idc=41:threads=auto:pass=2 -passlogfile \'" + pin + ".log\' -oac mp3lame -lameopts cbr:br=256 -o \'" + pout + "\'"
149         pas1exe = "nice -n 19 " + pas1exe
150         pas2exe = "nice -n 19 " + pas2exe
151         recdblist.printutf8(pas1exe)
152         txt=txt+"Cmd : "+pas1exe+"\n"
153         txtt=""
154         try:
155             txtt=unicode(commands.getoutput(pas1exe.encode('utf-8')),'utf-8')
156         except:
157             ""
158         txt=txt+txtt
159         recdblist.printutf8(pas2exe)
160         time.sleep(5)
161         txtt=""
162         try:
163             txtt=unicode(commands.getoutput(pas2exe.encode('utf-8')),'utf-8')
164         except:
165             ""
166         txt=txt+txtt
167     else:
168         pas1exe = "mencoder \'" + pin + "\' -vfm ffmpeg -quiet -sws 9 -ovc x264 " + encvf+" "+fps + " "+ofps+" -x264encopts " + bitrate +x264enc+":level_idc=41:threads=auto -oac mp3lame -lameopts cbr:br=256 -o \'" + pout + "\'"
169         pas1exe = "nice -n 19 " + pas1exe
170         recdblist.printutf8(pas1exe)
171         txt=txt+"Cmd : "+pas1exe+"\n"
172         txtt=""
173         try:
174             txtt=unicode(commands.getoutput(pas1exe.encode('utf-8')),'utf-8')
175         except:
176             ""
177         txt=txt+txtt
178     recdblist.addlog( pin, txt, "mencoder-log")
179
180 def encode_ffmpeg(pin,pout,size,is2pass,is24fps,quality):
181     """
182     
183     """
184     ffmpeg="ffmpeg"
185     if quality<3:
186         x264enc="-level 42 -me_method dia -partitions none -refs 1 -scenecut 0 -trellis 0 "
187         x264enc1=x264enc
188         x264enc2=x264enc1
189     elif quality<5:
190         x264enc="-level 42 -me_method dia -partitions parti8x8+parti4x4 -refs 1 -trellis 0 "
191         x264enc1="-level 42 -me_method dia -partitions none -refs 1 -trellis 0 "
192         x264enc1=x264enc
193         x264enc2=x264enc
194     elif quality<7:
195         x264enc="-level 42 "
196         x264enc1="-level 42 -me_method dia -partitions none "
197         x264enc1=x264enc
198         x264enc2=x264enc
199     elif quality<9:
200         x264enc="-level 42 -directpred auto -refs 5 -subq 6 -bf 1 -trellis 1 "
201         x264enc1="-level 42 -me_method dia -partitions none -refs 1 -subq 6 -bf 1 -trellis 1 "
202         x264enc1=x264enc
203         x264enc2=x264enc
204     elif quality<11:
205         x264enc="-level 42 -refs 5 -subq 7 -trellis 2 -partitions all -directpred auto -me_method umh -bf 3 "
206         x264enc1="-level 42 -me_method dia -partitions none -refs 1 -subq 7 -trellis 2 -directpred auto -bf 3 "
207         x264enc1=x264enc
208         x264enc2=x264enc
209     if is24fps==1:
210         if quality<3:
211             x264enc="-level 42 -me_method dia -partitions none -refs 1 -scenecut 0 -trellis 0 -deblockalpha 1 -deblockbeta 1 "
212             x264enc1="-level 42 -me_method dia -partitions none -refs 1 -scenecut 0 -trellis 0 -deblockalpha 1 -deblockbeta 1 "
213             x264enc1=x264enc
214             x264enc2=x264enc
215         elif quality<5:
216             x264enc="-level 42 -me_method dia -partitions parti8x8+parti4x4 -refs 1 -trellis 0 -deblockalpha 1 -deblockbeta 1 "
217             x264enc1="-level 42 -me_method dia -partitions none -refs 1 -trellis 0 -deblockalpha 1 -deblockbeta 1 "
218             x264enc1=x264enc
219             x264enc2=x264enc
220         elif quality<7:
221             x264enc="-level 42 -deblockalpha 1 -deblockbeta 1 "
222             x264enc1="-me_method dia -partitions none -vpre hq -level 42 -deblockalpha 1 -deblockbeta 1 "
223             x264enc1=x264enc
224             x264enc2=x264enc
225         elif quality<9:
226             x264enc="-level 42 -directpred auto -refs 5 -subq 6 -bf 1 -trellis 1 -deblockalpha 1 -deblockbeta 1 "
227             x264enc1="-level 42 -directpred auto -me_method dia -partitions none -refs 1 -subq 6 -bf 1 -trellis 1 -deblockalpha 1 -deblockbeta 1 "
228             x264enc1=x264enc
229             x264enc2=x264enc
230         elif quality<11:
231             x264enc="-level 42 -refs 5 -subq 7 -trellis 2 -partitions all -directpred auto -me_method umh -bf 3 -deblockalpha 1 -deblockbeta 1 "
232             x264enc1="-level 42 -refs 1 -subq 7 -trellis 2 -partitions none -directpred auto -me_method dia -bf 3 -deblockalpha 1 -deblockbeta 1 "
233             x264enc1=x264enc
234             x264enc2=x264enc
235     if size == "HD":
236         s = "-s 1280x720 "
237         bitrate = str(tv2avi.Bitrate_HD)
238         crf="crf=20"
239     elif size == "SD":
240         s ="-s 720x480 "
241         bitrate = str(tv2avi.Bitrate_SD)
242         crf="crf=20"
243     elif size == "FHD":
244         s ="-s 1920x1080 "
245         bitrate = str(tv2avi.Bitrate_FHD)
246         crf="crf=22"
247     elif size == "Short":
248         s= ""
249         bitrate = str(tv2avi.Bitrate_Short)
250         crf="crf=20"
251     elif size == "LowHD":
252         s = "-s 1280x720 "
253         bitrate = str(tv2avi.Bitrate_LowHD)
254         crf="crf=22"
255     else:
256         s = "-s 1280x720 "
257         bitrate = str(tv2avi.Bitrate_LowHD)
258         crf="crf=22"
259     txt=""
260     if is2pass==1:
261         os.environ['LANG']="ja_JP.UTF-8"
262         exe1=ffmpeg+" -y -i \""+pin+"\" -pass 1 -vsync 400 -vcodec libx264 -vpre fastfirstpass -b "+bitrate+"k "+x264enc1+s+"-deinterlace -an -f avi /dev/null"
263         exe2=ffmpeg+" -y -i \""+pin+"\" -pass 2 -vsync 400 -vcodec libx264 -vpre hq -b "+bitrate+"k "+x264enc2+s+"-deinterlace -an -f avi \"" +pout+"\"" #aviにすべきかどうか
264         recdblist.printutf8(exe1)
265         exe1 = "nice -n 19 " + exe1
266         exe2 = "nice -n 19 " + exe2
267         txt=txt+u"Cmd : "+exe1+"\n"
268         txtt=""
269         try:
270             txtt=unicode(commands.getoutput(exe1.encode('utf-8')),'utf-8')
271         except:
272             ""
273         txt=txt+txtt
274         recdblist.addlog( pin, txt, "dual audio ffmpeg-log - 1pass")
275         txt=""
276         txt=txt+u"Cmd : "+exe2+"\n"
277         txtt=""
278         try:
279             txtt=unicode(commands.getoutput(exe2.encode('utf-8')),'utf-8')
280         except:
281             ""
282         txt=txt+txtt
283         recdblist.addlog( pin, txt, "dual audio ffmpeg-log - 2pass")
284     else:
285         os.environ['LANG']="ja_JP.UTF-8"
286         exe=ffmpeg+" -y -i \""+pin+"\" -vsync 400 -vcodec libx264 -vpre hq -b "+bitrate+"k "+x264enc+s+"-deinterlace -an \"" +pout+"\""
287         txt=txt+"Cmd : "+exe+"\n"
288         exe = "nice -n 19 " + exe
289         txt=txt+"Cmd : "+exe+"\n"
290         txtt=""
291         try:
292             txtt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
293         except:
294             ""
295         txt=txt+txtt
296         recdblist.addlog( pin, txt, "dual audio ffmpeg-log")
297