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"
88 tsar=getMoviePAR2(pin)
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=getMovieBaseSize2(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 41 --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)
185 def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
189 ffmpeg=configreader.getpath("ffmpeg")
197 tsar=getMoviePAR2(pin)
198 filter="-deinterlace"
199 x264sar=str(tsar[0])+":"+str(tsar[1])
201 tsize=get_par_size(pin,720)
202 s = "-s "+str(tsize[0])+"x720 "
203 x264streamsize=str(tsize[0])+u"x720"
206 tsize=get_par_size(pin,480)
207 s = "-s "+str(tsize[0])+"x480 "
208 x264streamsize=str(tsize[0])+u"x480"
210 elif size == "FullHD":
211 tsize=get_par_size(pin,1080)
212 s = "-s "+str(tsize[0])+"x1080 "
213 x264streamsize=str(tsize[0])+u"x1080"
216 tsize=get_par_size(pin,480)
217 s = "-s "+str(tsize[0])+"x480 "
218 x264streamsize=str(tsize[0])+u"x480"
220 elif size == "QVGA_BASE":
221 tsize=get_par_size(pin,240)
222 s = "-s "+str(tsize[0])+"x240 "
223 x264streamsize=str(tsize[0])+u"x240"
225 elif size == "WVGA_BASE":
226 tsize=get_par_size(pin,480)
227 s = "-s "+str(tsize[0])+"x480 "
228 x264streamsize=str(tsize[0])+u"x480"
231 tsize=get_par_size(pin,720)
232 s = "-s "+str(tsize[0])+"x720 "
233 x264streamsize=str(tsize[0])+u"x720"
236 tsize=getMovieBaseSize2(pin)
238 s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "
240 x264tune=x264tune+" --tff --nal-hrd vbr"
242 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
244 x264preset=u"ultrafast"
246 x264preset=u"veryfast"
255 if size == "WVGA_BASE" or size == "QVGA_BASE":
256 x264profile=" --level 32 --profile baseline "
258 x264profile=" --level 41 --profile high "
261 os.environ['LANG']="ja_JP.UTF-8"
262 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - | "
263 exe=exe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)
264 exe = "nice -n 19 " + exe
266 recdblist.printutf8(exe)
268 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
271 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
272 def getMovieBaseSize(pin):
273 ffmpeg=configreader.getpath("ffmpeg")
274 os.environ['LANG']="ja_JP.UTF-8"
275 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
276 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
277 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
280 txtls=txts.split("\n")
286 tX=int(sizetxt.split("x")[0])
287 tY=int(sizetxt.split("x")[1])
288 tEX=int(partxt.split(":")[0])
289 tEY=int(partxt.split(":")[1])
293 return [sizeMaxX,sizeMaxY]
294 def getMovieBaseSize2(pin):##動画開始後すぐに解像度が変更されたときに対処
295 ffmpeg=configreader.getpath("ffmpeg")
296 os.environ['LANG']="ja_JP.UTF-8"
297 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
298 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
299 rT=re.compile(u"Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
302 txtls=txts.split("\n")
308 tX=int(sizetxt.split("x")[0])
309 tY=int(sizetxt.split("x")[1])
310 tEX=int(partxt.split(":")[0])
311 tEY=int(partxt.split(":")[1])
315 os.remove(pin+".size.ts")
316 return [sizeMaxX,sizeMaxY]
317 def getMoviePAR(pin):
318 ffmpeg=configreader.getpath("ffmpeg")
319 os.environ['LANG']="ja_JP.UTF-8"
320 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
321 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
322 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
326 txtls=txts.split("\n")
332 tX=int(sizetxt.split("x")[0])
333 tY=int(sizetxt.split("x")[1])
334 tEX=int(dartxt.split(":")[0])
335 tEY=int(dartxt.split(":")[1])
338 if tX==1920 and tY==1080:
345 def getMoviePAR2(pin):
346 ffmpeg=configreader.getpath("ffmpeg")
347 os.environ['LANG']="ja_JP.UTF-8"
348 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
349 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
350 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
354 txtls=txts.split("\n")
360 tX=int(sizetxt.split("x")[0])
361 tY=int(sizetxt.split("x")[1])
362 tEX=int(dartxt.split(":")[0])
363 tEY=int(dartxt.split(":")[1])
366 if tX==1920 and tY==1080:
372 os.remove(pin+".size.ts")
374 def getMovieDAR(pin):
375 ffmpeg=configreader.getpath("ffmpeg")
376 os.environ['LANG']="ja_JP.UTF-8"
377 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
378 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
379 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
383 txtls=txts.split("\n")
389 tX=int(sizetxt.split("x")[0])
390 tY=int(sizetxt.split("x")[1])
391 tEX=int(dartxt.split(":")[0])
392 tEY=int(dartxt.split(":")[1])
395 if tX==1920 and tY==1080:
402 def getMovieDAR2(pin):
403 ffmpeg=configreader.getpath("ffmpeg")
404 os.environ['LANG']="ja_JP.UTF-8"
405 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
406 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
407 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
411 txtls=txts.split("\n")
417 tX=int(sizetxt.split("x")[0])
418 tY=int(sizetxt.split("x")[1])
419 tEX=int(dartxt.split(":")[0])
420 tEY=int(dartxt.split(":")[1])
423 if tX==1920 and tY==1080:
429 os.remove(pin+".size.ts")
431 def get_par_size(pin,y):
432 tSize=getMovieBaseSize2(pin)
434 tX=tSize[0]*10*y/tSize[1]
444 def get_x264core_version():
445 x264=configreader.getpath("x264")
446 t1=commands.getoutput(x264+" --help|grep core")
447 rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
453 def get_x264_commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=-1,bitrate=0):
454 x264=configreader.getpath("x264")
455 os.environ['LANG']="ja_JP.UTF-8"
456 x264_sar="--sar "+sar
457 x264preset=u"--preset "+preset
460 x264bitrate=u"--bitrate "+str(bitrate)
462 x264crf=u"--crf "+str(crf)
463 x264_addline=configreader.getenv("x264_addline")
466 xtt=configreader.getenv("x264_thread")
472 x264_addline=configreader.getenv("x264_addline")
473 if get_x264core_version()>103:
474 x264res=u"--input-res "+x264streamsize
475 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
477 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