OSDN Git Service

fix rename error.
[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     if size == "WVGA_BASE" or size == "QVGA_BASE":\r
161         x264profile=" --level 32 --profile baseline "\r
162     else:\r
163         x264profile=" --level 41 --profile high "\r
164     x264crf=str(crf)\r
165     os.environ['LANG']="ja_JP.UTF-8"\r
166     random.seed(pin)\r
167     random.jumpahead(10)\r
168     temptime=int(time.time())\r
169     temptime=temptime % 9697\r
170     random.jumpahead(temptime)\r
171     streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))\r
172     os.system(u"mkfifo "+streampath)\r
173     encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" & "\r
174     encexe=encexe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf)\r
175     #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
176     encexe=u"nice -n 19 " +encexe\r
177     recdblist.printutf8(encexe)\r
178     txt=""\r
179     try:\r
180         txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8','ignore')\r
181     except:\r
182         ""\r
183     os.system("rm "+streampath)\r
184     recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)\r
185 def encodeFfmpegSar(pin,pout,size,is24fps,quality,crf,deinterlace=1):\r
186     """\r
187 \r
188     """\r
189     ffmpeg=configreader.getConfPath("ffmpeg")\r
190     fps=u"-r 29.970030 "\r
191     x264fps="30000/1001"\r
192     x264streamsize=""\r
193     x264preset=""\r
194     x264tune=""\r
195     x264_bitrate="2500"\r
196     x264_thread="auto"\r
197     tsar=getMoviePAR2(pin)\r
198     filter="-deinterlace"\r
199     x264sar=str(tsar[0])+":"+str(tsar[1])\r
200     if size == "HD":\r
201         tsize=getParSize(pin,720)\r
202         s = "-s "+str(tsize[0])+"x720 "\r
203         x264streamsize=str(tsize[0])+u"x720"\r
204         x264_bitrate="2500"\r
205     elif size == "WVGA":\r
206         tsize=getParSize(pin,480)\r
207         s = "-s "+str(tsize[0])+"x480 "\r
208         x264streamsize=str(tsize[0])+u"x480"\r
209         x264_bitrate="1500"\r
210     elif size == "FullHD":\r
211         tsize=getParSize(pin,1080)\r
212         s = "-s "+str(tsize[0])+"x1080 "\r
213         x264streamsize=str(tsize[0])+u"x1080"\r
214         x264_bitrate="5000"\r
215     elif size == "SD":\r
216         tsize=getParSize(pin,480)\r
217         s = "-s "+str(tsize[0])+"x480 "\r
218         x264streamsize=str(tsize[0])+u"x480"\r
219         x264_bitrate="1250"\r
220     elif size == "QVGA_BASE":\r
221         tsize=getParSize(pin,240)\r
222         s = "-s "+str(tsize[0])+"x240 "\r
223         x264streamsize=str(tsize[0])+u"x240"\r
224         x264_bitrate="300"\r
225     elif size == "WVGA_BASE":\r
226         tsize=getParSize(pin,480)\r
227         s = "-s "+str(tsize[0])+"x480 "\r
228         x264streamsize=str(tsize[0])+u"x480"\r
229         x264_bitrate="1500"\r
230     else:\r
231         tsize=getParSize(pin,720)\r
232         s = "-s "+str(tsize[0])+"x720 "\r
233         x264streamsize=str(tsize[0])+u"x720"\r
234         x264_bitrate="2500"\r
235     if deinterlace==0:\r
236         tsize=getMovieBaseSize2(pin)\r
237         fps=""\r
238         s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "\r
239         x264fps="30000/1001"\r
240         x264tune=x264tune+" --tff --nal-hrd vbr"\r
241         filter=""\r
242         x264streamsize=str(tsize[0])+u"x"+str(tsize[1])\r
243     if quality==1:\r
244         x264preset=u"ultrafast"\r
245     elif quality==2:\r
246         x264preset=u"veryfast"\r
247     elif quality==3:\r
248         x264preset=u"fast"\r
249     elif quality==4:\r
250         x264preset=u"medium"\r
251     elif quality==5:\r
252         x264preset=u"slow"\r
253     elif quality==6:\r
254         x264preset=u"slower"\r
255     if size == "WVGA_BASE" or size == "QVGA_BASE":\r
256         x264profile=" --level 32 --profile baseline "\r
257     else:\r
258         x264profile=" --level 41 --profile high "\r
259     x264crf=str(crf)\r
260     txt=""\r
261     os.environ['LANG']="ja_JP.UTF-8"\r
262     exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - | "\r
263     exe=exe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)\r
264     exe = "nice -n 19 " + exe\r
265     txt=""\r
266     recdblist.printutf8(exe)\r
267     try:\r
268         txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
269     except:\r
270         ""\r
271     recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)\r
272 def getMovieBaseSize(pin):\r
273     ffmpeg=configreader.getConfPath("ffmpeg")\r
274     os.environ['LANG']="ja_JP.UTF-8"\r
275     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"\r
276     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
277     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
278     sizeMaxX=0\r
279     sizeMaxY=0\r
280     txtls=txts.split("\n")\r
281     for t in txtls:\r
282         rM=rT.match(t)\r
283         if rM:\r
284             sizetxt=rM.group(1)\r
285             partxt=rM.group(2)\r
286             tX=int(sizetxt.split("x")[0])\r
287             tY=int(sizetxt.split("x")[1])\r
288             tEX=int(partxt.split(":")[0])\r
289             tEY=int(partxt.split(":")[1])\r
290             if sizeMaxX<tX:\r
291                 sizeMaxX=tX\r
292                 sizeMaxY=tY\r
293     return [sizeMaxX,sizeMaxY]\r
294 def getMovieBaseSize2(pin):##動画開始後すぐに解像度が変更されたときに対処\r
295     ffmpeg=configreader.getConfPath("ffmpeg")\r
296     os.environ['LANG']="ja_JP.UTF-8"\r
297     exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"\r
298     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
299     rT=re.compile(u"Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
300     sizeMaxX=0\r
301     sizeMaxY=0\r
302     txtls=txts.split("\n")\r
303     for t in txtls:\r
304         rM=rT.search(t)\r
305         if rM:\r
306             sizetxt=rM.group(1)\r
307             partxt=rM.group(2)\r
308             tX=int(sizetxt.split("x")[0])\r
309             tY=int(sizetxt.split("x")[1])\r
310             tEX=int(partxt.split(":")[0])\r
311             tEY=int(partxt.split(":")[1])\r
312             if sizeMaxX<tX:\r
313                 sizeMaxX=tX\r
314                 sizeMaxY=tY\r
315     os.remove(pin+".size.ts")\r
316     return [sizeMaxX,sizeMaxY]\r
317 def getMoviePAR(pin):\r
318     ffmpeg=configreader.getConfPath("ffmpeg")\r
319     os.environ['LANG']="ja_JP.UTF-8"\r
320     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"\r
321     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
322     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
323     sizeMaxX=0\r
324     parx=0\r
325     pary=0\r
326     txtls=txts.split("\n")\r
327     for t in txtls:\r
328         rM=rT.match(t)\r
329         if rM:\r
330             sizetxt=rM.group(1)\r
331             dartxt=rM.group(2)\r
332             tX=int(sizetxt.split("x")[0])\r
333             tY=int(sizetxt.split("x")[1])\r
334             tEX=int(dartxt.split(":")[0])\r
335             tEY=int(dartxt.split(":")[1])\r
336             if sizeMaxX<tX:\r
337                 sizeMaxX=tX\r
338                 if tX==1920 and tY==1080:\r
339                     parx=1\r
340                     pary=1\r
341                 else:\r
342                     parx=tEX\r
343                     pary=tEY\r
344     return [parx,pary]\r
345 def getMoviePAR2(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     parx=0\r
353     pary=0\r
354     txtls=txts.split("\n")\r
355     for t in txtls:\r
356         rM=rT.match(t)\r
357         if rM:\r
358             sizetxt=rM.group(1)\r
359             dartxt=rM.group(2)\r
360             tX=int(sizetxt.split("x")[0])\r
361             tY=int(sizetxt.split("x")[1])\r
362             tEX=int(dartxt.split(":")[0])\r
363             tEY=int(dartxt.split(":")[1])\r
364             if sizeMaxX<tX:\r
365                 sizeMaxX=tX\r
366                 if tX==1920 and tY==1080:\r
367                     parx=1\r
368                     pary=1\r
369                 else:\r
370                     parx=tEX\r
371                     pary=tEY\r
372     os.remove(pin+".size.ts")\r
373     return [parx,pary]\r
374 def getMovieDAR(pin):\r
375     ffmpeg=configreader.getConfPath("ffmpeg")\r
376     os.environ['LANG']="ja_JP.UTF-8"\r
377     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"\r
378     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
379     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")\r
380     sizeMaxX=0\r
381     darx=0\r
382     dary=0\r
383     txtls=txts.split("\n")\r
384     for t in txtls:\r
385         rM=rT.match(t)\r
386         if rM:\r
387             sizetxt=rM.group(1)\r
388             dartxt=rM.group(2)\r
389             tX=int(sizetxt.split("x")[0])\r
390             tY=int(sizetxt.split("x")[1])\r
391             tEX=int(dartxt.split(":")[0])\r
392             tEY=int(dartxt.split(":")[1])\r
393             if sizeMaxX<tX:\r
394                 sizeMaxX=tX\r
395                 if tX==1920 and tY==1080:\r
396                     darx=16\r
397                     dary=9\r
398                 else:\r
399                     darx=tEX\r
400                     dary=tEY\r
401     return [darx,dary]\r
402 def getMovieDAR2(pin):\r
403     ffmpeg=configreader.getConfPath("ffmpeg")\r
404     os.environ['LANG']="ja_JP.UTF-8"\r
405     exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"\r
406     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
407     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")\r
408     sizeMaxX=0\r
409     darx=0\r
410     dary=0\r
411     txtls=txts.split("\n")\r
412     for t in txtls:\r
413         rM=rT.match(t)\r
414         if rM:\r
415             sizetxt=rM.group(1)\r
416             dartxt=rM.group(2)\r
417             tX=int(sizetxt.split("x")[0])\r
418             tY=int(sizetxt.split("x")[1])\r
419             tEX=int(dartxt.split(":")[0])\r
420             tEY=int(dartxt.split(":")[1])\r
421             if sizeMaxX<tX:\r
422                 sizeMaxX=tX\r
423                 if tX==1920 and tY==1080:\r
424                     darx=16\r
425                     dary=9\r
426                 else:\r
427                     darx=tEX\r
428                     dary=tEY\r
429     os.remove(pin+".size.ts")\r
430     return [darx,dary]\r
431 def getParSize(pin,y):\r
432     tSize=getMovieBaseSize2(pin)\r
433     if tSize[1] != 0 :\r
434         tX=tSize[0]*10*y/tSize[1]\r
435         tY=y\r
436         if tX>int(tX/10)*10:\r
437             tX=tX/10+1\r
438         else:\r
439             tX=tX/10\r
440     else:\r
441         tX=-1\r
442         tY=-1\r
443     return [tX,tY]\r
444 def getX264CoreVersion():\r
445     x264=configreader.getConfPath("x264")\r
446     t1=commands.getoutput(x264+" --help|grep core")\r
447     rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")\r
448     rM=rT.match(t1)\r
449     v=-1\r
450     if rM:\r
451         v=int(rM.group(1))\r
452     return v\r
453 def getX264Commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=-1,bitrate=0):\r
454     x264=configreader.getConfPath("x264")\r
455     os.environ['LANG']="ja_JP.UTF-8"\r
456     x264_sar="--sar "+sar\r
457     x264preset=u"--preset "+preset\r
458     x264fps="--fps "+fps\r
459     if crf==-1:\r
460         x264bitrate=u"--bitrate "+str(bitrate)\r
461     else:\r
462         x264crf=u"--crf "+str(crf)\r
463     x264_addline=configreader.getConfEnv("x264_addline")\r
464     x264_thread="auto"\r
465     try:\r
466         xtt=configreader.getConfEnv("x264_thread")\r
467         xtt=int(xtt)\r
468         if xtt>0:\r
469             x264_thread=str(xtt)\r
470     except:\r
471         x264_thread="auto"\r
472     x264_addline=configreader.getConfEnv("x264_addline")\r
473     if getX264CoreVersion()>103:\r
474         x264res=u"--input-res "+x264streamsize\r
475         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
476     else:\r
477         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
478     return exe\r