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     x264_sar=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="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     x264=configreader.getpath("x264")
187     fps=u"-r 29.970030 "
188     x264fps="30000/1001"
189     x264streamsize=""
190     x264preset=""
191     x264tune=""
192     x264_bitrate="2500"
193     x264_thread="auto"
194     tsar=getMoviePAR(pin)
195     x264_sar=str(tsar[0])+":"+str(tsar[1])
196     if size == "HD":
197         tsize=get_par_size(pin,720)
198         s = "-s "+str(tsize[0])+"x720 "
199         x264streamsize=str(tsize[0])+u"x720"
200         x264_bitrate="2500"
201     elif size == "WVGA":
202         tsize=get_par_size(pin,480)
203         s = "-s "+str(tsize[0])+"x480 "
204         x264streamsize=str(tsize[0])+u"x480"
205         x264_bitrate="1500"
206     elif size == "FullHD":
207         tsize=get_par_size(pin,1080)
208         s = "-s "+str(tsize[0])+"x1080 "
209         x264streamsize=str(tsize[0])+u"x1080"
210         x264_bitrate="5000"
211     elif size == "SD":
212         tsize=get_par_size(pin,480)
213         s = "-s "+str(tsize[0])+"x480 "
214         x264streamsize=str(tsize[0])+u"x480"
215         x264_bitrate="1250"
216     elif size == "QVGA_BASE":
217         tsize=get_par_size(pin,240)
218         s = "-s "+str(tsize[0])+"x240 "
219         x264streamsize=str(tsize[0])+u"x240"
220         x264_bitrate="300"
221     elif size == "WVGA_BASE":
222         tsize=get_par_size(pin,480)
223         s = "-s "+str(tsize[0])+"x480 "
224         x264streamsize=str(tsize[0])+u"x480"
225         x264_bitrate="1500"
226     else:
227         tsize=get_par_size(pin,720)
228         s = "-s "+str(tsize[0])+"x720 "
229         x264streamsize=str(tsize[0])+u"x720"
230         x264_bitrate="2500"
231     if quality==1:
232         x264preset=u"ultrafast"
233     elif quality==2:
234         x264preset=u"veryfast"
235     elif quality==3:
236         x264preset=u"fast"
237     elif quality==4:
238         x264preset=u"medium"
239     elif quality==5:
240         x264preset=u"slow"
241     elif quality==6:
242         x264preset=u"slower"
243     if size == "WVGA_BASE" or size == "QVGA_BASE":
244         x264profile=" --level 32 --profile baseline "
245     else:
246         x264profile=" --level 42 --profile high "
247     x264crf=u"--crf "+str(crf)
248     txt=""
249     os.environ['LANG']="ja_JP.UTF-8"
250     exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - | "
251     exe=exe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)
252     #exe=exe+u" nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
253     exe = "nice -n 19 " + exe
254     txt=""
255     recdblist.printutf8(exe)
256     try:
257         txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
258     except:
259         ""
260     recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
261
262 def getMovieBaseSize(pin):
263     ffmpeg=configreader.getpath("ffmpeg")
264     os.environ['LANG']="ja_JP.UTF-8"
265     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
266     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
267     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
268     sizeMaxX=0
269     sizeMaxY=0
270     txtls=txts.split("\n")
271     for t in txtls:
272         rM=rT.match(t)
273         if rM:
274             sizetxt=rM.group(1)
275             partxt=rM.group(2)
276             tX=int(sizetxt.split("x")[0])
277             tY=int(sizetxt.split("x")[1])
278             tEX=int(partxt.split(":")[0])
279             tEY=int(partxt.split(":")[1])
280             if sizeMaxX<tX:
281                 sizeMaxX=tX
282                 sizeMaxY=tY
283     return [sizeMaxX,sizeMaxY]
284 def getMoviePAR(pin):
285     ffmpeg=configreader.getpath("ffmpeg")
286     os.environ['LANG']="ja_JP.UTF-8"
287     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
288     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
289     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
290     #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
291     sizeMaxX=0
292     parx=0
293     pary=0
294     txtls=txts.split("\n")
295     for t in txtls:
296         rM=rT.match(t)
297         if rM:
298             sizetxt=rM.group(1)
299             dartxt=rM.group(2)
300             tX=int(sizetxt.split("x")[0])
301             tY=int(sizetxt.split("x")[1])
302             tEX=int(dartxt.split(":")[0])
303             tEY=int(dartxt.split(":")[1])
304             if sizeMaxX<tX:
305                 sizeMaxX=tX
306                 if tX==1920 and tY==1080:
307                     parx=1
308                     pary=1
309                 else:
310                     parx=tEX
311                     pary=tEY
312     return [parx,pary]
313 def getMovieDAR(pin):
314     ffmpeg=configreader.getpath("ffmpeg")
315     os.environ['LANG']="ja_JP.UTF-8"
316     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
317     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
318     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
319     sizeMaxX=0
320     darx=0
321     dary=0
322     txtls=txts.split("\n")
323     for t in txtls:
324         rM=rT.match(t)
325         if rM:
326             sizetxt=rM.group(1)
327             dartxt=rM.group(2)
328             tX=int(sizetxt.split("x")[0])
329             tY=int(sizetxt.split("x")[1])
330             tEX=int(dartxt.split(":")[0])
331             tEY=int(dartxt.split(":")[1])
332             if sizeMaxX<tX:
333                 sizeMaxX=tX
334                 if tX==1920 and tY==1080:
335                     darx=16
336                     dary=9
337                 else:
338                     darx=tEX
339                     dary=tEY
340     return [darx,dary]
341 def get_par_size(pin,y):
342     tSize=getMovieBaseSize(pin)
343     tX=tSize[0]*10*y/tSize[1]
344     tY=y
345     if tX>int(tX/10)*10:
346         tX=tX/10+1
347     else:
348         tX=tX/10
349     return [tX,tY]
350
351 def get_x264core_version():
352     x264=configreader.getpath("x264")
353     t1=commands.getoutput(x264+" --help|grep core")
354     rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
355     rM=rT.match(t1)
356     v=-1
357     if rM:
358         v=int(rM.group(1))
359     return v
360 def get_x264_commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=0,bitrate=0):
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