OSDN Git Service

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