OSDN Git Service

add verbose level.
[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 tsize[0] <= 0 and tsize[1] <= 0:
143         tsize=[1280,720]
144     if quality==1:
145         x264preset=u"ultrafast"
146     elif quality==2:
147         x264preset=u"veryfast"
148     elif quality==3:
149         x264preset=u"fast"
150     elif quality==4:
151         x264preset=u"medium"
152     elif quality==5:
153         x264preset=u"slow"
154     elif quality==6:
155         x264preset=u"slower"
156     if size == "WVGA_BASE" or size == "QVGA_BASE":
157         x264profile=" --level 32 --profile baseline "
158     else:
159         x264profile=" --level 42 --profile high "
160     x264crf=str(crf)
161     os.environ['LANG']="ja_JP.UTF-8"
162     random.seed(pin)
163     random.jumpahead(10)
164     temptime=int(time.time())
165     temptime=temptime % 9697
166     random.jumpahead(temptime)
167     streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))
168     os.system(u"mkfifo "+streampath)
169     encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" & "
170     encexe=encexe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf)
171     #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
172     encexe=u"nice -n 19 " +encexe
173     recdblist.printutf8(encexe)
174     txt=""
175     try:
176         txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
177     except:
178         ""
179     os.system("rm "+streampath)
180     recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
181
182
183 def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
184     """
185
186     """
187     ffmpeg=configreader.getpath("ffmpeg")
188     fps=u"-r 29.970030 "
189     x264fps="30000/1001"
190     x264streamsize=""
191     x264preset=""
192     x264tune=""
193     x264_bitrate="2500"
194     x264_thread="auto"
195     tsar=getMoviePAR(pin)
196     filter="-deinterlace"
197     x264sar=str(tsar[0])+":"+str(tsar[1])
198     if size == "HD":
199         tsize=get_par_size(pin,720)
200         s = "-s "+str(tsize[0])+"x720 "
201         x264streamsize=str(tsize[0])+u"x720"
202         x264_bitrate="2500"
203     elif size == "WVGA":
204         tsize=get_par_size(pin,480)
205         s = "-s "+str(tsize[0])+"x480 "
206         x264streamsize=str(tsize[0])+u"x480"
207         x264_bitrate="1500"
208     elif size == "FullHD":
209         tsize=get_par_size(pin,1080)
210         s = "-s "+str(tsize[0])+"x1080 "
211         x264streamsize=str(tsize[0])+u"x1080"
212         x264_bitrate="5000"
213     elif size == "SD":
214         tsize=get_par_size(pin,480)
215         s = "-s "+str(tsize[0])+"x480 "
216         x264streamsize=str(tsize[0])+u"x480"
217         x264_bitrate="1250"
218     elif size == "QVGA_BASE":
219         tsize=get_par_size(pin,240)
220         s = "-s "+str(tsize[0])+"x240 "
221         x264streamsize=str(tsize[0])+u"x240"
222         x264_bitrate="300"
223     elif size == "WVGA_BASE":
224         tsize=get_par_size(pin,480)
225         s = "-s "+str(tsize[0])+"x480 "
226         x264streamsize=str(tsize[0])+u"x480"
227         x264_bitrate="1500"
228     else:
229         tsize=get_par_size(pin,720)
230         s = "-s "+str(tsize[0])+"x720 "
231         x264streamsize=str(tsize[0])+u"x720"
232         x264_bitrate="2500"
233     if deinterlace==0:
234         tsize=getMovieBaseSize(pin)
235         fps=""
236         s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "
237         x264fps="30000/1001"
238         x264tune=x264tune+" --tff --nal-hrd vbr"
239         filter=""
240         x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
241     if quality==1:
242         x264preset=u"ultrafast"
243     elif quality==2:
244         x264preset=u"veryfast"
245     elif quality==3:
246         x264preset=u"fast"
247     elif quality==4:
248         x264preset=u"medium"
249     elif quality==5:
250         x264preset=u"slow"
251     elif quality==6:
252         x264preset=u"slower"
253     if size == "WVGA_BASE" or size == "QVGA_BASE":
254         x264profile=" --level 32 --profile baseline "
255     else:
256         x264profile=" --level 42 --profile high "
257     x264crf=str(crf)
258     txt=""
259     os.environ['LANG']="ja_JP.UTF-8"
260     exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - | "
261     exe=exe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)
262     exe = "nice -n 19 " + exe
263     txt=""
264     recdblist.printutf8(exe)
265     try:
266         txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
267     except:
268         ""
269     recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
270
271 def getMovieBaseSize(pin):
272     ffmpeg=configreader.getpath("ffmpeg")
273     os.environ['LANG']="ja_JP.UTF-8"
274     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
275     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
276     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
277     sizeMaxX=0
278     sizeMaxY=0
279     txtls=txts.split("\n")
280     for t in txtls:
281         rM=rT.match(t)
282         if rM:
283             sizetxt=rM.group(1)
284             partxt=rM.group(2)
285             tX=int(sizetxt.split("x")[0])
286             tY=int(sizetxt.split("x")[1])
287             tEX=int(partxt.split(":")[0])
288             tEY=int(partxt.split(":")[1])
289             if sizeMaxX<tX:
290                 sizeMaxX=tX
291                 sizeMaxY=tY
292     return [sizeMaxX,sizeMaxY]
293 def getMoviePAR(pin):
294     ffmpeg=configreader.getpath("ffmpeg")
295     os.environ['LANG']="ja_JP.UTF-8"
296     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
297     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
298     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
299     #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
300     sizeMaxX=0
301     parx=0
302     pary=0
303     txtls=txts.split("\n")
304     for t in txtls:
305         rM=rT.match(t)
306         if rM:
307             sizetxt=rM.group(1)
308             dartxt=rM.group(2)
309             tX=int(sizetxt.split("x")[0])
310             tY=int(sizetxt.split("x")[1])
311             tEX=int(dartxt.split(":")[0])
312             tEY=int(dartxt.split(":")[1])
313             if sizeMaxX<tX:
314                 sizeMaxX=tX
315                 if tX==1920 and tY==1080:
316                     parx=1
317                     pary=1
318                 else:
319                     parx=tEX
320                     pary=tEY
321     return [parx,pary]
322 def getMovieDAR(pin):
323     ffmpeg=configreader.getpath("ffmpeg")
324     os.environ['LANG']="ja_JP.UTF-8"
325     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
326     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
327     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
328     sizeMaxX=0
329     darx=0
330     dary=0
331     txtls=txts.split("\n")
332     for t in txtls:
333         rM=rT.match(t)
334         if rM:
335             sizetxt=rM.group(1)
336             dartxt=rM.group(2)
337             tX=int(sizetxt.split("x")[0])
338             tY=int(sizetxt.split("x")[1])
339             tEX=int(dartxt.split(":")[0])
340             tEY=int(dartxt.split(":")[1])
341             if sizeMaxX<tX:
342                 sizeMaxX=tX
343                 if tX==1920 and tY==1080:
344                     darx=16
345                     dary=9
346                 else:
347                     darx=tEX
348                     dary=tEY
349     return [darx,dary]
350 def get_par_size(pin,y):
351     tSize=getMovieBaseSize(pin)
352     if tSize[1] != 0 :
353         tX=tSize[0]*10*y/tSize[1]
354         tY=y
355         if tX>int(tX/10)*10:
356             tX=tX/10+1
357         else:
358             tX=tX/10
359     else:
360         tX=-1
361         tY=-1
362     return [tX,tY]
363
364 def get_x264core_version():
365     x264=configreader.getpath("x264")
366     t1=commands.getoutput(x264+" --help|grep core")
367     rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
368     rM=rT.match(t1)
369     v=-1
370     if rM:
371         v=int(rM.group(1))
372     return v
373 def get_x264_commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=0,bitrate=0):
374     x264=configreader.getpath("x264")
375     os.environ['LANG']="ja_JP.UTF-8"
376     x264_sar="--sar "+sar
377     x264preset=u"--preset "+preset
378     x264fps="--fps "+fps
379     if crf==0:
380         x264bitrate=u"--bitrate "+str(bitrate)
381     else:
382         x264crf=u"--crf "+str(crf)
383     x264_addline=configreader.getenv("x264_addline")
384     x264_thread="auto"
385     try:
386         xtt=configreader.getenv("x264_thread")
387         xtt=int(xtt)
388         if xtt>0:
389             x264_thread=str(xtt)
390     except:
391         x264_thread="auto"
392     x264_addline=configreader.getenv("x264_addline")
393     if get_x264core_version()>103:
394         x264res=u"--input-res "+x264streamsize
395         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
396     else:
397         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
398     return exe