OSDN Git Service

change logo option.
[rec10/rec10-git.git] / rec10 / trunk / src / ts2x264.py
1 #!/usr/bin/python\r
2 # coding: UTF-8\r
3 # Rec10 TS Recording Tools\r
4 # Copyright (C) 2009-2011 Yukikaze\r
5 import commands\r
6 import configreader\r
7 import os\r
8 import os.path\r
9 import re\r
10 import random\r
11 import time\r
12 import traceback\r
13 import zip\r
14 \r
15 import recdblist\r
16 def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"\r
17     """\r
18     pinで指定されたファイルをpoutにx264でエンコードして書き出す\r
19     """\r
20     dualpass = 0\r
21     is24fps=0\r
22     size="HD"\r
23     crf=18\r
24     quality=4\r
25     quality=int(configreader.getConfEnv("x264_preset"))\r
26     crf=int(configreader.getConfEnv("crf"))\r
27     deinterlace=1\r
28     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     else:\r
74         try:\r
75             encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=1)\r
76             if os.path.exists(pout):\r
77                 if not os.path.getsize(pout)>1*1000:\r
78                     recdblist.addCommonlogEX("Warning", "Old mencoder option used.","","",log_level=200)\r
79                     try:\r
80                         encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=0)\r
81                     except Exception, inst:\r
82                         recdblist.addCommonlogEX("Error", "ts2x264_mencoder_old(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)\r
83             else:\r
84                 recdblist.addCommonlogEX("Warning", "Old mencoder option used.","","",log_level=200)\r
85                 try:\r
86                     encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=0)\r
87                 except Exception, inst:\r
88                     recdblist.addCommonlogEX("Error", "ts2x264_mencoder_old(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)\r
89         except Exception, inst:\r
90             recdblist.addCommonlogEX("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)\r
91 def encodeMencoderSar(pin,pout,size,is24fps,quality,crf,deinterlace=1,uselavf=0):\r
92     mencoder=configreader.getConfPath("mencoder")\r
93     tee=configreader.getConfPath("tee")\r
94     encvf=""\r
95     txt=""\r
96     encvf="-sws 9 -vf yadif=0,pp=l5"\r
97     harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"\r
98     ofps="-ofps 30000/1001"\r
99     fps="-fps 30000/1001"\r
100     x264fps="30000/1001"\r
101     x264streamsize=""\r
102     x264preset=""\r
103     x264tune=""\r
104     x264_bitrate="5000"\r
105     tsar=getMoviePAR2(pin)\r
106     x264sar=str(tsar[0])+":"+str(tsar[1])\r
107     lavft=""\r
108     if is24fps==1:\r
109         ofps="-ofps 24000/1001"\r
110         fps="-fps 30000/1001"\r
111         x264fps="24000/1001"\r
112         x264tune="--tune animation"\r
113         encvf="-sws 9 -vf pullup,softskip"\r
114         harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"\r
115     if size == "HD":\r
116         tsize=getParSize(pin,720)\r
117         encvf = encvf + ",scale=-3:720::0:3"+harddup\r
118         x264streamsize=str(tsize[0])+u"x720"\r
119         x264_bitrate="2500"\r
120     elif size == "WVGA":\r
121         tsize=getParSize(pin,480)\r
122         encvf = encvf + ",scale=-3:480::0:3"+harddup\r
123         x264streamsize=str(tsize[0])+u"x480"\r
124         x264_bitrate="1500"\r
125     elif size == "FullHD":\r
126         tsize=getParSize(pin,1080)\r
127         encvf = encvf + ",scale=-3:1080::0:3"+harddup\r
128         x264streamsize=str(tsize[0])+u"x1080"\r
129         x264_bitrate="5000"\r
130     elif size == "QVGA_BASE":\r
131         tsize=getParSize(pin,240)\r
132         encvf = encvf + ",scale=-3:240::0:3"+harddup\r
133         x264streamsize=str(tsize[0])+u"x240"\r
134         x264_bitrate="300"\r
135     elif size == "WVGA_BASE":\r
136         tsize=getParSize(pin,480)\r
137         encvf = encvf + ",scale=-3:480::0:3"+harddup\r
138         x264streamsize=str(tsize[0])+u"x480"\r
139         x264_bitrate="1500"\r
140     else:\r
141         tsize=getParSize(pin,720)\r
142         encvf = encvf + ",scale=-3:720::0:3"+harddup\r
143         x264streamsize=str(tsize[0])+u"x720"\r
144         x264_bitrate="2500"\r
145     if deinterlace==0:\r
146         tsize=getMovieBaseSize2(pin)\r
147         ofps="-ofps 30000/1001"\r
148         #fps="-fps 30000/1001"\r
149         fps=""\r
150         x264fps="30000/1001"\r
151         x264tune=x264tune+" --tff --nal-hrd vbr"\r
152         encvf="-vf hqdn3d=2:1:2"\r
153         harddup=",harddup"\r
154         encvf=encvf+harddup\r
155         x264streamsize=str(tsize[0])+u"x"+str(tsize[1])\r
156     if tsize[0] <= 0 or tsize[1] <= 0:\r
157         encvf="-sws 9 -vf yadif=0,pp=l5"\r
158         harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"\r
159         ofps="-ofps 30000/1001"\r
160         fps="-fps 30000/1001"\r
161         x264fps="30000/1001"\r
162         tsize=[1280,720]\r
163         encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup\r
164         x264streamsize=u"1280x720"\r
165         x264_bitrate="2500"\r
166     if quality==1:\r
167         x264preset=u"ultrafast"\r
168     elif quality==2:\r
169         x264preset=u"veryfast"\r
170     elif quality==3:\r
171         x264preset=u"fast"\r
172     elif quality==4:\r
173         x264preset=u"medium"\r
174     elif quality==5:\r
175         x264preset=u"slow"\r
176     elif quality==6:\r
177         x264preset=u"slower"\r
178     else:\r
179         x264preset=u"medium"\r
180     if size == "WVGA_BASE" or size == "QVGA_BASE":\r
181         x264profile=" --level 32 --profile baseline "\r
182     else:\r
183         x264profile=" --level 41 --profile high "\r
184     if uselavf==1:\r
185         lavft="-demuxer lavf "\r
186     x264crf=str(crf)\r
187     os.environ['LANG']="ja_JP.UTF-8"\r
188     random.seed(pin)\r
189     random.jumpahead(10)\r
190     temptime=int(time.time())\r
191     temptime=temptime % 9697\r
192     random.jumpahead(temptime)\r
193     streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))\r
194     unicode(commands.getoutput(u"mkfifo "+streampath),'utf-8','ignore')\r
195     logmencoder=recdblist.getLogTitle(pin)+".mencoder.log"\r
196     logx264=recdblist.getLogTitle(pin)+".x264.log"\r
197     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
198     encexe=encexe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf)\r
199     #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
200     encexe=u"nice -n 19 " +encexe+" 2>&1 | "+tee+" \""+logx264+"\""\r
201     try:\r
202         recdblist.addCommandSelfLog(pin, encexe)\r
203     except Exception, inst:\r
204         print type(inst)\r
205         print str(inst)\r
206         print traceback.print_exc(file=sys.stdout)\r
207     recdblist.printutf8(encexe)\r
208     txt=""\r
209     try:\r
210         txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8','ignore')\r
211     except:\r
212         ""\r
213     os.remove(streampath)\r
214     zip.addFile2FileZip(logmencoder, recdblist.getLogTitle(pin)+".log.zip")\r
215     zip.addFile2FileZip(logx264, recdblist.getLogTitle(pin)+".log.zip")\r
216     os.remove(logmencoder)\r
217     os.remove(logx264)\r
218     recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)\r
219 #    recdblist.addCommandLogTar(pin,u"Mencoder","mencoder", encexe,txt)\r
220 def encodeFfmpegSar(pin,pout,size,is24fps,quality,crf,deinterlace=1):\r
221     """\r
222 \r
223     """\r
224     ffmpeg=configreader.getConfPath("ffmpeg")\r
225     tee=configreader.getConfPath("tee")\r
226     fps=u"-r 29.970030 "\r
227     x264fps="30000/1001"\r
228     x264streamsize=""\r
229     x264preset=""\r
230     x264tune=""\r
231     x264_bitrate="2500"\r
232     x264_thread="auto"\r
233     tsar=getMoviePAR2(pin)\r
234     filter="-deinterlace"\r
235     x264sar=str(tsar[0])+":"+str(tsar[1])\r
236     if is24fps==1:\r
237         fps=u"-r 23.976023 "\r
238         x264fps="24000/1001"\r
239         x264tune="--tune animation"\r
240     if size == "HD":\r
241         tsize=getParSize(pin,720)\r
242         s = "-s "+str(tsize[0])+"x720 "\r
243         x264streamsize=str(tsize[0])+u"x720"\r
244         x264_bitrate="2500"\r
245     elif size == "WVGA":\r
246         tsize=getParSize(pin,480)\r
247         s = "-s "+str(tsize[0])+"x480 "\r
248         x264streamsize=str(tsize[0])+u"x480"\r
249         x264_bitrate="1500"\r
250     elif size == "FullHD":\r
251         tsize=getParSize(pin,1080)\r
252         s = "-s "+str(tsize[0])+"x1080 "\r
253         x264streamsize=str(tsize[0])+u"x1080"\r
254         x264_bitrate="5000"\r
255     elif size == "SD":\r
256         tsize=getParSize(pin,480)\r
257         s = "-s "+str(tsize[0])+"x480 "\r
258         x264streamsize=str(tsize[0])+u"x480"\r
259         x264_bitrate="1250"\r
260     elif size == "QVGA_BASE":\r
261         tsize=getParSize(pin,240)\r
262         s = "-s "+str(tsize[0])+"x240 "\r
263         x264streamsize=str(tsize[0])+u"x240"\r
264         x264_bitrate="300"\r
265     elif size == "WVGA_BASE":\r
266         tsize=getParSize(pin,480)\r
267         s = "-s "+str(tsize[0])+"x480 "\r
268         x264streamsize=str(tsize[0])+u"x480"\r
269         x264_bitrate="1500"\r
270     else:\r
271         tsize=getParSize(pin,720)\r
272         s = "-s "+str(tsize[0])+"x720 "\r
273         x264streamsize=str(tsize[0])+u"x720"\r
274         x264_bitrate="2500"\r
275     if deinterlace==0:\r
276         tsize=getMovieBaseSize2(pin)\r
277         fps=""\r
278         s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "\r
279         x264fps="30000/1001"\r
280         x264tune=x264tune+" --tff --nal-hrd vbr"\r
281         filter=""\r
282         x264streamsize=str(tsize[0])+u"x"+str(tsize[1])\r
283     if quality==1:\r
284         x264preset=u"ultrafast"\r
285     elif quality==2:\r
286         x264preset=u"veryfast"\r
287     elif quality==3:\r
288         x264preset=u"fast"\r
289     elif quality==4:\r
290         x264preset=u"medium"\r
291     elif quality==5:\r
292         x264preset=u"slow"\r
293     elif quality==6:\r
294         x264preset=u"slower"\r
295     else:\r
296         x264preset=u"medium"\r
297     if size == "WVGA_BASE" or size == "QVGA_BASE":\r
298         x264profile=" --level 32 --profile baseline "\r
299     else:\r
300         x264profile=" --level 41 --profile high "\r
301     x264crf=str(crf)\r
302     txt=""\r
303     os.environ['LANG']="ja_JP.UTF-8"\r
304     log_ffmpeg=recdblist.getLogTitle(pin)+".ffmpeg.log"\r
305     log_x264=recdblist.getLogTitle(pin)+".x264.log"\r
306     exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - 2> \""+log_ffmpeg+"\" | "\r
307     exe=exe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)\r
308     exe = "nice -n 19 " + exe+" 2>&1 | "+tee+" \""+log_x264+"\""\r
309     txt=""\r
310     #recdblist.addCommandSelfLog(log_ffmpeg,recdblist.getLogTitle(pin)+".log.zip")\r
311     #recdblist.addCommandSelfLog(log_x264,recdblist.getLogTitle(pin)+".log.zip")\r
312     recdblist.printutf8(exe)\r
313     recdblist.addCommandSelfLog(pin,exe)\r
314     try:\r
315         txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
316     except:\r
317         ""\r
318     recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)\r
319     zip.addFile2FileZip(log_ffmpeg, recdblist.getLogTitle(pin+".log.zip"))\r
320     zip.addFile2FileZip(log_x264, recdblist.getLogTitle(pin+".log.zip"))\r
321     os.remove(log_ffmpeg)\r
322     os.remove(log_x264)\r
323 def getMovieBaseSize(pin):\r
324     ffmpeg=configreader.getConfPath("ffmpeg")\r
325     os.environ['LANG']="ja_JP.UTF-8"\r
326     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"\r
327     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
328     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
329     sizeMaxX=0\r
330     sizeMaxY=0\r
331     txtls=txts.split("\n")\r
332     for t in txtls:\r
333         rM=rT.match(t)\r
334         if rM:\r
335             sizetxt=rM.group(1)\r
336             partxt=rM.group(2)\r
337             tX=int(sizetxt.split("x")[0])\r
338             tY=int(sizetxt.split("x")[1])\r
339             tEX=int(partxt.split(":")[0])\r
340             tEY=int(partxt.split(":")[1])\r
341             if sizeMaxX<tX:\r
342                 sizeMaxX=tX\r
343                 sizeMaxY=tY\r
344     return [sizeMaxX,sizeMaxY]\r
345 def getMovieBaseSize2(pin):##動画開始後すぐに解像度が変更されたときに対処\r
346     ffmpeg=configreader.getConfPath("ffmpeg")\r
347     os.environ['LANG']="ja_JP.UTF-8"\r
348     exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"\r
349     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
350     rT=re.compile(u"Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
351     sizeMaxX=0\r
352     sizeMaxY=0\r
353     txtls=txts.split("\n")\r
354     for t in txtls:\r
355         rM=rT.search(t)\r
356         if rM:\r
357             sizetxt=rM.group(1)\r
358             partxt=rM.group(2)\r
359             tX=int(sizetxt.split("x")[0])\r
360             tY=int(sizetxt.split("x")[1])\r
361             tEX=int(partxt.split(":")[0])\r
362             tEY=int(partxt.split(":")[1])\r
363             if sizeMaxX<tX:\r
364                 sizeMaxX=tX\r
365                 sizeMaxY=tY\r
366     os.remove(pin+".size.ts")\r
367     return [sizeMaxX,sizeMaxY]\r
368 def getMoviePAR(pin):\r
369     ffmpeg=configreader.getConfPath("ffmpeg")\r
370     os.environ['LANG']="ja_JP.UTF-8"\r
371     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"\r
372     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
373     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
374     sizeMaxX=0\r
375     parx=0\r
376     pary=0\r
377     txtls=txts.split("\n")\r
378     for t in txtls:\r
379         rM=rT.match(t)\r
380         if rM:\r
381             sizetxt=rM.group(1)\r
382             dartxt=rM.group(2)\r
383             tX=int(sizetxt.split("x")[0])\r
384             tY=int(sizetxt.split("x")[1])\r
385             tEX=int(dartxt.split(":")[0])\r
386             tEY=int(dartxt.split(":")[1])\r
387             if sizeMaxX<tX:\r
388                 sizeMaxX=tX\r
389                 if tX==1920 and tY==1080:\r
390                     parx=1\r
391                     pary=1\r
392                 else:\r
393                     parx=tEX\r
394                     pary=tEY\r
395     return [parx,pary]\r
396 def getMoviePAR2(pin):\r
397     ffmpeg=configreader.getConfPath("ffmpeg")\r
398     os.environ['LANG']="ja_JP.UTF-8"\r
399     exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"\r
400     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
401     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
402     sizeMaxX=0\r
403     parx=0\r
404     pary=0\r
405     txtls=txts.split("\n")\r
406     for t in txtls:\r
407         rM=rT.match(t)\r
408         if rM:\r
409             sizetxt=rM.group(1)\r
410             dartxt=rM.group(2)\r
411             tX=int(sizetxt.split("x")[0])\r
412             tY=int(sizetxt.split("x")[1])\r
413             tEX=int(dartxt.split(":")[0])\r
414             tEY=int(dartxt.split(":")[1])\r
415             if sizeMaxX<tX:\r
416                 sizeMaxX=tX\r
417                 if tX==1920 and tY==1080:\r
418                     parx=1\r
419                     pary=1\r
420                 else:\r
421                     parx=tEX\r
422                     pary=tEY\r
423     os.remove(pin+".size.ts")\r
424     return [parx,pary]\r
425 def getMovieDAR(pin):\r
426     ffmpeg=configreader.getConfPath("ffmpeg")\r
427     os.environ['LANG']="ja_JP.UTF-8"\r
428     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"\r
429     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
430     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")\r
431     sizeMaxX=0\r
432     darx=0\r
433     dary=0\r
434     txtls=txts.split("\n")\r
435     for t in txtls:\r
436         rM=rT.match(t)\r
437         if rM:\r
438             sizetxt=rM.group(1)\r
439             dartxt=rM.group(2)\r
440             tX=int(sizetxt.split("x")[0])\r
441             tY=int(sizetxt.split("x")[1])\r
442             tEX=int(dartxt.split(":")[0])\r
443             tEY=int(dartxt.split(":")[1])\r
444             if sizeMaxX<tX:\r
445                 sizeMaxX=tX\r
446                 if tX==1920 and tY==1080:\r
447                     darx=16\r
448                     dary=9\r
449                 else:\r
450                     darx=tEX\r
451                     dary=tEY\r
452     return [darx,dary]\r
453 def getMovieDAR2(pin):\r
454     ffmpeg=configreader.getConfPath("ffmpeg")\r
455     os.environ['LANG']="ja_JP.UTF-8"\r
456     exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"\r
457     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
458     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")\r
459     sizeMaxX=0\r
460     darx=0\r
461     dary=0\r
462     txtls=txts.split("\n")\r
463     for t in txtls:\r
464         rM=rT.match(t)\r
465         if rM:\r
466             sizetxt=rM.group(1)\r
467             dartxt=rM.group(2)\r
468             tX=int(sizetxt.split("x")[0])\r
469             tY=int(sizetxt.split("x")[1])\r
470             tEX=int(dartxt.split(":")[0])\r
471             tEY=int(dartxt.split(":")[1])\r
472             if sizeMaxX<tX:\r
473                 sizeMaxX=tX\r
474                 if tX==1920 and tY==1080:\r
475                     darx=16\r
476                     dary=9\r
477                 else:\r
478                     darx=tEX\r
479                     dary=tEY\r
480     os.remove(pin+".size.ts")\r
481     return [darx,dary]\r
482 def getParSize(pin,y):\r
483     tSize=getMovieBaseSize2(pin)\r
484     if tSize[1] != 0 :\r
485         tX=tSize[0]*10*y/tSize[1]\r
486         tY=y\r
487         if tX>int(tX/10)*10:\r
488             tX=tX/10+1\r
489         else:\r
490             tX=tX/10\r
491     else:\r
492         tX=-1\r
493         tY=-1\r
494     return [tX,tY]\r
495 def getX264CoreVersion():\r
496     x264=configreader.getConfPath("x264")\r
497     t1=commands.getoutput(x264+" --help|grep core")\r
498     rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")\r
499     rM=rT.match(t1)\r
500     v=-1\r
501     if rM:\r
502         v=int(rM.group(1))\r
503     return v\r
504 def getX264Commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=-1,bitrate=0):\r
505     x264=configreader.getConfPath("x264")\r
506     os.environ['LANG']="ja_JP.UTF-8"\r
507     x264_sar="--sar "+sar\r
508     x264preset=u"--preset "+preset\r
509     x264fps="--fps "+fps\r
510     if crf==-1:\r
511         x264bitrate=u"--bitrate "+str(bitrate)\r
512     else:\r
513         x264crf=u"--crf "+str(crf)\r
514     x264_addline=configreader.getConfEnv("x264_addline")\r
515     x264_thread="auto"\r
516     try:\r
517         xtt=configreader.getConfEnv("x264_thread")\r
518         xtt=int(xtt)\r
519         if xtt>0:\r
520             x264_thread=str(xtt)\r
521     except:\r
522         x264_thread="auto"\r
523     x264_addline=configreader.getConfEnv("x264_addline")\r
524     if getX264CoreVersion()>103:\r
525         x264res=u"--input-res "+x264streamsize\r
526         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
527     else:\r
528         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
529     return exe\r