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