OSDN Git Service

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