OSDN Git Service

implement BonTsDemux Audio Split option.
[rec10/rec10-git.git] / rec10 / trunk / src / ts2x264.py
1 #!/usr/bin/python\r
2 # coding: UTF-8\r
3 # Rec10 TS Recording Tools\r
4 # Copyright (C) 2009-2011 Yukikaze\r
5 import commands\r
6 import configreader\r
7 import os\r
8 import os.path\r
9 import re\r
10 import random\r
11 import time\r
12 import traceback\r
13 import zip\r
14 \r
15 import recdblist\r
16 def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"\r
17     """\r
18     pinで指定されたファイルをpoutにx264でエンコードして書き出す\r
19     """\r
20     dualpass = 0\r
21     is24fps=0\r
22     size="HD"\r
23     crf=18\r
24     quality=4\r
25     quality=int(configreader.getConfEnv("x264_preset"))\r
26     crf=int(configreader.getConfEnv("crf"))\r
27     deinterlace=1\r
28     if re.search("H", opts):\r
29         size = "HD"\r
30     if re.search("S", opts):\r
31         size = "WVGA"\r
32     if re.search("F", opts):\r
33         size = "FullHD"\r
34     if re.search("W",opts):\r
35         size = "WVGA"\r
36     if re.search("MW1",opts):\r
37         size = "QVGA_BASE"\r
38         crf=crf+4\r
39     if re.search("MW2", opts):\r
40         size = "WVGA_BASE"\r
41         crf=crf+2\r
42     if re.search("v", opts):\r
43         is24fps=1\r
44         crf=int(configreader.getConfEnv("animation_crf"))\r
45     if re.search("a", opts):\r
46         is24fps=1\r
47         crf=int(configreader.getConfEnv("animation_crf"))\r
48     if re.search("I", opts):\r
49         deinterlace=0\r
50     if re.search("q",opts):\r
51         quality=quality-2\r
52     if re.search("w",opts):\r
53         quality=quality-1\r
54     if re.search("e",opts):\r
55         quality=quality+1\r
56     if re.search("r",opts):\r
57         quality=quality+2\r
58     if re.search("u",opts):\r
59         crf=crf+2\r
60     if re.search("i",opts):\r
61         crf=crf+1\r
62     if re.search("o",opts):\r
63         crf=crf-1\r
64     if re.search("p",opts):\r
65         crf=crf-2\r
66     if re.search("d",opts):#二カ国語放送の場合\r
67         tm2v=pin.replace(".ts",".m2v")\r
68         encodeFfmpegSar(tm2v,pout,size,is24fps,quality,crf,deinterlace)\r
69     elif re.search("5",opts):#5.1chの場合\r
70         encodeFfmpegSar(pin,pout,size,is24fps,quality,crf,deinterlace)\r
71     elif re.search("0",opts):#BonTsDemuxを使いたい場合\r
72         tm2v=pin.replace(".ts",".m2v")\r
73         encodeFfmpegSar(tm2v,pout,size,is24fps,quality,crf,deinterlace)\r
74     else:\r
75         try:\r
76             encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=1)\r
77             if os.path.exists(pout):\r
78                 if not os.path.getsize(pout)>1*1000:\r
79                     recdblist.addCommonlogEX("Warning", "Old mencoder option used.","","",log_level=200)\r
80                     try:\r
81                         encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=0)\r
82                     except Exception, inst:\r
83                         recdblist.addCommonlogEX("Error", "ts2x264_mencoder_old(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)\r
84             else:\r
85                 recdblist.addCommonlogEX("Warning", "Old mencoder option used.","","",log_level=200)\r
86                 try:\r
87                     encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=0)\r
88                 except Exception, inst:\r
89                     recdblist.addCommonlogEX("Error", "ts2x264_mencoder_old(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)\r
90         except Exception, inst:\r
91             recdblist.addCommonlogEX("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)\r
92 def encodeMencoderSar(pin,pout,size,is24fps,quality,crf,deinterlace=1,uselavf=0):\r
93     mencoder=configreader.getConfPath("mencoder")\r
94     tee=configreader.getConfPath("tee")\r
95     encvf=""\r
96     txt=""\r
97     encvf="-sws 9 -vf yadif=0,pp=l5"\r
98     harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"\r
99     ofps="-ofps 30000/1001"\r
100     fps="-fps 30000/1001"\r
101     x264fps="30000/1001"\r
102     x264streamsize=""\r
103     x264preset=""\r
104     x264tune=""\r
105     x264_bitrate="5000"\r
106     tsar=getMoviePAR2(pin)\r
107     x264sar=str(tsar[0])+":"+str(tsar[1])\r
108     lavft=""\r
109     if is24fps==1:\r
110         ofps="-ofps 24000/1001"\r
111         fps="-fps 30000/1001"\r
112         x264fps="24000/1001"\r
113         x264tune="--tune animation"\r
114         encvf="-sws 9 -vf pullup,softskip"\r
115         harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"\r
116     if size == "HD":\r
117         tsize=getParSize(pin,720)\r
118         encvf = encvf + ",scale=-3:720::0:3"+harddup\r
119         x264streamsize=str(tsize[0])+u"x720"\r
120         x264_bitrate="2500"\r
121     elif size == "WVGA":\r
122         tsize=getParSize(pin,480)\r
123         encvf = encvf + ",scale=-3:480::0:3"+harddup\r
124         x264streamsize=str(tsize[0])+u"x480"\r
125         x264_bitrate="1500"\r
126     elif size == "FullHD":\r
127         tsize=getParSize(pin,1080)\r
128         encvf = encvf + ",scale=-3:1080::0:3"+harddup\r
129         x264streamsize=str(tsize[0])+u"x1080"\r
130         x264_bitrate="5000"\r
131     elif size == "QVGA_BASE":\r
132         tsize=getParSize(pin,240)\r
133         encvf = encvf + ",scale=-3:240::0:3"+harddup\r
134         x264streamsize=str(tsize[0])+u"x240"\r
135         x264_bitrate="300"\r
136     elif size == "WVGA_BASE":\r
137         tsize=getParSize(pin,480)\r
138         encvf = encvf + ",scale=-3:480::0:3"+harddup\r
139         x264streamsize=str(tsize[0])+u"x480"\r
140         x264_bitrate="1500"\r
141     else:\r
142         tsize=getParSize(pin,720)\r
143         encvf = encvf + ",scale=-3:720::0:3"+harddup\r
144         x264streamsize=str(tsize[0])+u"x720"\r
145         x264_bitrate="2500"\r
146     if deinterlace==0:\r
147         tsize=getMovieBaseSize2(pin)\r
148         ofps="-ofps 30000/1001"\r
149         #fps="-fps 30000/1001"\r
150         fps=""\r
151         x264fps="30000/1001"\r
152         x264tune=x264tune+" --tff --nal-hrd vbr"\r
153         encvf="-vf hqdn3d=2:1:2"\r
154         harddup=",harddup"\r
155         encvf=encvf+harddup\r
156         x264streamsize=str(tsize[0])+u"x"+str(tsize[1])\r
157     if tsize[0] <= 0 or tsize[1] <= 0:\r
158         encvf="-sws 9 -vf yadif=0,pp=l5"\r
159         harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"\r
160         ofps="-ofps 30000/1001"\r
161         fps="-fps 30000/1001"\r
162         x264fps="30000/1001"\r
163         tsize=[1280,720]\r
164         encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup\r
165         x264streamsize=u"1280x720"\r
166         x264_bitrate="2500"\r
167     if quality==1:\r
168         x264preset=u"ultrafast"\r
169     elif quality==2:\r
170         x264preset=u"veryfast"\r
171     elif quality==3:\r
172         x264preset=u"fast"\r
173     elif quality==4:\r
174         x264preset=u"medium"\r
175     elif quality==5:\r
176         x264preset=u"slow"\r
177     elif quality==6:\r
178         x264preset=u"slower"\r
179     else:\r
180         x264preset=u"medium"\r
181     if size == "WVGA_BASE" or size == "QVGA_BASE":\r
182         x264profile=" --level 32 --profile baseline "\r
183     else:\r
184         x264profile=" --level 41 --profile high "\r
185     if uselavf==1:\r
186         lavft="-demuxer lavf "\r
187     x264crf=str(crf)\r
188     os.environ['LANG']="ja_JP.UTF-8"\r
189     random.seed(pin)\r
190     random.jumpahead(10)\r
191     temptime=int(time.time())\r
192     temptime=temptime % 9697\r
193     random.jumpahead(temptime)\r
194     streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))\r
195     unicode(commands.getoutput(u"mkfifo "+streampath),'utf-8','ignore')\r
196     logmencoder=recdblist.getLogTitle(pin)+".mencoder.log"\r
197     logx264=recdblist.getLogTitle(pin)+".x264.log"\r
198     encexe=mencoder+u" \""+pin+u"\" -quiet -vfm ffmpeg "+lavft+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" 2>&1 | "+tee+" \""+logmencoder+"\" & "\r
199     encexe=encexe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf)\r
200     #encexe=encexe+u" nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u"  --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" "+streampath+" "+x264streamsize\r
201     encexe=u"nice -n 19 " +encexe+" 2>&1 | "+tee+" \""+logx264+"\""\r
202     try:\r
203         recdblist.addCommandSelfLog(pin, encexe)\r
204     except Exception, inst:\r
205         print type(inst)\r
206         print str(inst)\r
207         print traceback.print_exc(file=sys.stdout)\r
208     recdblist.printutf8(encexe)\r
209     txt=""\r
210     try:\r
211         txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8','ignore')\r
212     except:\r
213         ""\r
214     os.remove(streampath)\r
215     zip.addFile2FileZip(logmencoder, recdblist.getLogTitle(pin)+".log.zip")\r
216     zip.addFile2FileZip(logx264, recdblist.getLogTitle(pin)+".log.zip")\r
217     os.remove(logmencoder)\r
218     os.remove(logx264)\r
219     recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)\r
220 #    recdblist.addCommandLogTar(pin,u"Mencoder","mencoder", encexe,txt)\r
221 def encodeFfmpegSar(pin,pout,size,is24fps,quality,crf,deinterlace=1):\r
222     """\r
223 \r
224     """\r
225     ffmpeg=configreader.getConfPath("ffmpeg")\r
226     tee=configreader.getConfPath("tee")\r
227     fps=u"-r 29.970030 "\r
228     x264fps="30000/1001"\r
229     x264streamsize=""\r
230     x264preset=""\r
231     x264tune=""\r
232     x264_bitrate="2500"\r
233     x264_thread="auto"\r
234     tsar=getMoviePAR2(pin)\r
235     filter="-deinterlace"\r
236     x264sar=str(tsar[0])+":"+str(tsar[1])\r
237     if is24fps==1:\r
238         fps=u"-r 23.976023 "\r
239         x264fps="24000/1001"\r
240         x264tune="--tune animation"\r
241     if size == "HD":\r
242         tsize=getParSize(pin,720)\r
243         s = "-s "+str(tsize[0])+"x720 "\r
244         x264streamsize=str(tsize[0])+u"x720"\r
245         x264_bitrate="2500"\r
246     elif size == "WVGA":\r
247         tsize=getParSize(pin,480)\r
248         s = "-s "+str(tsize[0])+"x480 "\r
249         x264streamsize=str(tsize[0])+u"x480"\r
250         x264_bitrate="1500"\r
251     elif size == "FullHD":\r
252         tsize=getParSize(pin,1080)\r
253         s = "-s "+str(tsize[0])+"x1080 "\r
254         x264streamsize=str(tsize[0])+u"x1080"\r
255         x264_bitrate="5000"\r
256     elif size == "SD":\r
257         tsize=getParSize(pin,480)\r
258         s = "-s "+str(tsize[0])+"x480 "\r
259         x264streamsize=str(tsize[0])+u"x480"\r
260         x264_bitrate="1250"\r
261     elif size == "QVGA_BASE":\r
262         tsize=getParSize(pin,240)\r
263         s = "-s "+str(tsize[0])+"x240 "\r
264         x264streamsize=str(tsize[0])+u"x240"\r
265         x264_bitrate="300"\r
266     elif size == "WVGA_BASE":\r
267         tsize=getParSize(pin,480)\r
268         s = "-s "+str(tsize[0])+"x480 "\r
269         x264streamsize=str(tsize[0])+u"x480"\r
270         x264_bitrate="1500"\r
271     else:\r
272         tsize=getParSize(pin,720)\r
273         s = "-s "+str(tsize[0])+"x720 "\r
274         x264streamsize=str(tsize[0])+u"x720"\r
275         x264_bitrate="2500"\r
276     if deinterlace==0:\r
277         tsize=getMovieBaseSize2(pin)\r
278         fps=""\r
279         s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "\r
280         x264fps="30000/1001"\r
281         x264tune=x264tune+" --tff --nal-hrd vbr"\r
282         filter=""\r
283         x264streamsize=str(tsize[0])+u"x"+str(tsize[1])\r
284     if quality==1:\r
285         x264preset=u"ultrafast"\r
286     elif quality==2:\r
287         x264preset=u"veryfast"\r
288     elif quality==3:\r
289         x264preset=u"fast"\r
290     elif quality==4:\r
291         x264preset=u"medium"\r
292     elif quality==5:\r
293         x264preset=u"slow"\r
294     elif quality==6:\r
295         x264preset=u"slower"\r
296     else:\r
297         x264preset=u"medium"\r
298     if size == "WVGA_BASE" or size == "QVGA_BASE":\r
299         x264profile=" --level 32 --profile baseline "\r
300     else:\r
301         x264profile=" --level 41 --profile high "\r
302     x264crf=str(crf)\r
303     txt=""\r
304     os.environ['LANG']="ja_JP.UTF-8"\r
305     log_ffmpeg=recdblist.getLogTitle(pin)+".ffmpeg.log"\r
306     log_x264=recdblist.getLogTitle(pin)+".x264.log"\r
307     exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - 2> \""+log_ffmpeg+"\" | "\r
308     exe=exe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)\r
309     exe = "nice -n 19 " + exe+" 2>&1 | "+tee+" \""+log_x264+"\""\r
310     txt=""\r
311     #recdblist.addCommandSelfLog(log_ffmpeg,recdblist.getLogTitle(pin)+".log.zip")\r
312     #recdblist.addCommandSelfLog(log_x264,recdblist.getLogTitle(pin)+".log.zip")\r
313     recdblist.printutf8(exe)\r
314     recdblist.addCommandSelfLog(pin,exe)\r
315     try:\r
316         txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
317     except:\r
318         ""\r
319     recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)\r
320     zip.addFile2FileZip(log_ffmpeg, recdblist.getLogTitle(pin+".log.zip"))\r
321     zip.addFile2FileZip(log_x264, recdblist.getLogTitle(pin+".log.zip"))\r
322     os.remove(log_ffmpeg)\r
323     os.remove(log_x264)\r
324 def getMovieBaseSize(pin):\r
325     ffmpeg=configreader.getConfPath("ffmpeg")\r
326     os.environ['LANG']="ja_JP.UTF-8"\r
327     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"\r
328     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
329     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
330     sizeMaxX=0\r
331     sizeMaxY=0\r
332     txtls=txts.split("\n")\r
333     for t in txtls:\r
334         rM=rT.match(t)\r
335         if rM:\r
336             sizetxt=rM.group(1)\r
337             partxt=rM.group(2)\r
338             tX=int(sizetxt.split("x")[0])\r
339             tY=int(sizetxt.split("x")[1])\r
340             tEX=int(partxt.split(":")[0])\r
341             tEY=int(partxt.split(":")[1])\r
342             if sizeMaxX<tX:\r
343                 sizeMaxX=tX\r
344                 sizeMaxY=tY\r
345     return [sizeMaxX,sizeMaxY]\r
346 def getMovieBaseSize2(pin):##動画開始後すぐに解像度が変更されたときに対処\r
347     ffmpeg=configreader.getConfPath("ffmpeg")\r
348     os.environ['LANG']="ja_JP.UTF-8"\r
349     exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"\r
350     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
351     rT=re.compile(u"Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
352     sizeMaxX=0\r
353     sizeMaxY=0\r
354     txtls=txts.split("\n")\r
355     for t in txtls:\r
356         rM=rT.search(t)\r
357         if rM:\r
358             sizetxt=rM.group(1)\r
359             partxt=rM.group(2)\r
360             tX=int(sizetxt.split("x")[0])\r
361             tY=int(sizetxt.split("x")[1])\r
362             tEX=int(partxt.split(":")[0])\r
363             tEY=int(partxt.split(":")[1])\r
364             if sizeMaxX<tX:\r
365                 sizeMaxX=tX\r
366                 sizeMaxY=tY\r
367     os.remove(pin+".size.ts")\r
368     return [sizeMaxX,sizeMaxY]\r
369 def getMoviePAR(pin):\r
370     ffmpeg=configreader.getConfPath("ffmpeg")\r
371     os.environ['LANG']="ja_JP.UTF-8"\r
372     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"\r
373     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
374     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
375     sizeMaxX=0\r
376     parx=0\r
377     pary=0\r
378     txtls=txts.split("\n")\r
379     for t in txtls:\r
380         rM=rT.match(t)\r
381         if rM:\r
382             sizetxt=rM.group(1)\r
383             dartxt=rM.group(2)\r
384             tX=int(sizetxt.split("x")[0])\r
385             tY=int(sizetxt.split("x")[1])\r
386             tEX=int(dartxt.split(":")[0])\r
387             tEY=int(dartxt.split(":")[1])\r
388             if sizeMaxX<tX:\r
389                 sizeMaxX=tX\r
390                 if tX==1920 and tY==1080:\r
391                     parx=1\r
392                     pary=1\r
393                 else:\r
394                     parx=tEX\r
395                     pary=tEY\r
396     return [parx,pary]\r
397 def getMoviePAR2(pin):\r
398     ffmpeg=configreader.getConfPath("ffmpeg")\r
399     os.environ['LANG']="ja_JP.UTF-8"\r
400     exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"\r
401     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
402     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
403     sizeMaxX=0\r
404     parx=0\r
405     pary=0\r
406     txtls=txts.split("\n")\r
407     for t in txtls:\r
408         rM=rT.match(t)\r
409         if rM:\r
410             sizetxt=rM.group(1)\r
411             dartxt=rM.group(2)\r
412             tX=int(sizetxt.split("x")[0])\r
413             tY=int(sizetxt.split("x")[1])\r
414             tEX=int(dartxt.split(":")[0])\r
415             tEY=int(dartxt.split(":")[1])\r
416             if sizeMaxX<tX:\r
417                 sizeMaxX=tX\r
418                 if tX==1920 and tY==1080:\r
419                     parx=1\r
420                     pary=1\r
421                 else:\r
422                     parx=tEX\r
423                     pary=tEY\r
424     os.remove(pin+".size.ts")\r
425     return [parx,pary]\r
426 def getMovieDAR(pin):\r
427     ffmpeg=configreader.getConfPath("ffmpeg")\r
428     os.environ['LANG']="ja_JP.UTF-8"\r
429     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"\r
430     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
431     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")\r
432     sizeMaxX=0\r
433     darx=0\r
434     dary=0\r
435     txtls=txts.split("\n")\r
436     for t in txtls:\r
437         rM=rT.match(t)\r
438         if rM:\r
439             sizetxt=rM.group(1)\r
440             dartxt=rM.group(2)\r
441             tX=int(sizetxt.split("x")[0])\r
442             tY=int(sizetxt.split("x")[1])\r
443             tEX=int(dartxt.split(":")[0])\r
444             tEY=int(dartxt.split(":")[1])\r
445             if sizeMaxX<tX:\r
446                 sizeMaxX=tX\r
447                 if tX==1920 and tY==1080:\r
448                     darx=16\r
449                     dary=9\r
450                 else:\r
451                     darx=tEX\r
452                     dary=tEY\r
453     return [darx,dary]\r
454 def getMovieDAR2(pin):\r
455     ffmpeg=configreader.getConfPath("ffmpeg")\r
456     os.environ['LANG']="ja_JP.UTF-8"\r
457     exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"\r
458     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
459     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")\r
460     sizeMaxX=0\r
461     darx=0\r
462     dary=0\r
463     txtls=txts.split("\n")\r
464     for t in txtls:\r
465         rM=rT.match(t)\r
466         if rM:\r
467             sizetxt=rM.group(1)\r
468             dartxt=rM.group(2)\r
469             tX=int(sizetxt.split("x")[0])\r
470             tY=int(sizetxt.split("x")[1])\r
471             tEX=int(dartxt.split(":")[0])\r
472             tEY=int(dartxt.split(":")[1])\r
473             if sizeMaxX<tX:\r
474                 sizeMaxX=tX\r
475                 if tX==1920 and tY==1080:\r
476                     darx=16\r
477                     dary=9\r
478                 else:\r
479                     darx=tEX\r
480                     dary=tEY\r
481     os.remove(pin+".size.ts")\r
482     return [darx,dary]\r
483 def getParSize(pin,y):\r
484     tSize=getMovieBaseSize2(pin)\r
485     if tSize[1] != 0 :\r
486         tX=tSize[0]*10*y/tSize[1]\r
487         tY=y\r
488         if tX>int(tX/10)*10:\r
489             tX=tX/10+1\r
490         else:\r
491             tX=tX/10\r
492     else:\r
493         tX=-1\r
494         tY=-1\r
495     return [tX,tY]\r
496 def getX264CoreVersion():\r
497     x264=configreader.getConfPath("x264")\r
498     t1=commands.getoutput(x264+" --help|grep core")\r
499     rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")\r
500     rM=rT.match(t1)\r
501     v=-1\r
502     if rM:\r
503         v=int(rM.group(1))\r
504     return v\r
505 def getX264Commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=-1,bitrate=0):\r
506     x264=configreader.getConfPath("x264")\r
507     os.environ['LANG']="ja_JP.UTF-8"\r
508     x264_sar="--sar "+sar\r
509     x264preset=u"--preset "+preset\r
510     x264fps="--fps "+fps\r
511     if crf==-1:\r
512         x264bitrate=u"--bitrate "+str(bitrate)\r
513     else:\r
514         x264crf=u"--crf "+str(crf)\r
515     x264_addline=configreader.getConfEnv("x264_addline")\r
516     x264_thread="auto"\r
517     try:\r
518         xtt=configreader.getConfEnv("x264_thread")\r
519         xtt=int(xtt)\r
520         if xtt>0:\r
521             x264_thread=str(xtt)\r
522     except:\r
523         x264_thread="auto"\r
524     x264_addline=configreader.getConfEnv("x264_addline")\r
525     if getX264CoreVersion()>103:\r
526         x264res=u"--input-res "+x264streamsize\r
527         exe=u"nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --colormatrix bt709 --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" "+x264res+" -o \""+pout+"\" "+pin\r
528     else:\r
529         exe=u"nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --colormatrix bt709 --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" "+pin+" "+x264streamsize\r
530     return exe\r