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="-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="-vf pullup,softskip"
109         harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
110     if deinterlace==0:
111         ofps="-ofps 60000/1001"
112         fps="-fps 60000/1001"
113         x264fps="--fps 60000/1001"
114         x264tune="--tune animation --tff --nal-hrd vbr"
115         encvf="-vf hqdn3d=2:1:2"
116         harddup=",harddup"
117     if size == "HD":
118         tsize=get_par_size(pin,720)
119         encvf = encvf + ",scale=-3:720::0:3"+harddup
120         x264streamsize=str(tsize[0])+u"x720"
121         x264_bitrate="2500"
122     elif size == "WVGA":
123         tsize=get_par_size(pin,480)
124         encvf = encvf + ",scale=-3:480::0:3"+harddup
125         x264streamsize=str(tsize[0])+u"x480"
126         x264_bitrate="1500"
127     elif size == "FullHD":
128         tsize=get_par_size(pin,1080)
129         encvf = encvf + ",scale=-3:1080::0:3"+harddup
130         x264streamsize=str(tsize[0])+u"x1080"
131         x264_bitrate="5000"
132     elif size == "QVGA_BASE":
133         tsize=get_par_size(pin,240)
134         encvf = encvf + ",scale=-3:240::0:3"+harddup
135         x264streamsize=str(tsize[0])+u"x240"
136         x264_bitrate="300"
137     elif size == "WVGA_BASE":
138         tsize=get_par_size(pin,480)
139         encvf = encvf + ",scale=-3:480::0:3"+harddup
140         x264streamsize=str(tsize[0])+u"x480"
141         x264_bitrate="1500"
142     else:
143         tsize=get_par_size(pin,720)
144         encvf = encvf + ",scale=-3:720::0:3"+harddup
145         x264streamsize=str(tsize[0])+u"x720"
146         x264_bitrate="2500"
147     if quality==1:
148         x264preset=u"--preset ultrafast"
149     elif quality==2:
150         x264preset=u"--preset veryfast"
151     elif quality==3:
152         x264preset=u"--preset fast"
153     elif quality==4:
154         x264preset=u"--preset medium"
155     elif quality==5:
156         x264preset=u"--preset slow"
157     elif quality==6:
158         x264preset=u"--preset slower"
159     if size == "WVGA_BASE" or size == "QVGA_BASE":
160         x264profile=" --level 32 --profile baseline "
161     else:
162         x264profile=" --level 42 --profile high "
163     x264crf=u"--crf "+str(crf)
164     os.environ['LANG']="ja_JP.UTF-8"
165     random.seed(pin)
166     random.jumpahead(10)
167     streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 999999)))
168     os.system(u"mkfifo "+streampath)
169     encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet -sws 9 "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" &"
170     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
171     encexe=u"nice -n 19 " +encexe
172     recdblist.printutf8(encexe)
173     txt=""
174     try:
175         txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
176     except:
177         ""
178     os.system("rm "+streampath)
179     recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
180
181
182 def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
183     """
184
185     """
186     ffmpeg=configreader.getpath("ffmpeg")
187     x264=configreader.getpath("x264")
188     fps=u"-r 29.970030 "
189     x264fps="--fps 30000/1001"
190     x264streamsize=""
191     x264preset=""
192     x264tune=""
193     x264_bitrate="2500"
194     x264_thread="auto"
195     tsar=getMoviePAR(pin)
196     x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
197     try:
198         xtt=configreader.getenv("x264_thread")
199         xtt=int(xtt)
200         if xtt>0:
201             x264_thread=str(xtt)
202     except:
203         x264_thread="auto"
204     x264_addline=configreader.getenv("x264_addline")
205     if size == "HD":
206         tsize=get_par_size(pin,720)
207         s = "-s "+str(tsize[0])+"x720 "
208         x264streamsize=str(tsize[0])+u"x720"
209         x264_bitrate="2500"
210     elif size == "WVGA":
211         tsize=get_par_size(pin,480)
212         s = "-s "+str(tsize[0])+"x480 "
213         x264streamsize=str(tsize[0])+u"x480"
214         x264_bitrate="1500"
215     elif size == "FullHD":
216         tsize=get_par_size(pin,1080)
217         s = "-s "+str(tsize[0])+"x1080 "
218         x264streamsize=str(tsize[0])+u"x1080"
219         x264_bitrate="5000"
220     elif size == "SD":
221         tsize=get_par_size(pin,480)
222         s = "-s "+str(tsize[0])+"x480 "
223         x264streamsize=str(tsize[0])+u"x480"
224         x264_bitrate="1250"
225     elif size == "QVGA_BASE":
226         tsize=get_par_size(pin,240)
227         s = "-s "+str(tsize[0])+"x240 "
228         x264streamsize=str(tsize[0])+u"x240"
229         x264_bitrate="300"
230     elif size == "WVGA_BASE":
231         tsize=get_par_size(pin,480)
232         s = "-s "+str(tsize[0])+"x480 "
233         x264streamsize=str(tsize[0])+u"x480"
234         x264_bitrate="1500"
235     else:
236         tsize=get_par_size(pin,720)
237         s = "-s "+str(tsize[0])+"x720 "
238         x264streamsize=str(tsize[0])+u"x720"
239         x264_bitrate="2500"
240     if quality==1:
241         x264preset=u"--preset ultrafast"
242     elif quality==2:
243         x264preset=u"--preset veryfast"
244     elif quality==3:
245         x264preset=u"--preset fast"
246     elif quality==4:
247         x264preset=u"--preset medium"
248     elif quality==5:
249         x264preset=u"--preset slow"
250     elif quality==6:
251         x264preset=u"--preset slower"
252     if size == "WVGA_BASE" or size == "QVGA_BASE":
253         x264profile=" --level 32 --profile baseline "
254     else:
255         x264profile=" --level 42 --profile high "
256     x264crf=u"--crf "+str(crf)
257     txt=""
258     os.environ['LANG']="ja_JP.UTF-8"
259     exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - |"
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 def getMovieSize(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 tX==1920 and tY==1080:
288                 tEX=1
289                 tEY=1
290             tX2=tX*tEX*10/tEY
291             if tX2>10*int(tX2/10):
292                 tX2=tX2/10+1
293             else:
294                 tX2=tX2/10
295             if sizeMaxX<tX2:
296                 sizeMaxX=tX2
297                 sizeMaxY=tY
298     return [sizeMaxX,sizeMaxY]
299 def getMovieBaseSize(pin):
300     ffmpeg=configreader.getpath("ffmpeg")
301     os.environ['LANG']="ja_JP.UTF-8"
302     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
303     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
304     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
305     sizeMaxX=0
306     sizeMaxY=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             partxt=rM.group(2)
313             tX=int(sizetxt.split("x")[0])
314             tY=int(sizetxt.split("x")[1])
315             tEX=int(partxt.split(":")[0])
316             tEY=int(partxt.split(":")[1])
317             if sizeMaxX<tX:
318                 sizeMaxX=tX
319                 sizeMaxY=tY
320     return [sizeMaxX,sizeMaxY]
321 def getMoviePAR(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+PAR\D+(\d+:\d+)\D+.*\Z")
327     #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
328     sizeMaxX=0
329     parx=0
330     pary=0
331     txtls=txts.split("\n")
332     for t in txtls:
333         rM=rT.match(t)
334         if rM:
335             sizetxt=rM.group(1)
336             dartxt=rM.group(2)
337             tX=int(sizetxt.split("x")[0])
338             tY=int(sizetxt.split("x")[1])
339             tEX=int(dartxt.split(":")[0])
340             tEY=int(dartxt.split(":")[1])
341             if sizeMaxX<tX:
342                 sizeMaxX=tX
343                 if tX==1920 and tY==1080:
344                     parx=1
345                     pary=1
346                 else:
347                     parx=tEX
348                     pary=tEY
349     return [parx,pary]
350 def getMovieDAR(pin):
351     ffmpeg=configreader.getpath("ffmpeg")
352     os.environ['LANG']="ja_JP.UTF-8"
353     exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
354     txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
355     rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
356     sizeMaxX=0
357     darx=0
358     dary=0
359     txtls=txts.split("\n")
360     for t in txtls:
361         rM=rT.match(t)
362         if rM:
363             sizetxt=rM.group(1)
364             dartxt=rM.group(2)
365             tX=int(sizetxt.split("x")[0])
366             tY=int(sizetxt.split("x")[1])
367             tEX=int(dartxt.split(":")[0])
368             tEY=int(dartxt.split(":")[1])
369             if sizeMaxX<tX:
370                 sizeMaxX=tX
371                 if tX==1920 and tY==1080:
372                     darx=16
373                     dary=9
374                 else:
375                     darx=tEX
376                     dary=tEY
377     return [darx,dary]
378 def get16_9ffmpeg_s(pin,x,y):
379     try:
380         tDAR=getMovieDAR(pin)
381         if (tDAR[0]==16 and tDAR[1]==9):
382             return 0
383         elif tDAR==[0,0]:
384             return 0
385         else:
386             tSize=getMovieSize(pin)
387             tn=x-tSize[1]*x/tSize[0]
388             tn=tn/4
389             tn=tn*2#偶数にするための処理
390             return tn
391     except:
392         return 0
393 def get_par_size(pin,y):
394     tSize=getMovieBaseSize(pin)
395     tX=tSize[0]*10*y/tSize[1]
396     tY=y
397     if tX>int(tX/10)*10:
398         tX=tX/10+1
399     else:
400         tX=tX/10
401     return [tX,tY]