OSDN Git Service

implement interlaced encoding(beta).
[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     x264=configreader.getpath("x264")
82     encvf=""
83     txt=""
84     encvf="-sws 9 -vf yadif=0,pp=l5"
85     harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
86     ofps="-ofps 30000/1001"
87     fps="-fps 30000/1001"
88     x264fps="--fps 30000/1001"
89     x264streamsize=""
90     x264preset=""
91     x264tune=""
92     x264_bitrate="5000"
93     x264_thread="auto"
94     tsar=getMoviePAR(pin)
95     x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
96     try:
97         xtt=configreader.getenv("x264_thread")
98         xtt=int(xtt)
99         if xtt>0:
100             x264_thread=str(xtt)
101     except:
102         x264_thread="auto"
103     x264_addline=configreader.getenv("x264_addline")
104     if is24fps==1:
105         ofps="-ofps 24000/1001"
106         fps="-fps 30000/1001"
107         x264fps="--fps 24000/1001"
108         x264tune="--tune animation"
109         encvf="-sws 9 -vf pullup,softskip"
110         harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
111     if size == "HD":
112         tsize=get_par_size(pin,720)
113         encvf = encvf + ",scale=-3:720::0:3"+harddup
114         x264streamsize=str(tsize[0])+u"x720"
115         x264_bitrate="2500"
116     elif size == "WVGA":
117         tsize=get_par_size(pin,480)
118         encvf = encvf + ",scale=-3:480::0:3"+harddup
119         x264streamsize=str(tsize[0])+u"x480"
120         x264_bitrate="1500"
121     elif size == "FullHD":
122         tsize=get_par_size(pin,1080)
123         encvf = encvf + ",scale=-3:1080::0:3"+harddup
124         x264streamsize=str(tsize[0])+u"x1080"
125         x264_bitrate="5000"
126     elif size == "QVGA_BASE":
127         tsize=get_par_size(pin,240)
128         encvf = encvf + ",scale=-3:240::0:3"+harddup
129         x264streamsize=str(tsize[0])+u"x240"
130         x264_bitrate="300"
131     elif size == "WVGA_BASE":
132         tsize=get_par_size(pin,480)
133         encvf = encvf + ",scale=-3:480::0:3"+harddup
134         x264streamsize=str(tsize[0])+u"x480"
135         x264_bitrate="1500"
136     else:
137         tsize=get_par_size(pin,720)
138         encvf = encvf + ",scale=-3:720::0:3"+harddup
139         x264streamsize=str(tsize[0])+u"x720"
140         x264_bitrate="2500"
141     if deinterlace==0:
142         tsize=getMovieBaseSize(pin)
143         ofps="-ofps 30000/1001"
144         #fps="-fps 30000/1001"
145         fps=""
146         x264fps="--fps 30000/1001"
147         x264tune=x264tune+" --tff --nal-hrd vbr"
148         encvf="-vf hqdn3d=2:1:2"
149         harddup=",harddup"
150         encvf=encvf+harddup
151         x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
152     if quality==1:
153         x264preset=u"--preset ultrafast"
154     elif quality==2:
155         x264preset=u"--preset veryfast"
156     elif quality==3:
157         x264preset=u"--preset fast"
158     elif quality==4:
159         x264preset=u"--preset medium"
160     elif quality==5:
161         x264preset=u"--preset slow"
162     elif quality==6:
163         x264preset=u"--preset 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=u"--crf "+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+u" nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u"  --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" "+streampath+" "+x264streamsize
179     encexe=u"nice -n 19 " +encexe
180     recdblist.printutf8(encexe)
181     txt=""
182     try:
183         txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
184     except:
185         ""
186     os.system("rm "+streampath)
187     recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
188
189
190 def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
191     """
192
193     """
194     ffmpeg=configreader.getpath("ffmpeg")
195     x264=configreader.getpath("x264")
196     fps=u"-r 29.970030 "
197     x264fps="--fps 30000/1001"
198     x264streamsize=""
199     x264preset=""
200     x264tune=""
201     x264_bitrate="2500"
202     x264_thread="auto"
203     tsar=getMoviePAR(pin)
204     x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
205     try:
206         xtt=configreader.getenv("x264_thread")
207         xtt=int(xtt)
208         if xtt>0:
209             x264_thread=str(xtt)
210     except:
211         x264_thread="auto"
212     x264_addline=configreader.getenv("x264_addline")
213     if size == "HD":
214         tsize=get_par_size(pin,720)
215         s = "-s "+str(tsize[0])+"x720 "
216         x264streamsize=str(tsize[0])+u"x720"
217         x264_bitrate="2500"
218     elif size == "WVGA":
219         tsize=get_par_size(pin,480)
220         s = "-s "+str(tsize[0])+"x480 "
221         x264streamsize=str(tsize[0])+u"x480"
222         x264_bitrate="1500"
223     elif size == "FullHD":
224         tsize=get_par_size(pin,1080)
225         s = "-s "+str(tsize[0])+"x1080 "
226         x264streamsize=str(tsize[0])+u"x1080"
227         x264_bitrate="5000"
228     elif size == "SD":
229         tsize=get_par_size(pin,480)
230         s = "-s "+str(tsize[0])+"x480 "
231         x264streamsize=str(tsize[0])+u"x480"
232         x264_bitrate="1250"
233     elif size == "QVGA_BASE":
234         tsize=get_par_size(pin,240)
235         s = "-s "+str(tsize[0])+"x240 "
236         x264streamsize=str(tsize[0])+u"x240"
237         x264_bitrate="300"
238     elif size == "WVGA_BASE":
239         tsize=get_par_size(pin,480)
240         s = "-s "+str(tsize[0])+"x480 "
241         x264streamsize=str(tsize[0])+u"x480"
242         x264_bitrate="1500"
243     else:
244         tsize=get_par_size(pin,720)
245         s = "-s "+str(tsize[0])+"x720 "
246         x264streamsize=str(tsize[0])+u"x720"
247         x264_bitrate="2500"
248     if quality==1:
249         x264preset=u"--preset ultrafast"
250     elif quality==2:
251         x264preset=u"--preset veryfast"
252     elif quality==3:
253         x264preset=u"--preset fast"
254     elif quality==4:
255         x264preset=u"--preset medium"
256     elif quality==5:
257         x264preset=u"--preset slow"
258     elif quality==6:
259         x264preset=u"--preset slower"
260     if size == "WVGA_BASE" or size == "QVGA_BASE":
261         x264profile=" --level 32 --profile baseline "
262     else:
263         x264profile=" --level 42 --profile high "
264     x264crf=u"--crf "+str(crf)
265     txt=""
266     os.environ['LANG']="ja_JP.UTF-8"
267     exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - |"
268     exe=exe+u" nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
269     exe = "nice -n 19 " + exe
270     txt=""
271     recdblist.printutf8(exe)
272     try:
273         txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
274     except:
275         ""
276     recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
277
278 def getMovieBaseSize(pin):
279     ffmpeg=configreader.getpath("ffmpeg")
280     os.environ['LANG']="ja_JP.UTF-8"
281     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
282     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
283     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
284     sizeMaxX=0
285     sizeMaxY=0
286     txtls=txts.split("\n")
287     for t in txtls:
288         rM=rT.match(t)
289         if rM:
290             sizetxt=rM.group(1)
291             partxt=rM.group(2)
292             tX=int(sizetxt.split("x")[0])
293             tY=int(sizetxt.split("x")[1])
294             tEX=int(partxt.split(":")[0])
295             tEY=int(partxt.split(":")[1])
296             if sizeMaxX<tX:
297                 sizeMaxX=tX
298                 sizeMaxY=tY
299     return [sizeMaxX,sizeMaxY]
300 def getMoviePAR(pin):
301     ffmpeg=configreader.getpath("ffmpeg")
302     os.environ['LANG']="ja_JP.UTF-8"
303     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
304     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
305     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
306     #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
307     sizeMaxX=0
308     parx=0
309     pary=0
310     txtls=txts.split("\n")
311     for t in txtls:
312         rM=rT.match(t)
313         if rM:
314             sizetxt=rM.group(1)
315             dartxt=rM.group(2)
316             tX=int(sizetxt.split("x")[0])
317             tY=int(sizetxt.split("x")[1])
318             tEX=int(dartxt.split(":")[0])
319             tEY=int(dartxt.split(":")[1])
320             if sizeMaxX<tX:
321                 sizeMaxX=tX
322                 if tX==1920 and tY==1080:
323                     parx=1
324                     pary=1
325                 else:
326                     parx=tEX
327                     pary=tEY
328     return [parx,pary]
329 def getMovieDAR(pin):
330     ffmpeg=configreader.getpath("ffmpeg")
331     os.environ['LANG']="ja_JP.UTF-8"
332     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
333     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
334     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
335     sizeMaxX=0
336     darx=0
337     dary=0
338     txtls=txts.split("\n")
339     for t in txtls:
340         rM=rT.match(t)
341         if rM:
342             sizetxt=rM.group(1)
343             dartxt=rM.group(2)
344             tX=int(sizetxt.split("x")[0])
345             tY=int(sizetxt.split("x")[1])
346             tEX=int(dartxt.split(":")[0])
347             tEY=int(dartxt.split(":")[1])
348             if sizeMaxX<tX:
349                 sizeMaxX=tX
350                 if tX==1920 and tY==1080:
351                     darx=16
352                     dary=9
353                 else:
354                     darx=tEX
355                     dary=tEY
356     return [darx,dary]
357 def get_par_size(pin,y):
358     tSize=getMovieBaseSize(pin)
359     tX=tSize[0]*10*y/tSize[1]
360     tY=y
361     if tX>int(tX/10)*10:
362         tX=tX/10+1
363     else:
364         tX=tX/10
365     return [tX,tY]