OSDN Git Service

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