OSDN Git Service

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