OSDN Git Service

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