3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
15 def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
17 pinで指定されたファイルをpoutにx264でエンコードして書き出す
24 quality=int(configreader.getenv("x264_preset"))
25 crf=int(configreader.getenv("crf"))
27 if re.search("H", opts):
29 if re.search("S", opts):
31 if re.search("F", opts):
33 if re.search("W",opts):
35 if re.search("1",opts):
38 if re.search("2", opts):
41 if re.search("v", opts):
43 crf=int(configreader.getenv("animation_crf"))
44 if re.search("a", opts):
46 crf=int(configreader.getenv("animation_crf"))
47 if re.search("I", opts):
49 if re.search("q",opts):
51 if re.search("w",opts):
53 if re.search("e",opts):
55 if re.search("r",opts):
57 if re.search("u",opts):
59 if re.search("i",opts):
61 if re.search("o",opts):
63 if re.search("p",opts):
65 if re.search("d",opts):#二カ国語放送の場合
66 tm2v=pin.replace(".ts",".m2v")
67 encode_ffmpeg_sar(tm2v,pout,size,is24fps,quality,crf,deinterlace)
68 elif re.search("5",opts):#5.1chの場合
69 encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace)
72 encode_sar(pin, pout,size,is24fps,quality,crf,deinterlace)
73 except Exception, inst:
74 recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
75 def encode_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
76 mencoder=configreader.getpath("mencoder")
79 encvf="-sws 9 -vf yadif=0,pp=l5"
80 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
81 ofps="-ofps 30000/1001"
89 x264sar=str(tsar[0])+":"+str(tsar[1])
91 ofps="-ofps 24000/1001"
94 x264tune="--tune animation"
95 encvf="-sws 9 -vf pullup,softskip"
96 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
98 tsize=get_par_size(pin,720)
99 encvf = encvf + ",scale=-3:720::0:3"+harddup
100 x264streamsize=str(tsize[0])+u"x720"
103 tsize=get_par_size(pin,480)
104 encvf = encvf + ",scale=-3:480::0:3"+harddup
105 x264streamsize=str(tsize[0])+u"x480"
107 elif size == "FullHD":
108 tsize=get_par_size(pin,1080)
109 encvf = encvf + ",scale=-3:1080::0:3"+harddup
110 x264streamsize=str(tsize[0])+u"x1080"
112 elif size == "QVGA_BASE":
113 tsize=get_par_size(pin,240)
114 encvf = encvf + ",scale=-3:240::0:3"+harddup
115 x264streamsize=str(tsize[0])+u"x240"
117 elif size == "WVGA_BASE":
118 tsize=get_par_size(pin,480)
119 encvf = encvf + ",scale=-3:480::0:3"+harddup
120 x264streamsize=str(tsize[0])+u"x480"
123 tsize=get_par_size(pin,720)
124 encvf = encvf + ",scale=-3:720::0:3"+harddup
125 x264streamsize=str(tsize[0])+u"x720"
128 tsize=getMovieBaseSize(pin)
129 ofps="-ofps 30000/1001"
130 #fps="-fps 30000/1001"
133 x264tune=x264tune+" --tff --nal-hrd vbr"
134 encvf="-vf hqdn3d=2:1:2"
137 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
138 if tsize[0] <= 0 or tsize[1] <= 0:
139 encvf="-sws 9 -vf yadif=0,pp=l5"
140 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
141 ofps="-ofps 30000/1001"
142 fps="-fps 30000/1001"
145 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
146 x264streamsize=u"1280x720"
149 x264preset=u"ultrafast"
151 x264preset=u"veryfast"
160 if size == "WVGA_BASE" or size == "QVGA_BASE":
161 x264profile=" --level 32 --profile baseline "
163 x264profile=" --level 42 --profile high "
165 os.environ['LANG']="ja_JP.UTF-8"
168 temptime=int(time.time())
169 temptime=temptime % 9697
170 random.jumpahead(temptime)
171 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))
172 os.system(u"mkfifo "+streampath)
173 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" & "
174 encexe=encexe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf)
175 #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
176 encexe=u"nice -n 19 " +encexe
177 recdblist.printutf8(encexe)
180 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8','ignore')
183 os.system("rm "+streampath)
184 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
187 def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
191 ffmpeg=configreader.getpath("ffmpeg")
199 tsar=getMoviePAR(pin)
200 filter="-deinterlace"
201 x264sar=str(tsar[0])+":"+str(tsar[1])
203 tsize=get_par_size(pin,720)
204 s = "-s "+str(tsize[0])+"x720 "
205 x264streamsize=str(tsize[0])+u"x720"
208 tsize=get_par_size(pin,480)
209 s = "-s "+str(tsize[0])+"x480 "
210 x264streamsize=str(tsize[0])+u"x480"
212 elif size == "FullHD":
213 tsize=get_par_size(pin,1080)
214 s = "-s "+str(tsize[0])+"x1080 "
215 x264streamsize=str(tsize[0])+u"x1080"
218 tsize=get_par_size(pin,480)
219 s = "-s "+str(tsize[0])+"x480 "
220 x264streamsize=str(tsize[0])+u"x480"
222 elif size == "QVGA_BASE":
223 tsize=get_par_size(pin,240)
224 s = "-s "+str(tsize[0])+"x240 "
225 x264streamsize=str(tsize[0])+u"x240"
227 elif size == "WVGA_BASE":
228 tsize=get_par_size(pin,480)
229 s = "-s "+str(tsize[0])+"x480 "
230 x264streamsize=str(tsize[0])+u"x480"
233 tsize=get_par_size(pin,720)
234 s = "-s "+str(tsize[0])+"x720 "
235 x264streamsize=str(tsize[0])+u"x720"
238 tsize=getMovieBaseSize(pin)
240 s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "
242 x264tune=x264tune+" --tff --nal-hrd vbr"
244 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
246 x264preset=u"ultrafast"
248 x264preset=u"veryfast"
257 if size == "WVGA_BASE" or size == "QVGA_BASE":
258 x264profile=" --level 32 --profile baseline "
260 x264profile=" --level 41 --profile high "
263 os.environ['LANG']="ja_JP.UTF-8"
264 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - | "
265 exe=exe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)
266 exe = "nice -n 19 " + exe
268 recdblist.printutf8(exe)
270 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
273 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
275 def getMovieBaseSize(pin):
276 ffmpeg=configreader.getpath("ffmpeg")
277 os.environ['LANG']="ja_JP.UTF-8"
278 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
279 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
280 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
283 txtls=txts.split("\n")
289 tX=int(sizetxt.split("x")[0])
290 tY=int(sizetxt.split("x")[1])
291 tEX=int(partxt.split(":")[0])
292 tEY=int(partxt.split(":")[1])
296 return [sizeMaxX,sizeMaxY]
297 def getMovieBaseSize2(pin):##動画開始後すぐに解像度が変更されたときに対処
298 ffmpeg=configreader.getpath("ffmpeg")
299 os.environ['LANG']="ja_JP.UTF-8"
300 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
301 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
302 rT=re.compile(u"Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
305 txtls=txts.split("\n")
311 tX=int(sizetxt.split("x")[0])
312 tY=int(sizetxt.split("x")[1])
313 tEX=int(partxt.split(":")[0])
314 tEY=int(partxt.split(":")[1])
318 os.remove(pin+".size.ts")
319 return [sizeMaxX,sizeMaxY]
320 def getMoviePAR(pin):
321 ffmpeg=configreader.getpath("ffmpeg")
322 os.environ['LANG']="ja_JP.UTF-8"
323 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
324 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
325 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
329 txtls=txts.split("\n")
335 tX=int(sizetxt.split("x")[0])
336 tY=int(sizetxt.split("x")[1])
337 tEX=int(dartxt.split(":")[0])
338 tEY=int(dartxt.split(":")[1])
341 if tX==1920 and tY==1080:
348 def getMoviePAR2(pin):
349 ffmpeg=configreader.getpath("ffmpeg")
350 os.environ['LANG']="ja_JP.UTF-8"
351 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
352 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
353 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
357 txtls=txts.split("\n")
363 tX=int(sizetxt.split("x")[0])
364 tY=int(sizetxt.split("x")[1])
365 tEX=int(dartxt.split(":")[0])
366 tEY=int(dartxt.split(":")[1])
369 if tX==1920 and tY==1080:
375 os.remove(pin+".size.ts")
377 def getMovieDAR(pin):
378 ffmpeg=configreader.getpath("ffmpeg")
379 os.environ['LANG']="ja_JP.UTF-8"
380 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
381 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
382 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
386 txtls=txts.split("\n")
392 tX=int(sizetxt.split("x")[0])
393 tY=int(sizetxt.split("x")[1])
394 tEX=int(dartxt.split(":")[0])
395 tEY=int(dartxt.split(":")[1])
398 if tX==1920 and tY==1080:
405 def getMovieDAR2(pin):
406 ffmpeg=configreader.getpath("ffmpeg")
407 os.environ['LANG']="ja_JP.UTF-8"
408 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
409 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
410 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
414 txtls=txts.split("\n")
420 tX=int(sizetxt.split("x")[0])
421 tY=int(sizetxt.split("x")[1])
422 tEX=int(dartxt.split(":")[0])
423 tEY=int(dartxt.split(":")[1])
426 if tX==1920 and tY==1080:
432 os.remove(pin+".size.ts")
434 def get_par_size(pin,y):
435 tSize=getMovieBaseSize(pin)
437 tX=tSize[0]*10*y/tSize[1]
447 def get_x264core_version():
448 x264=configreader.getpath("x264")
449 t1=commands.getoutput(x264+" --help|grep core")
450 rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
456 def get_x264_commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=-1,bitrate=0):
457 x264=configreader.getpath("x264")
458 os.environ['LANG']="ja_JP.UTF-8"
459 x264_sar="--sar "+sar
460 x264preset=u"--preset "+preset
463 x264bitrate=u"--bitrate "+str(bitrate)
465 x264crf=u"--crf "+str(crf)
466 x264_addline=configreader.getenv("x264_addline")
469 xtt=configreader.getenv("x264_thread")
475 x264_addline=configreader.getenv("x264_addline")
476 if get_x264core_version()>103:
477 x264res=u"--input-res "+x264streamsize
478 exe=u"nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --colormatrix bt709 --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" "+x264res+" -o \""+pout+"\" "+pin
480 exe=u"nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --colormatrix bt709 --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" "+pin+" "+x264streamsize