3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
13 def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
15 pinで指定されたファイルをpoutにx264でエンコードして書き出す
22 quality=int(configreader.getenv("x264_preset"))
23 crf=int(configreader.getenv("crf"))
24 if re.search("H", opts):
26 if re.search("S", opts):
28 if re.search("F", opts):
30 if re.search("W",opts):
32 if re.search("MW8", opts):
35 if re.search("MW9", opts):
38 if re.search("v", opts):
40 crf=int(configreader.getenv("animation_crf"))
41 if re.search("a", opts):
43 crf=int(configreader.getenv("animation_crf"))
44 if re.search("q",opts):
46 if re.search("w",opts):
48 if re.search("e",opts):
50 if re.search("r",opts):
52 if re.search("u",opts):
54 if re.search("i",opts):
56 if re.search("o",opts):
58 if re.search("p",opts):
60 if re.search("d",opts):
61 encode_ffmpeg(pin,pout,size,is24fps,quality,crf)
62 elif re.search("5",opts):
63 encode_ffmpeg(pin,pout,size,is24fps,quality,crf)
64 elif re.search("Y",opts):
65 encode_ffmpeg_kai(pin,pout,size,is24fps,quality,crf)
66 elif re.search("b",opts):
68 tm2v=pin.replace(".ts",".m2v")
69 encode(tm2v, pout,size,is24fps,quality,crf)
70 except Exception, inst:
71 recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst))
74 encode(pin, pout,size,is24fps,quality,crf)
75 except Exception, inst:
76 recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst))
77 def encode(pin,pout,size,is24fps,quality,crf):
78 mencoder=configreader.getpath("mencoder")
79 x264=configreader.getpath("x264")
82 encvf="-vf yadif=0,pp=l5"
83 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
84 ofps="-ofps 30000/1001"
86 x264fps="--fps 30000/1001"
93 xtt=configreader.getenv("x264_thread")
99 x264_addline=configreader.getenv("x264_addline")
101 ofps="-ofps 24000/1001"
102 fps="-fps 30000/1001"
103 x264fps="--fps 24000/1001"
104 x264tune="--tune animation"
105 encvf="-vf pullup,softskip"
106 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
108 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
109 x264streamsize=u"1280x720"
112 encvf = encvf + ",scale=-2:480::0:3,expand=854:480"+harddup
113 x264streamsize=u"854x480"
115 elif size == "FullHD":
116 encvf = encvf + ",scale=-2:1080::0:3,expand=1920:1080"+harddup
117 x264streamsize=u"1920x1080"
120 encvf = encvf + ",scale=720:-2::0:3,expand=720:480"+harddup
121 x264streamsize=u"720x480"
123 elif size == "QVGA_BASE":
124 encvf = encvf + ",scale=320:-2::0:3,expand=320:240"+harddup
125 x264streamsize=u"320x240"
127 elif size == "WVGA_BASE":
128 encvf = encvf + ",scale=-2:480::0:3,expand=854:480"+harddup
129 x264streamsize=u"854x480"
132 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
133 x264streamsize=u"1280x720"
136 x264preset=u"--preset ultrafast"
138 x264preset=u"--preset veryfast"
140 x264preset=u"--preset fast"
142 x264preset=u"--preset medium"
144 x264preset=u"--preset slow"
146 x264preset=u"--preset slower"
147 if size == "WVGA_BASE" or size == "QVGA_BASE":
148 x264profile=" --level 32 --profile baseline "
150 x264profile=" --level 41 --profile high "
151 x264crf=u"--crf "+str(crf)
152 os.environ['LANG']="ja_JP.UTF-8"
155 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 999999)))
156 os.system(u"mkfifo "+streampath)
157 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet -sws 9 "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" &"
158 encexe=encexe+u" nice -n 19 "+x264+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" "+streampath+" "+x264streamsize
159 encexe=u"nice -n 19 " +encexe
160 recdblist.printutf8(encexe)
163 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
166 os.system("rm "+streampath)
167 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
169 def encode_ffmpeg(pin,pout,size,is24fps,quality,crf):
173 ffmpeg=configreader.getpath("ffmpeg")
174 x264=configreader.getpath("x264")
176 x264fps="--fps 30000/1001"
183 xtt=configreader.getenv("x264_thread")
189 x264_addline=configreader.getenv("x264_addline")
192 x264streamsize=u"1280x720"
196 x264streamsize=u"854x480"
198 elif size == "FullHD":
200 x264streamsize=u"1920x1080"
204 x264streamsize=u"720x480"
206 elif size == "QVGA_BASE":
208 x264streamsize=u"320x240"
210 elif size == "WVGA_BASE":
212 x264streamsize=u"854x480"
216 x264streamsize=u"1280x720"
219 x264preset=u"--preset ultrafast"
221 x264preset=u"--preset veryfast"
223 x264preset=u"--preset fast"
225 x264preset=u"--preset medium"
227 x264preset=u"--preset slow"
229 x264preset=u"--preset slower"
230 if size == "WVGA_BASE" or size == "QVGA_BASE":
231 x264profile=" --level 32 --profile baseline "
233 x264profile=" --level 41 --profile high "
234 x264crf=u"--crf "+str(crf)
236 os.environ['LANG']="ja_JP.UTF-8"
237 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - |"
238 exe=exe+u" nice -n 19 "+x264+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
239 exe = "nice -n 19 " + exe
241 recdblist.printutf8(exe)
243 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
246 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
247 def encode_ffmpeg_kai(pin,pout,size,is24fps,quality,crf):
251 ffmpeg=configreader.getpath("ffmpeg")
252 x264=configreader.getpath("x264")
254 x264fps="--fps 30000/1001"
261 xtt=configreader.getenv("x264_thread")
267 x264_addline=configreader.getenv("x264_addline")
269 padx=get16_9ffmpeg_s(pin,1280,720)
270 s = "-s "+str(1280-2*padx)+"x720 "
272 s=s+"-padleft "+str(padx)+" -padright "+str(padx)+" "
273 x264streamsize=u"1280x720"
276 padx=get16_9ffmpeg_s(pin,854,480)
277 s = "-s "+str(854-2*padx)+"x480 "
279 s=s+"-padleft "+str(padx)+" -padright "+str(padx)+" "
280 x264streamsize=u"854x480"
282 elif size == "FullHD":
283 padx=get16_9ffmpeg_s(pin,1920,1080)
284 s = "-s "+str(1920-2*padx)+"x1080 "
286 s=s+"-padleft "+str(padx)+" -padright "+str(padx)+" "
287 x264streamsize=u"1920x1080"
291 x264streamsize=u"720x480"
293 elif size == "QVGA_BASE":
295 x264streamsize=u"320x240"
297 elif size == "WVGA_BASE":
299 x264streamsize=u"854x480"
302 padx=get16_9ffmpeg_s(pin,1280,720)
303 s = "-s "+str(1280-2*padx)+"x720 "
305 s=s+"-padleft "+str(padx)+" -padright "+str(padx)+" "
306 x264streamsize=u"1280x720"
309 x264preset=u"--preset ultrafast"
311 x264preset=u"--preset veryfast"
313 x264preset=u"--preset fast"
315 x264preset=u"--preset medium"
317 x264preset=u"--preset slow"
319 x264preset=u"--preset slower"
320 if size == "WVGA_BASE" or size == "QVGA_BASE":
321 x264profile=" --level 32 --profile baseline "
323 x264profile=" --level 41 --profile high "
324 x264crf=u"--crf "+str(crf)
326 os.environ['LANG']="ja_JP.UTF-8"
327 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - |"
328 exe=exe+u" nice -n 19 "+x264+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
329 exe = "nice -n 19 " + exe
331 recdblist.printutf8(exe)
333 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
336 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
337 def getMovieSize(pin):
338 ffmpeg=configreader.getpath("ffmpeg")
339 os.environ['LANG']="ja_JP.UTF-8"
340 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
341 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
342 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
345 txtls=txts.split("\n")
351 tX=int(sizetxt.split("x")[0])
352 tY=int(sizetxt.split("x")[1])
353 tEX=int(partxt.split(":")[0])
354 tEY=int(partxt.split(":")[1])
356 if tX2>10*int(tX2/10):
363 return [sizeMaxX,sizeMaxY]
364 def getMovieDAR(pin):
365 ffmpeg=configreader.getpath("ffmpeg")
366 os.environ['LANG']="ja_JP.UTF-8"
367 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
368 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
369 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
373 txtls=txts.split("\n")
379 tX=int(sizetxt.split("x")[0])
380 tY=int(sizetxt.split("x")[1])
381 tEX=int(dartxt.split(":")[0])
382 tEY=int(dartxt.split(":")[1])
388 def get16_9ffmpeg_s(pin,x,y):
390 tDAR=getMovieDAR(pin)
391 if (tDAR[0]==16 and tDAR[1]==9):
396 tSize=getMovieSize(pin)
397 tn=x-tSize[1]*x/tSize[0]