OSDN Git Service

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