OSDN Git Service

implement ffmepg interlaced 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
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     filter="-deinterlace"
195     x264sar=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 deinterlace==0:
232         tsize=getMovieBaseSize(pin)
233         fps=""
234         s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "
235         x264fps="30000/1001"
236         x264tune=x264tune+" --tff --nal-hrd vbr"
237         filter=""
238         x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
239     if quality==1:
240         x264preset=u"ultrafast"
241     elif quality==2:
242         x264preset=u"veryfast"
243     elif quality==3:
244         x264preset=u"fast"
245     elif quality==4:
246         x264preset=u"medium"
247     elif quality==5:
248         x264preset=u"slow"
249     elif quality==6:
250         x264preset=u"slower"
251     if size == "WVGA_BASE" or size == "QVGA_BASE":
252         x264profile=" --level 32 --profile baseline "
253     else:
254         x264profile=" --level 42 --profile high "
255     x264crf=str(crf)
256     txt=""
257     os.environ['LANG']="ja_JP.UTF-8"
258     exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - | "
259     exe=exe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)
260     #exe=exe+u" nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
261     exe = "nice -n 19 " + exe
262     txt=""
263     recdblist.printutf8(exe)
264     try:
265         txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
266     except:
267         ""
268     recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
269
270 def getMovieBaseSize(pin):
271     ffmpeg=configreader.getpath("ffmpeg")
272     os.environ['LANG']="ja_JP.UTF-8"
273     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
274     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
275     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
276     sizeMaxX=0
277     sizeMaxY=0
278     txtls=txts.split("\n")
279     for t in txtls:
280         rM=rT.match(t)
281         if rM:
282             sizetxt=rM.group(1)
283             partxt=rM.group(2)
284             tX=int(sizetxt.split("x")[0])
285             tY=int(sizetxt.split("x")[1])
286             tEX=int(partxt.split(":")[0])
287             tEY=int(partxt.split(":")[1])
288             if sizeMaxX<tX:
289                 sizeMaxX=tX
290                 sizeMaxY=tY
291     return [sizeMaxX,sizeMaxY]
292 def getMoviePAR(pin):
293     ffmpeg=configreader.getpath("ffmpeg")
294     os.environ['LANG']="ja_JP.UTF-8"
295     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
296     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
297     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
298     #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
299     sizeMaxX=0
300     parx=0
301     pary=0
302     txtls=txts.split("\n")
303     for t in txtls:
304         rM=rT.match(t)
305         if rM:
306             sizetxt=rM.group(1)
307             dartxt=rM.group(2)
308             tX=int(sizetxt.split("x")[0])
309             tY=int(sizetxt.split("x")[1])
310             tEX=int(dartxt.split(":")[0])
311             tEY=int(dartxt.split(":")[1])
312             if sizeMaxX<tX:
313                 sizeMaxX=tX
314                 if tX==1920 and tY==1080:
315                     parx=1
316                     pary=1
317                 else:
318                     parx=tEX
319                     pary=tEY
320     return [parx,pary]
321 def getMovieDAR(pin):
322     ffmpeg=configreader.getpath("ffmpeg")
323     os.environ['LANG']="ja_JP.UTF-8"
324     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
325     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
326     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
327     sizeMaxX=0
328     darx=0
329     dary=0
330     txtls=txts.split("\n")
331     for t in txtls:
332         rM=rT.match(t)
333         if rM:
334             sizetxt=rM.group(1)
335             dartxt=rM.group(2)
336             tX=int(sizetxt.split("x")[0])
337             tY=int(sizetxt.split("x")[1])
338             tEX=int(dartxt.split(":")[0])
339             tEY=int(dartxt.split(":")[1])
340             if sizeMaxX<tX:
341                 sizeMaxX=tX
342                 if tX==1920 and tY==1080:
343                     darx=16
344                     dary=9
345                 else:
346                     darx=tEX
347                     dary=tEY
348     return [darx,dary]
349 def get_par_size(pin,y):
350     tSize=getMovieBaseSize(pin)
351     tX=tSize[0]*10*y/tSize[1]
352     tY=y
353     if tX>int(tX/10)*10:
354         tX=tX/10+1
355     else:
356         tX=tX/10
357     return [tX,tY]
358
359 def get_x264core_version():
360     x264=configreader.getpath("x264")
361     t1=commands.getoutput(x264+" --help|grep core")
362     rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
363     rM=rT.match(t1)
364     v=-1
365     if rM:
366         v=int(rM.group(1))
367     return v
368 def get_x264_commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=0,bitrate=0):
369     x264=configreader.getpath("x264")
370     os.environ['LANG']="ja_JP.UTF-8"
371     x264_sar="--sar "+sar
372     x264preset=u"--preset "+preset
373     x264fps="--fps "+fps
374     if crf==0:
375         x264bitrate=u"--bitrate "+str(bitrate)
376     else:
377         x264crf=u"--crf "+str(crf)
378     x264_addline=configreader.getenv("x264_addline")
379     x264_thread="auto"
380     try:
381         xtt=configreader.getenv("x264_thread")
382         xtt=int(xtt)
383         if xtt>0:
384             x264_thread=str(xtt)
385     except:
386         x264_thread="auto"
387     x264_addline=configreader.getenv("x264_addline")
388     if get_x264core_version()>103:
389         x264res=u"--input-res "+x264streamsize
390         exe=u"nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" "+x264res+" -o \""+pout+"\" "+pin
391     else:
392         exe=u"nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" "+pin+" "+x264streamsize
393     return exe