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