OSDN Git Service

implement videoframe compliment option.
[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("MW8", opts):
36         size = "QVGA_BASE"
37         crf=crf+4
38     if re.search("MW9", 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         encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace)
67     elif re.search("5",opts):#5.1chの場合
68         encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace)
69     elif re.search("b",opts):#m2vテスト
70         try:
71             tm2v=pin.replace(".ts",".m2v")
72             encode_sar(tm2v, 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     elif re.search("x",opts):#videoframe補完テスト
76         tm2v=pin.replace(".ts",".m2v")
77         encode_ffmpeg_sar(tm2v,pout,size,is24fps,quality,crf,deinterlace)
78     else:
79         try:
80             encode_sar(pin, pout,size,is24fps,quality,crf,deinterlace)
81         except Exception, inst:
82             recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
83 def encode_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
84     mencoder=configreader.getpath("mencoder")
85     encvf=""
86     txt=""
87     encvf="-sws 9 -vf yadif=0,pp=l5"
88     harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
89     ofps="-ofps 30000/1001"
90     fps="-fps 30000/1001"
91     x264fps="30000/1001"
92     x264streamsize=""
93     x264preset=""
94     x264tune=""
95     x264_bitrate="5000"
96     tsar=getMoviePAR(pin)
97     x264sar=str(tsar[0])+":"+str(tsar[1])
98     if is24fps==1:
99         ofps="-ofps 24000/1001"
100         fps="-fps 30000/1001"
101         x264fps="24000/1001"
102         x264tune="--tune animation"
103         encvf="-sws 9 -vf pullup,softskip"
104         harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
105     if size == "HD":
106         tsize=get_par_size(pin,720)
107         encvf = encvf + ",scale=-3:720::0:3"+harddup
108         x264streamsize=str(tsize[0])+u"x720"
109         x264_bitrate="2500"
110     elif size == "WVGA":
111         tsize=get_par_size(pin,480)
112         encvf = encvf + ",scale=-3:480::0:3"+harddup
113         x264streamsize=str(tsize[0])+u"x480"
114         x264_bitrate="1500"
115     elif size == "FullHD":
116         tsize=get_par_size(pin,1080)
117         encvf = encvf + ",scale=-3:1080::0:3"+harddup
118         x264streamsize=str(tsize[0])+u"x1080"
119         x264_bitrate="5000"
120     elif size == "QVGA_BASE":
121         tsize=get_par_size(pin,240)
122         encvf = encvf + ",scale=-3:240::0:3"+harddup
123         x264streamsize=str(tsize[0])+u"x240"
124         x264_bitrate="300"
125     elif size == "WVGA_BASE":
126         tsize=get_par_size(pin,480)
127         encvf = encvf + ",scale=-3:480::0:3"+harddup
128         x264streamsize=str(tsize[0])+u"x480"
129         x264_bitrate="1500"
130     else:
131         tsize=get_par_size(pin,720)
132         encvf = encvf + ",scale=-3:720::0:3"+harddup
133         x264streamsize=str(tsize[0])+u"x720"
134         x264_bitrate="2500"
135     if deinterlace==0:
136         tsize=getMovieBaseSize(pin)
137         ofps="-ofps 30000/1001"
138         #fps="-fps 30000/1001"
139         fps=""
140         x264fps="30000/1001"
141         x264tune=x264tune+" --tff --nal-hrd vbr"
142         encvf="-vf hqdn3d=2:1:2"
143         harddup=",harddup"
144         encvf=encvf+harddup
145         x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
146     if tsize[0] <= 0 or tsize[1] <= 0:
147         encvf="-sws 9 -vf yadif=0,pp=l5"
148         harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
149         ofps="-ofps 30000/1001"
150         fps="-fps 30000/1001"
151         x264fps="30000/1001"
152         tsize=[1280,720]
153         encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
154         x264streamsize=u"1280x720"
155         x264_bitrate="2500"
156     if quality==1:
157         x264preset=u"ultrafast"
158     elif quality==2:
159         x264preset=u"veryfast"
160     elif quality==3:
161         x264preset=u"fast"
162     elif quality==4:
163         x264preset=u"medium"
164     elif quality==5:
165         x264preset=u"slow"
166     elif quality==6:
167         x264preset=u"slower"
168     if size == "WVGA_BASE" or size == "QVGA_BASE":
169         x264profile=" --level 32 --profile baseline "
170     else:
171         x264profile=" --level 42 --profile high "
172     x264crf=str(crf)
173     os.environ['LANG']="ja_JP.UTF-8"
174     random.seed(pin)
175     random.jumpahead(10)
176     temptime=int(time.time())
177     temptime=temptime % 9697
178     random.jumpahead(temptime)
179     streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))
180     os.system(u"mkfifo "+streampath)
181     encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" & "
182     encexe=encexe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf)
183     #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
184     encexe=u"nice -n 19 " +encexe
185     recdblist.printutf8(encexe)
186     txt=""
187     try:
188         txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8','ignore')
189     except:
190         ""
191     os.system("rm "+streampath)
192     recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
193
194
195 def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
196     """
197
198     """
199     ffmpeg=configreader.getpath("ffmpeg")
200     fps=u"-r 29.970030 "
201     x264fps="30000/1001"
202     x264streamsize=""
203     x264preset=""
204     x264tune=""
205     x264_bitrate="2500"
206     x264_thread="auto"
207     tsar=getMoviePAR(pin)
208     filter="-deinterlace"
209     x264sar=str(tsar[0])+":"+str(tsar[1])
210     if size == "HD":
211         tsize=get_par_size(pin,720)
212         s = "-s "+str(tsize[0])+"x720 "
213         x264streamsize=str(tsize[0])+u"x720"
214         x264_bitrate="2500"
215     elif size == "WVGA":
216         tsize=get_par_size(pin,480)
217         s = "-s "+str(tsize[0])+"x480 "
218         x264streamsize=str(tsize[0])+u"x480"
219         x264_bitrate="1500"
220     elif size == "FullHD":
221         tsize=get_par_size(pin,1080)
222         s = "-s "+str(tsize[0])+"x1080 "
223         x264streamsize=str(tsize[0])+u"x1080"
224         x264_bitrate="5000"
225     elif size == "SD":
226         tsize=get_par_size(pin,480)
227         s = "-s "+str(tsize[0])+"x480 "
228         x264streamsize=str(tsize[0])+u"x480"
229         x264_bitrate="1250"
230     elif size == "QVGA_BASE":
231         tsize=get_par_size(pin,240)
232         s = "-s "+str(tsize[0])+"x240 "
233         x264streamsize=str(tsize[0])+u"x240"
234         x264_bitrate="300"
235     elif size == "WVGA_BASE":
236         tsize=get_par_size(pin,480)
237         s = "-s "+str(tsize[0])+"x480 "
238         x264streamsize=str(tsize[0])+u"x480"
239         x264_bitrate="1500"
240     else:
241         tsize=get_par_size(pin,720)
242         s = "-s "+str(tsize[0])+"x720 "
243         x264streamsize=str(tsize[0])+u"x720"
244         x264_bitrate="2500"
245     if deinterlace==0:
246         tsize=getMovieBaseSize(pin)
247         fps=""
248         s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "
249         x264fps="30000/1001"
250         x264tune=x264tune+" --tff --nal-hrd vbr"
251         filter=""
252         x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
253     if quality==1:
254         x264preset=u"ultrafast"
255     elif quality==2:
256         x264preset=u"veryfast"
257     elif quality==3:
258         x264preset=u"fast"
259     elif quality==4:
260         x264preset=u"medium"
261     elif quality==5:
262         x264preset=u"slow"
263     elif quality==6:
264         x264preset=u"slower"
265     if size == "WVGA_BASE" or size == "QVGA_BASE":
266         x264profile=" --level 32 --profile baseline "
267     else:
268         x264profile=" --level 42 --profile high "
269     x264crf=str(crf)
270     txt=""
271     os.environ['LANG']="ja_JP.UTF-8"
272     exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - | "
273     exe=exe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)
274     exe = "nice -n 19 " + exe
275     txt=""
276     recdblist.printutf8(exe)
277     try:
278         txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
279     except:
280         ""
281     recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
282
283 def getMovieBaseSize(pin):
284     ffmpeg=configreader.getpath("ffmpeg")
285     os.environ['LANG']="ja_JP.UTF-8"
286     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
287     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
288     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
289     sizeMaxX=0
290     sizeMaxY=0
291     txtls=txts.split("\n")
292     for t in txtls:
293         rM=rT.match(t)
294         if rM:
295             sizetxt=rM.group(1)
296             partxt=rM.group(2)
297             tX=int(sizetxt.split("x")[0])
298             tY=int(sizetxt.split("x")[1])
299             tEX=int(partxt.split(":")[0])
300             tEY=int(partxt.split(":")[1])
301             if sizeMaxX<tX:
302                 sizeMaxX=tX
303                 sizeMaxY=tY
304     return [sizeMaxX,sizeMaxY]
305 def getMoviePAR(pin):
306     ffmpeg=configreader.getpath("ffmpeg")
307     os.environ['LANG']="ja_JP.UTF-8"
308     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
309     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
310     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
311     #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
312     sizeMaxX=0
313     parx=0
314     pary=0
315     txtls=txts.split("\n")
316     for t in txtls:
317         rM=rT.match(t)
318         if rM:
319             sizetxt=rM.group(1)
320             dartxt=rM.group(2)
321             tX=int(sizetxt.split("x")[0])
322             tY=int(sizetxt.split("x")[1])
323             tEX=int(dartxt.split(":")[0])
324             tEY=int(dartxt.split(":")[1])
325             if sizeMaxX<tX:
326                 sizeMaxX=tX
327                 if tX==1920 and tY==1080:
328                     parx=1
329                     pary=1
330                 else:
331                     parx=tEX
332                     pary=tEY
333     return [parx,pary]
334 def getMovieDAR(pin):
335     ffmpeg=configreader.getpath("ffmpeg")
336     os.environ['LANG']="ja_JP.UTF-8"
337     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
338     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
339     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
340     sizeMaxX=0
341     darx=0
342     dary=0
343     txtls=txts.split("\n")
344     for t in txtls:
345         rM=rT.match(t)
346         if rM:
347             sizetxt=rM.group(1)
348             dartxt=rM.group(2)
349             tX=int(sizetxt.split("x")[0])
350             tY=int(sizetxt.split("x")[1])
351             tEX=int(dartxt.split(":")[0])
352             tEY=int(dartxt.split(":")[1])
353             if sizeMaxX<tX:
354                 sizeMaxX=tX
355                 if tX==1920 and tY==1080:
356                     darx=16
357                     dary=9
358                 else:
359                     darx=tEX
360                     dary=tEY
361     return [darx,dary]
362 def get_par_size(pin,y):
363     tSize=getMovieBaseSize(pin)
364     if tSize[1] != 0 :
365         tX=tSize[0]*10*y/tSize[1]
366         tY=y
367         if tX>int(tX/10)*10:
368             tX=tX/10+1
369         else:
370             tX=tX/10
371     else:
372         tX=-1
373         tY=-1
374     return [tX,tY]
375
376 def get_x264core_version():
377     x264=configreader.getpath("x264")
378     t1=commands.getoutput(x264+" --help|grep core")
379     rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
380     rM=rT.match(t1)
381     v=-1
382     if rM:
383         v=int(rM.group(1))
384     return v
385 def get_x264_commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=0,bitrate=0):
386     x264=configreader.getpath("x264")
387     os.environ['LANG']="ja_JP.UTF-8"
388     x264_sar="--sar "+sar
389     x264preset=u"--preset "+preset
390     x264fps="--fps "+fps
391     if crf==0:
392         x264bitrate=u"--bitrate "+str(bitrate)
393     else:
394         x264crf=u"--crf "+str(crf)
395     x264_addline=configreader.getenv("x264_addline")
396     x264_thread="auto"
397     try:
398         xtt=configreader.getenv("x264_thread")
399         xtt=int(xtt)
400         if xtt>0:
401             x264_thread=str(xtt)
402     except:
403         x264_thread="auto"
404     x264_addline=configreader.getenv("x264_addline")
405     if get_x264core_version()>103:
406         x264res=u"--input-res "+x264streamsize
407         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
408     else:
409         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
410     return exe