OSDN Git Service

implement interlaced encode option(alpha ver).
[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
12 import recdblist
13 def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
14     """
15     pinで指定されたファイルをpoutにx264でエンコードして書き出す
16     """
17     dualpass = 0
18     is24fps=0
19     size="HD"
20     crf=18
21     quality=4
22     quality=int(configreader.getenv("x264_preset"))
23     crf=int(configreader.getenv("crf"))
24     deinterlace=1
25     if re.search("H", opts):
26         size = "HD"
27     if re.search("S", opts):
28         size = "WVGA"
29     if re.search("F", opts):
30         size = "FullHD"
31     if re.search("W",opts):
32         size = "WVGA"
33     if re.search("MW8", opts):
34         size = "QVGA_BASE"
35         crf=crf+4
36     if re.search("MW9", opts):
37         size = "WVGA_BASE"
38         crf=crf+2
39     if re.search("v", opts):
40         is24fps=1
41         crf=int(configreader.getenv("animation_crf"))
42     if re.search("a", opts):
43         is24fps=1
44         crf=int(configreader.getenv("animation_crf"))
45     if re.search("I", opts):
46         deinterlace=0
47     if re.search("q",opts):
48         quality=quality-2
49     if re.search("w",opts):
50         quality=quality-1
51     if re.search("e",opts):
52         quality=quality+1
53     if re.search("r",opts):
54         quality=quality+2
55     if re.search("u",opts):
56         crf=crf+2
57     if re.search("i",opts):
58         crf=crf+1
59     if re.search("o",opts):
60         crf=crf-1
61     if re.search("p",opts):
62         crf=crf-2
63     if re.search("d",opts):
64         encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace)
65     elif re.search("5",opts):
66         encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace)
67     elif re.search("b",opts):
68         try:
69             tm2v=pin.replace(".ts",".m2v")
70             encode_sar(tm2v, pout,size,is24fps,quality,crf,deinterlace)
71         except Exception, inst:
72             recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst))
73     else:
74         try:
75             encode_sar(pin, pout,size,is24fps,quality,crf,deinterlace)
76         except Exception, inst:
77             recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst))
78 def encode_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
79     mencoder=configreader.getpath("mencoder")
80     x264=configreader.getpath("x264")
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="--fps 30000/1001"
88     x264streamsize=""
89     x264preset=""
90     x264tune=""
91     x264_bitrate="5000"
92     x264_thread="auto"
93     tsar=getMoviePAR(pin)
94     x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
95     try:
96         xtt=configreader.getenv("x264_thread")
97         xtt=int(xtt)
98         if xtt>0:
99             x264_thread=str(xtt)
100     except:
101         x264_thread="auto"
102     x264_addline=configreader.getenv("x264_addline")
103     if is24fps==1:
104         ofps="-ofps 24000/1001"
105         fps="-fps 30000/1001"
106         x264fps="--fps 24000/1001"
107         x264tune="--tune animation"
108         encvf="-sws 9 -vf pullup,softskip"
109         harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
110     if size == "HD":
111         tsize=get_par_size(pin,720)
112         encvf = encvf + ",scale=-3:720::0:3"+harddup
113         x264streamsize=str(tsize[0])+u"x720"
114         x264_bitrate="2500"
115     elif size == "WVGA":
116         tsize=get_par_size(pin,480)
117         encvf = encvf + ",scale=-3:480::0:3"+harddup
118         x264streamsize=str(tsize[0])+u"x480"
119         x264_bitrate="1500"
120     elif size == "FullHD":
121         tsize=get_par_size(pin,1080)
122         encvf = encvf + ",scale=-3:1080::0:3"+harddup
123         x264streamsize=str(tsize[0])+u"x1080"
124         x264_bitrate="5000"
125     elif size == "QVGA_BASE":
126         tsize=get_par_size(pin,240)
127         encvf = encvf + ",scale=-3:240::0:3"+harddup
128         x264streamsize=str(tsize[0])+u"x240"
129         x264_bitrate="300"
130     elif size == "WVGA_BASE":
131         tsize=get_par_size(pin,480)
132         encvf = encvf + ",scale=-3:480::0:3"+harddup
133         x264streamsize=str(tsize[0])+u"x480"
134         x264_bitrate="1500"
135     else:
136         tsize=get_par_size(pin,720)
137         encvf = encvf + ",scale=-3:720::0:3"+harddup
138         x264streamsize=str(tsize[0])+u"x720"
139         x264_bitrate="2500"
140     if deinterlace==0:
141         tsize=getMovieBaseSize(pin)
142         ofps="-ofps 30000/1001"
143         fps="-fps 30000/1001"
144         x264fps="--fps 30000/1001"
145         x264tune=x264tune+" --tff --nal-hrd vbr"
146         encvf="-vf hqdn3d=2:1:2"
147         harddup=",harddup"
148         encvf=encvf+harddup
149         x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
150     if quality==1:
151         x264preset=u"--preset ultrafast"
152     elif quality==2:
153         x264preset=u"--preset veryfast"
154     elif quality==3:
155         x264preset=u"--preset fast"
156     elif quality==4:
157         x264preset=u"--preset medium"
158     elif quality==5:
159         x264preset=u"--preset slow"
160     elif quality==6:
161         x264preset=u"--preset slower"
162     if size == "WVGA_BASE" or size == "QVGA_BASE":
163         x264profile=" --level 32 --profile baseline "
164     else:
165         x264profile=" --level 42 --profile high "
166     x264crf=u"--crf "+str(crf)
167     os.environ['LANG']="ja_JP.UTF-8"
168     random.seed(pin)
169     random.jumpahead(10)
170     streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 999999)))
171     os.system(u"mkfifo "+streampath)
172     encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" &"
173     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
174     encexe=u"nice -n 19 " +encexe
175     recdblist.printutf8(encexe)
176     txt=""
177     try:
178         txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
179     except:
180         ""
181     os.system("rm "+streampath)
182     recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
183
184
185 def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
186     """
187
188     """
189     ffmpeg=configreader.getpath("ffmpeg")
190     x264=configreader.getpath("x264")
191     fps=u"-r 29.970030 "
192     x264fps="--fps 30000/1001"
193     x264streamsize=""
194     x264preset=""
195     x264tune=""
196     x264_bitrate="2500"
197     x264_thread="auto"
198     tsar=getMoviePAR(pin)
199     x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
200     try:
201         xtt=configreader.getenv("x264_thread")
202         xtt=int(xtt)
203         if xtt>0:
204             x264_thread=str(xtt)
205     except:
206         x264_thread="auto"
207     x264_addline=configreader.getenv("x264_addline")
208     if size == "HD":
209         tsize=get_par_size(pin,720)
210         s = "-s "+str(tsize[0])+"x720 "
211         x264streamsize=str(tsize[0])+u"x720"
212         x264_bitrate="2500"
213     elif size == "WVGA":
214         tsize=get_par_size(pin,480)
215         s = "-s "+str(tsize[0])+"x480 "
216         x264streamsize=str(tsize[0])+u"x480"
217         x264_bitrate="1500"
218     elif size == "FullHD":
219         tsize=get_par_size(pin,1080)
220         s = "-s "+str(tsize[0])+"x1080 "
221         x264streamsize=str(tsize[0])+u"x1080"
222         x264_bitrate="5000"
223     elif size == "SD":
224         tsize=get_par_size(pin,480)
225         s = "-s "+str(tsize[0])+"x480 "
226         x264streamsize=str(tsize[0])+u"x480"
227         x264_bitrate="1250"
228     elif size == "QVGA_BASE":
229         tsize=get_par_size(pin,240)
230         s = "-s "+str(tsize[0])+"x240 "
231         x264streamsize=str(tsize[0])+u"x240"
232         x264_bitrate="300"
233     elif size == "WVGA_BASE":
234         tsize=get_par_size(pin,480)
235         s = "-s "+str(tsize[0])+"x480 "
236         x264streamsize=str(tsize[0])+u"x480"
237         x264_bitrate="1500"
238     else:
239         tsize=get_par_size(pin,720)
240         s = "-s "+str(tsize[0])+"x720 "
241         x264streamsize=str(tsize[0])+u"x720"
242         x264_bitrate="2500"
243     if quality==1:
244         x264preset=u"--preset ultrafast"
245     elif quality==2:
246         x264preset=u"--preset veryfast"
247     elif quality==3:
248         x264preset=u"--preset fast"
249     elif quality==4:
250         x264preset=u"--preset medium"
251     elif quality==5:
252         x264preset=u"--preset slow"
253     elif quality==6:
254         x264preset=u"--preset slower"
255     if size == "WVGA_BASE" or size == "QVGA_BASE":
256         x264profile=" --level 32 --profile baseline "
257     else:
258         x264profile=" --level 42 --profile high "
259     x264crf=u"--crf "+str(crf)
260     txt=""
261     os.environ['LANG']="ja_JP.UTF-8"
262     exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - |"
263     exe=exe+u" nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
264     exe = "nice -n 19 " + exe
265     txt=""
266     recdblist.printutf8(exe)
267     try:
268         txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
269     except:
270         ""
271     recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
272
273 def getMovieBaseSize(pin):
274     ffmpeg=configreader.getpath("ffmpeg")
275     os.environ['LANG']="ja_JP.UTF-8"
276     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
277     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
278     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
279     sizeMaxX=0
280     sizeMaxY=0
281     txtls=txts.split("\n")
282     for t in txtls:
283         rM=rT.match(t)
284         if rM:
285             sizetxt=rM.group(1)
286             partxt=rM.group(2)
287             tX=int(sizetxt.split("x")[0])
288             tY=int(sizetxt.split("x")[1])
289             tEX=int(partxt.split(":")[0])
290             tEY=int(partxt.split(":")[1])
291             if sizeMaxX<tX:
292                 sizeMaxX=tX
293                 sizeMaxY=tY
294     return [sizeMaxX,sizeMaxY]
295 def getMoviePAR(pin):
296     ffmpeg=configreader.getpath("ffmpeg")
297     os.environ['LANG']="ja_JP.UTF-8"
298     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
299     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
300     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
301     #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
302     sizeMaxX=0
303     parx=0
304     pary=0
305     txtls=txts.split("\n")
306     for t in txtls:
307         rM=rT.match(t)
308         if rM:
309             sizetxt=rM.group(1)
310             dartxt=rM.group(2)
311             tX=int(sizetxt.split("x")[0])
312             tY=int(sizetxt.split("x")[1])
313             tEX=int(dartxt.split(":")[0])
314             tEY=int(dartxt.split(":")[1])
315             if sizeMaxX<tX:
316                 sizeMaxX=tX
317                 if tX==1920 and tY==1080:
318                     parx=1
319                     pary=1
320                 else:
321                     parx=tEX
322                     pary=tEY
323     return [parx,pary]
324 def getMovieDAR(pin):
325     ffmpeg=configreader.getpath("ffmpeg")
326     os.environ['LANG']="ja_JP.UTF-8"
327     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
328     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
329     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
330     sizeMaxX=0
331     darx=0
332     dary=0
333     txtls=txts.split("\n")
334     for t in txtls:
335         rM=rT.match(t)
336         if rM:
337             sizetxt=rM.group(1)
338             dartxt=rM.group(2)
339             tX=int(sizetxt.split("x")[0])
340             tY=int(sizetxt.split("x")[1])
341             tEX=int(dartxt.split(":")[0])
342             tEY=int(dartxt.split(":")[1])
343             if sizeMaxX<tX:
344                 sizeMaxX=tX
345                 if tX==1920 and tY==1080:
346                     darx=16
347                     dary=9
348                 else:
349                     darx=tEX
350                     dary=tEY
351     return [darx,dary]
352 def get_par_size(pin,y):
353     tSize=getMovieBaseSize(pin)
354     tX=tSize[0]*10*y/tSize[1]
355     tY=y
356     if tX>int(tX/10)*10:
357         tX=tX/10+1
358     else:
359         tX=tX/10
360     return [tX,tY]