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 --keyint 30"\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 usebluray==1:\r
190             encvf = encvf + ",scale=-2:1080::0:3,expand=1920:1080"+harddup\r
191             x264streamsize=u"1920x1080"\r
192     if tsize[0] <= 0 or tsize[1] <= 0:\r
193         encvf="-sws 9 -vf yadif=0,pp=l5"\r
194         harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"\r
195         ofps="-ofps 30000/1001"\r
196         fps="-fps 30000/1001"\r
197         x264fps="30000/1001 --keyint 30"\r
198         tsize=[1280,720]\r
199         encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup\r
200         x264streamsize=u"1280x720"\r
201         x264_bitrate="2500"\r
202     if quality==1:\r
203         x264preset=u"ultrafast"\r
204     elif quality==2:\r
205         x264preset=u"veryfast"\r
206     elif quality==3:\r
207         x264preset=u"fast"\r
208     elif quality==4:\r
209         x264preset=u"medium"\r
210     elif quality==5:\r
211         x264preset=u"slow"\r
212     elif quality==6:\r
213         x264preset=u"slower"\r
214     else:\r
215         x264preset=u"medium"\r
216     if size == "WVGA_BASE" or size == "QVGA_BASE":\r
217         x264profile=" --level 32 --profile baseline "\r
218     else:\r
219         x264profile=" --level 41 --profile high "\r
220     if uselavf==1:\r
221         lavft="-demuxer lavf "\r
222     x264crf=str(crf)\r
223     os.environ['LANG']="ja_JP.UTF-8"\r
224     random.seed(pin)\r
225     random.jumpahead(10)\r
226     temptime=int(time.time())\r
227     temptime=temptime % 9697\r
228     random.jumpahead(temptime)\r
229     streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))\r
230     unicode(commands.getoutput(u"mkfifo "+streampath),'utf-8','ignore')\r
231     logmencoder=recdblist.getLogTitle(pin)+".mencoder.log"\r
232     logx264=recdblist.getLogTitle(pin)+".x264.log"\r
233     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
234     encexe=encexe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf,interlaced=x264interlaced,bluray=usebluray)\r
235     #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
236     encexe=u"nice -n 19 " +encexe+" 2>&1 | "+tee+" \""+logx264+"\""\r
237     try:\r
238         recdblist.addCommandSelfLog(pin, encexe)\r
239     except Exception, inst:\r
240         print type(inst)\r
241         print str(inst)\r
242         print traceback.print_exc(file=sys.stdout)\r
243     recdblist.printutf8(encexe)\r
244     txt=""\r
245     try:\r
246         txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8','ignore')\r
247     except:\r
248         ""\r
249     os.remove(streampath)\r
250     zip.addFile2FileZip(logmencoder, recdblist.getLogTitle(pin)+".log.zip")\r
251     zip.addFile2FileZip(logx264, recdblist.getLogTitle(pin)+".log.zip")\r
252     os.remove(logmencoder)\r
253     os.remove(logx264)\r
254     recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)\r
255 #    recdblist.addCommandLogTar(pin,u"Mencoder","mencoder", encexe,txt)\r
256 def encodeFfmpegSar(pin,pout,size,is24fps,quality,crf,deinterlace=1,usebluray=0,usesar=1):\r
257     """\r
258 \r
259     """\r
260     ffmpeg=configreader.getConfPath("ffmpeg")\r
261     tee=configreader.getConfPath("tee")\r
262     fps=u"-r 29.970030 "\r
263     x264fps="30000/1001 --keyint 30"\r
264     x264streamsize=""\r
265     x264preset=""\r
266     x264tune=""\r
267     x264_bitrate="2500"\r
268     x264_thread="auto"\r
269     x264interlaced=0\r
270     tsar=getMoviePAR2(pin)\r
271     filter="-deinterlace"\r
272     x264sar=str(tsar[0])+":"+str(tsar[1])\r
273     if is24fps==1:\r
274         fps=u"-r 23.976023 "\r
275         x264fps="24000/1001 --keyint 24"\r
276         x264tune="--tune animation"\r
277     if size == "HD":\r
278         tsize=getParSize(pin,720)\r
279         s = "-s "+str(tsize[0])+"x720 "\r
280         x264streamsize=str(tsize[0])+u"x720"\r
281         x264_bitrate="2500"\r
282     elif size == "WVGA":\r
283         tsize=getParSize(pin,480)\r
284         s = "-s "+str(tsize[0])+"x480 "\r
285         x264streamsize=str(tsize[0])+u"x480"\r
286         x264_bitrate="1500"\r
287     elif size == "FullHD":\r
288         tsize=getParSize(pin,1080)\r
289         s = "-s "+str(tsize[0])+"x1080 "\r
290         x264streamsize=str(tsize[0])+u"x1080"\r
291         x264_bitrate="5000"\r
292     elif size == "SD":\r
293         tsize=getParSize(pin,480)\r
294         s = "-s "+str(tsize[0])+"x480 "\r
295         x264streamsize=str(tsize[0])+u"x480"\r
296         x264_bitrate="1250"\r
297     elif size == "QVGA_BASE":\r
298         tsize=getParSize(pin,240)\r
299         s = "-s "+str(tsize[0])+"x240 "\r
300         x264streamsize=str(tsize[0])+u"x240"\r
301         x264_bitrate="300"\r
302     elif size == "WVGA_BASE":\r
303         tsize=getParSize(pin,480)\r
304         s = "-s "+str(tsize[0])+"x480 "\r
305         x264streamsize=str(tsize[0])+u"x480"\r
306         x264_bitrate="1500"\r
307     else:\r
308         tsize=getParSize(pin,720)\r
309         s = "-s "+str(tsize[0])+"x720 "\r
310         x264streamsize=str(tsize[0])+u"x720"\r
311         x264_bitrate="2500"\r
312     if deinterlace==0:\r
313         tsize=getMovieBaseSize2(pin)\r
314         fps=""\r
315         s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "\r
316         x264fps="30000/1001 --keyint 30"\r
317         x264interlaced=1\r
318         filter=""\r
319         x264streamsize=str(tsize[0])+u"x"+str(tsize[1])\r
320     if quality==1:\r
321         x264preset=u"ultrafast"\r
322     elif quality==2:\r
323         x264preset=u"veryfast"\r
324     elif quality==3:\r
325         x264preset=u"fast"\r
326     elif quality==4:\r
327         x264preset=u"medium"\r
328     elif quality==5:\r
329         x264preset=u"slow"\r
330     elif quality==6:\r
331         x264preset=u"slower"\r
332     else:\r
333         x264preset=u"medium"\r
334     if size == "WVGA_BASE" or size == "QVGA_BASE":\r
335         x264profile=" --level 32 --profile baseline "\r
336     else:\r
337         x264profile=" --level 41 --profile high "\r
338     x264crf=str(crf)\r
339     txt=""\r
340     os.environ['LANG']="ja_JP.UTF-8"\r
341     log_ffmpeg=recdblist.getLogTitle(pin)+".ffmpeg.log"\r
342     log_x264=recdblist.getLogTitle(pin)+".x264.log"\r
343     exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - 2> \""+log_ffmpeg+"\" | "\r
344     exe=exe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf,interlaced=x264interlaced,bluray=usebluray)\r
345     exe = "nice -n 19 " + exe+" 2>&1 | "+tee+" \""+log_x264+"\""\r
346     txt=""\r
347     #recdblist.addCommandSelfLog(log_ffmpeg,recdblist.getLogTitle(pin)+".log.zip")\r
348     #recdblist.addCommandSelfLog(log_x264,recdblist.getLogTitle(pin)+".log.zip")\r
349     recdblist.printutf8(exe)\r
350     recdblist.addCommandSelfLog(pin,exe)\r
351     try:\r
352         txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
353     except:\r
354         ""\r
355     recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)\r
356     zip.addFile2FileZip(log_ffmpeg, recdblist.getLogTitle(pin+".log.zip"))\r
357     zip.addFile2FileZip(log_x264, recdblist.getLogTitle(pin+".log.zip"))\r
358     os.remove(log_ffmpeg)\r
359     os.remove(log_x264)\r
360 def getMovieBaseSize(pin):\r
361     ffmpeg=configreader.getConfPath("ffmpeg")\r
362     os.environ['LANG']="ja_JP.UTF-8"\r
363     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"\r
364     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
365     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
366     sizeMaxX=0\r
367     sizeMaxY=0\r
368     txtls=txts.split("\n")\r
369     for t in txtls:\r
370         rM=rT.match(t)\r
371         if rM:\r
372             sizetxt=rM.group(1)\r
373             partxt=rM.group(2)\r
374             tX=int(sizetxt.split("x")[0])\r
375             tY=int(sizetxt.split("x")[1])\r
376             tEX=int(partxt.split(":")[0])\r
377             tEY=int(partxt.split(":")[1])\r
378             if sizeMaxX<tX:\r
379                 sizeMaxX=tX\r
380                 sizeMaxY=tY\r
381     return [sizeMaxX,sizeMaxY]\r
382 def getMovieBaseSize2(pin):##動画開始後すぐに解像度が変更されたときに対処\r
383     ffmpeg=configreader.getConfPath("ffmpeg")\r
384     os.environ['LANG']="ja_JP.UTF-8"\r
385     exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"\r
386     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
387     rT=re.compile(u"Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
388     sizeMaxX=0\r
389     sizeMaxY=0\r
390     txtls=txts.split("\n")\r
391     for t in txtls:\r
392         rM=rT.search(t)\r
393         if rM:\r
394             sizetxt=rM.group(1)\r
395             partxt=rM.group(2)\r
396             tX=int(sizetxt.split("x")[0])\r
397             tY=int(sizetxt.split("x")[1])\r
398             tEX=int(partxt.split(":")[0])\r
399             tEY=int(partxt.split(":")[1])\r
400             if sizeMaxX<tX:\r
401                 sizeMaxX=tX\r
402                 sizeMaxY=tY\r
403     os.remove(pin+".size.ts")\r
404     return [sizeMaxX,sizeMaxY]\r
405 def getMoviePAR(pin):\r
406     ffmpeg=configreader.getConfPath("ffmpeg")\r
407     os.environ['LANG']="ja_JP.UTF-8"\r
408     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"\r
409     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
410     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
411     sizeMaxX=0\r
412     parx=0\r
413     pary=0\r
414     txtls=txts.split("\n")\r
415     for t in txtls:\r
416         rM=rT.match(t)\r
417         if rM:\r
418             sizetxt=rM.group(1)\r
419             dartxt=rM.group(2)\r
420             tX=int(sizetxt.split("x")[0])\r
421             tY=int(sizetxt.split("x")[1])\r
422             tEX=int(dartxt.split(":")[0])\r
423             tEY=int(dartxt.split(":")[1])\r
424             if sizeMaxX<tX:\r
425                 sizeMaxX=tX\r
426                 if tX==1920 and tY==1080:\r
427                     parx=1\r
428                     pary=1\r
429                 else:\r
430                     parx=tEX\r
431                     pary=tEY\r
432     return [parx,pary]\r
433 def getMoviePAR2(pin):\r
434     ffmpeg=configreader.getConfPath("ffmpeg")\r
435     os.environ['LANG']="ja_JP.UTF-8"\r
436     exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"\r
437     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
438     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
439     sizeMaxX=0\r
440     parx=0\r
441     pary=0\r
442     txtls=txts.split("\n")\r
443     for t in txtls:\r
444         rM=rT.match(t)\r
445         if rM:\r
446             sizetxt=rM.group(1)\r
447             dartxt=rM.group(2)\r
448             tX=int(sizetxt.split("x")[0])\r
449             tY=int(sizetxt.split("x")[1])\r
450             tEX=int(dartxt.split(":")[0])\r
451             tEY=int(dartxt.split(":")[1])\r
452             if sizeMaxX<tX:\r
453                 sizeMaxX=tX\r
454                 if tX==1920 and tY==1080:\r
455                     parx=1\r
456                     pary=1\r
457                 else:\r
458                     parx=tEX\r
459                     pary=tEY\r
460     os.remove(pin+".size.ts")\r
461     return [parx,pary]\r
462 def getMovieDAR(pin):\r
463     ffmpeg=configreader.getConfPath("ffmpeg")\r
464     os.environ['LANG']="ja_JP.UTF-8"\r
465     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"\r
466     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
467     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")\r
468     sizeMaxX=0\r
469     darx=0\r
470     dary=0\r
471     txtls=txts.split("\n")\r
472     for t in txtls:\r
473         rM=rT.match(t)\r
474         if rM:\r
475             sizetxt=rM.group(1)\r
476             dartxt=rM.group(2)\r
477             tX=int(sizetxt.split("x")[0])\r
478             tY=int(sizetxt.split("x")[1])\r
479             tEX=int(dartxt.split(":")[0])\r
480             tEY=int(dartxt.split(":")[1])\r
481             if sizeMaxX<tX:\r
482                 sizeMaxX=tX\r
483                 if tX==1920 and tY==1080:\r
484                     darx=16\r
485                     dary=9\r
486                 else:\r
487                     darx=tEX\r
488                     dary=tEY\r
489     return [darx,dary]\r
490 def getMovieDAR2(pin):\r
491     ffmpeg=configreader.getConfPath("ffmpeg")\r
492     os.environ['LANG']="ja_JP.UTF-8"\r
493     exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"\r
494     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
495     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")\r
496     sizeMaxX=0\r
497     darx=0\r
498     dary=0\r
499     txtls=txts.split("\n")\r
500     for t in txtls:\r
501         rM=rT.match(t)\r
502         if rM:\r
503             sizetxt=rM.group(1)\r
504             dartxt=rM.group(2)\r
505             tX=int(sizetxt.split("x")[0])\r
506             tY=int(sizetxt.split("x")[1])\r
507             tEX=int(dartxt.split(":")[0])\r
508             tEY=int(dartxt.split(":")[1])\r
509             if sizeMaxX<tX:\r
510                 sizeMaxX=tX\r
511                 if tX==1920 and tY==1080:\r
512                     darx=16\r
513                     dary=9\r
514                 else:\r
515                     darx=tEX\r
516                     dary=tEY\r
517     os.remove(pin+".size.ts")\r
518     return [darx,dary]\r
519 def getParSize(pin,y):\r
520     tSize=getMovieBaseSize2(pin)\r
521     if tSize[1] != 0 :\r
522         tX=tSize[0]*10*y/tSize[1]\r
523         tY=y\r
524         if tX>int(tX/10)*10:\r
525             tX=tX/10+1\r
526         else:\r
527             tX=tX/10\r
528     else:\r
529         tX=-1\r
530         tY=-1\r
531     return [tX,tY]\r
532 def getX264CoreVersion():\r
533     x264=configreader.getConfPath("x264")\r
534     t1=commands.getoutput(x264+" --help|grep core")\r
535     rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")\r
536     rM=rT.match(t1)\r
537     v=-1\r
538     if rM:\r
539         v=int(rM.group(1))\r
540     return v\r
541 def getX264Commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=-1,bitrate=0,interlaced=0,bluray=0):\r
542     x264=configreader.getConfPath("x264")\r
543     os.environ['LANG']="ja_JP.UTF-8"\r
544     x264_sar="--sar "+sar\r
545     x264preset=u"--preset "+preset\r
546     x264fps="--fps "+fps\r
547     x264interlaced=""\r
548     x264bluray=""\r
549     x264_addline=configreader.getConfEnv("x264_addline")\r
550     if crf==-1:\r
551         x264bitrate=u"--bitrate "+str(bitrate)\r
552     else:\r
553         x264crf=u"--crf "+str(crf)\r
554     if interlaced==1:\r
555         x264interlaced="--tff --nal-hrd vbr"\r
556     if bluray==1:\r
557         x264bluray="--weightp 1 --nal-hrd vbr --bframes 3 --b-pyramid none --open-gop bluray --slices 4 --aud --colorprim bt709 --transfer bt709 "\r
558         x264_addline=""\r
559         if fps=="24000/1001 --keyint 24" or fps =="24000/1001":\r
560             if x264streamsize=="1280x720":\r
561                 x264bluray=u"--ref 6 "+x264bluray\r
562             if x264streamsize=="1920x1080":\r
563                 x264bluray=u"--ref 3 "+x264bluray\r
564         elif fps=="30000/1001 --keyint 30" or fps =="30000/1001":\r
565             if x264streamsize=="1280x720":\r
566                 x264bluray=u"--ref 6 --pulldown double "+x264bluray\r
567             if x264streamsize=="1920x1080":\r
568                 if interlaced==0:\r
569                     x264bluray=u"--ref 3 --fake-interlaced --pic-struct "+x264bluray\r
570                 else:\r
571                     x264bluray=u"--ref 3 "+x264bluray\r
572     x264_thread="auto"\r
573     try:\r
574         xtt=configreader.getConfEnv("x264_thread")\r
575         xtt=int(xtt)\r
576         if xtt>0:\r
577             x264_thread=str(xtt)\r
578     except:\r
579         x264_thread="auto"\r
580     x264_addline=configreader.getConfEnv("x264_addline")\r
581     if getX264CoreVersion()>103:\r
582         x264res=u"--input-res "+x264streamsize\r
583         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
584     else:\r
585         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
586     return exe\r