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