OSDN Git Service

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