OSDN Git Service

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