OSDN Git Service

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