OSDN Git Service

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