OSDN Git Service

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