OSDN Git Service

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