OSDN Git Service

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