3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2011 Yukikaze
17 def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
19 pinで指定されたファイルをpoutにx264でエンコードして書き出す
26 quality=int(configreader.getConfEnv("x264_preset"))
27 crf=int(configreader.getConfEnv("crf"))
31 if re.search("H", opts):
33 if re.search("S", opts):
35 if re.search("F", opts):
37 if re.search("W",opts):
39 if re.search("MW1",opts):
43 if re.search("MW2", opts):
47 if re.search("v", opts):
49 crf=int(configreader.getConfEnv("animation_crf"))
50 if re.search("a", opts):
52 crf=int(configreader.getConfEnv("animation_crf"))
53 if re.search("I", opts):
55 if re.search("B", opts):
58 if re.search("q",opts):
60 if re.search("w",opts):
62 if re.search("e",opts):
64 if re.search("r",opts):
66 if re.search("u",opts):
68 if re.search("i",opts):
70 if re.search("o",opts):
72 if re.search("p",opts):
74 if re.search("d",opts):#二カ国語放送の場合
75 tm2v=pin.replace(".ts",".m2v")
76 encodeFfmpegSar(tm2v,pout,size,is24fps,quality,crf,deinterlace=deinterlace,usebluray=bluray,usesar=sar)
77 elif re.search("5",opts):#5.1chの場合
78 encodeFfmpegSar(pin,pout,size,is24fps,quality,crf,deinterlace=deinterlace,usebluray=bluray,usesar=sar)
79 elif re.search("b",opts):#BonTsDemuxを使いたい場合
80 tm2v=pin.replace(".ts",".m2v")
81 encodeFfmpegSar(tm2v,pout,size,is24fps,quality,crf,deinterlace=deinterlace,usebluray=bluray,usesar=sar)
84 encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=1,usebluray=bluray,usesar=sar)
85 if os.path.exists(pout):
86 if not os.path.getsize(pout)>1*1000:
87 recdblist.addCommonlogEX("Warning", "Old mencoder option used.","","",log_level=200)
89 encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=0,usebluray=bluray,usesar=sar)
90 except Exception, inst:
91 recdblist.addCommonlogEX("Error", "ts2x264_mencoder_old(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
93 recdblist.addCommonlogEX("Warning", "Old mencoder option used.","","",log_level=200)
95 encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=0,usebluray=bluray,usesar=sar)
96 except Exception, inst:
97 recdblist.addCommonlogEX("Error", "ts2x264_mencoder_old(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
98 except Exception, inst:
99 recdblist.addCommonlogEX("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
100 def encodeMencoderSar(pin,pout,size,is24fps,quality,crf,deinterlace=1,uselavf=0,usebluray=0,usesar=1):
101 mencoder=configreader.getConfPath("mencoder")
102 tee=configreader.getConfPath("tee")
105 encvf="-sws 9 -vf yadif=0,pp=l5"
106 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
107 ofps="-ofps 30000/1001"
108 fps="-fps 30000/1001"
109 x264fps="30000/1001 --keyint 30"
116 tsar=getMoviePAR2(pin)
118 x264sar=str(tsar[0])+u":"+str(tsar[1])
121 ofps="-ofps 24000/1001"
122 fps="-fps 30000/1001"
123 x264fps="24000/1001 --keyint 24"
124 x264tune="--tune animation"
125 encvf="-sws 9 -vf pullup,softskip"
126 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
128 tsize=getParSize(pin,720)
130 encvf = encvf + ",scale=-3:720::0:3"+harddup
131 x264streamsize=str(tsize[0])+u"x720"
133 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
134 x264streamsize=u"1280x720"
137 tsize=getParSize(pin,480)
139 encvf = encvf + ",scale=-3:480::0:3"+harddup
140 x264streamsize=str(tsize[0])+u"x480"
142 encvf = encvf + ",scale=-2:480::0:3,expand=854:480"+harddup
143 x264streamsize=u"854x480"
145 elif size == "FullHD":
146 tsize=getParSize(pin,1080)
148 encvf = encvf + ",scale=-3:1080::0:3"+harddup
149 x264streamsize=str(tsize[0])+u"x1080"
151 encvf = encvf + ",scale=-2:1080::0:3,expand=1920:1080"+harddup
152 x264streamsize=u"1920x1080"
154 elif size == "QVGA_BASE":
155 tsize=getParSize(pin,240)
157 encvf = encvf + ",scale=-3:240::0:3"+harddup
158 x264streamsize=str(tsize[0])+u"x240"
160 encvf = encvf + ",scale=320:-2::0:3,expand=320:240"+harddup
161 x264streamsize=u"320x240"
163 elif size == "WVGA_BASE":
164 tsize=getParSize(pin,480)
166 encvf = encvf + ",scale=-3:480::0:3"+harddup
167 x264streamsize=str(tsize[0])+u"x480"
169 encvf = encvf + ",scale=-2:480::0:3,expand=854:480"+harddup
170 x264streamsize=u"854x480"
173 tsize=getParSize(pin,720)
175 encvf = encvf + ",scale=-3:720::0:3"+harddup
176 x264streamsize=str(tsize[0])+u"x720"
178 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
179 x264streamsize=u"1280x720"
182 tsize=getMovieBaseSize2(pin)
183 ofps="-ofps 30000/1001"
184 #fps="-fps 30000/1001"
186 x264fps="30000/1001 --keyint 30"
188 encvf="-vf hqdn3d=2:1:2"
191 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
193 encvf = u"-sws 9 "+encvf + ",scale=-2:1080::0:3,expand=1920:1080"+harddup
194 x264streamsize=u"1920x1080"
195 if tsize[0] <= 0 or tsize[1] <= 0:
196 encvf="-sws 9 -vf yadif=0,pp=l5"
197 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
198 ofps="-ofps 30000/1001"
199 fps="-fps 30000/1001"
200 x264fps="30000/1001 --keyint 30"
202 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
203 x264streamsize=u"1280x720"
206 x264preset=u"ultrafast"
208 x264preset=u"veryfast"
219 if size == "WVGA_BASE" or size == "QVGA_BASE":
220 x264profile=" --level 32 --profile baseline "
222 x264profile=" --level 41 --profile high "
224 lavft="-demuxer lavf "
226 os.environ['LANG']="ja_JP.UTF-8"
229 temptime=int(time.time())
230 temptime=temptime % 9697
231 random.jumpahead(temptime)
232 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))
233 unicode(commands.getoutput(u"mkfifo "+streampath),'utf-8','ignore')
234 logmencoder=recdblist.getLogTitle(pin)+".mencoder.log"
235 logx264=recdblist.getLogTitle(pin)+".x264.log"
236 encexe=mencoder+u" \""+pin+u"\" -quiet -vfm ffmpeg "+lavft+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" 2>&1 | "+tee+" \""+logmencoder+"\" & "
237 encexe=encexe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf,interlaced=x264interlaced,bluray=usebluray)
238 #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
239 encexe=u"nice -n 19 " +encexe+" 2>&1 | "+tee+" \""+logx264+"\""
241 recdblist.addCommandSelfLog(pin, encexe)
242 except Exception, inst:
245 print traceback.print_exc(file=sys.stdout)
246 recdblist.printutf8(encexe)
249 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8','ignore')
252 os.remove(streampath)
253 zip.addFile2FileZip(logmencoder, recdblist.getLogTitle(pin)+".log.zip")
254 zip.addFile2FileZip(logx264, recdblist.getLogTitle(pin)+".log.zip")
255 os.remove(logmencoder)
257 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
258 # recdblist.addCommandLogTar(pin,u"Mencoder","mencoder", encexe,txt)
259 def encodeFfmpegSar(pin,pout,size,is24fps,quality,crf,deinterlace=1,usebluray=0,usesar=1):
263 ffmpeg=configreader.getConfPath("ffmpeg")
264 tee=configreader.getConfPath("tee")
266 x264fps="30000/1001 --keyint 30"
273 tsar=getMoviePAR2(pin)
274 filter="-deinterlace"
275 x264sar=str(tsar[0])+":"+str(tsar[1])
278 x264fps="24000/1001 --keyint 24"
279 x264tune="--tune animation"
281 tsize=getParSize(pin,720)
282 s = "-s "+str(tsize[0])+"x720 "
283 x264streamsize=str(tsize[0])+u"x720"
286 tsize=getParSize(pin,480)
287 s = "-s "+str(tsize[0])+"x480 "
288 x264streamsize=str(tsize[0])+u"x480"
290 elif size == "FullHD":
291 tsize=getParSize(pin,1080)
292 s = "-s "+str(tsize[0])+"x1080 "
293 x264streamsize=str(tsize[0])+u"x1080"
296 tsize=getParSize(pin,480)
297 s = "-s "+str(tsize[0])+"x480 "
298 x264streamsize=str(tsize[0])+u"x480"
300 elif size == "QVGA_BASE":
301 tsize=getParSize(pin,240)
302 s = "-s "+str(tsize[0])+"x240 "
303 x264streamsize=str(tsize[0])+u"x240"
305 elif size == "WVGA_BASE":
306 tsize=getParSize(pin,480)
307 s = "-s "+str(tsize[0])+"x480 "
308 x264streamsize=str(tsize[0])+u"x480"
311 tsize=getParSize(pin,720)
312 s = "-s "+str(tsize[0])+"x720 "
313 x264streamsize=str(tsize[0])+u"x720"
316 tsize=getMovieBaseSize2(pin)
318 s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "
319 x264fps="30000/1001 --keyint 30"
322 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
324 x264preset=u"ultrafast"
326 x264preset=u"veryfast"
337 if size == "WVGA_BASE" or size == "QVGA_BASE":
338 x264profile=" --level 32 --profile baseline "
340 x264profile=" --level 41 --profile high "
343 os.environ['LANG']="ja_JP.UTF-8"
344 log_ffmpeg=recdblist.getLogTitle(pin)+".ffmpeg.log"
345 log_x264=recdblist.getLogTitle(pin)+".x264.log"
346 exe=ffmpeg+u" -y -i \""+pin+"\" -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - 2> \""+log_ffmpeg+"\" | "
347 exe=exe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf,interlaced=x264interlaced,bluray=usebluray)
348 exe = "nice -n 19 " + exe+" 2>&1 | "+tee+" \""+log_x264+"\""
350 #recdblist.addCommandSelfLog(log_ffmpeg,recdblist.getLogTitle(pin)+".log.zip")
351 #recdblist.addCommandSelfLog(log_x264,recdblist.getLogTitle(pin)+".log.zip")
352 recdblist.printutf8(exe)
353 recdblist.addCommandSelfLog(pin,exe)
355 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
358 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
359 zip.addFile2FileZip(log_ffmpeg, recdblist.getLogTitle(pin+".log.zip"))
360 zip.addFile2FileZip(log_x264, recdblist.getLogTitle(pin+".log.zip"))
361 os.remove(log_ffmpeg)
363 def getMovieBaseSize2(pin):##動画開始後すぐに解像度が変更されたときに対処
364 ffmpeg=configreader.getConfPath("ffmpeg")
365 os.environ['LANG']="ja_JP.UTF-8"
366 exe=ffmpeg+u" -ss 5 -fs 1 -t 10 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
368 recdblist.addCommandSelfLog(pin, exe)
369 except Exception, inst:
372 print traceback.print_exc(file=sys.stdout)
373 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
374 rT=re.compile(u"Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
375 rT2=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+SAR\D+(\d+:\d+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
378 txtls=txts.split("\n")
385 tX=int(sizetxt.split("x")[0])
386 tY=int(sizetxt.split("x")[1])
387 tEX=int(partxt.split(":")[0])
388 tEY=int(partxt.split(":")[1])
394 tX=int(sizetxt.split("x")[0])
395 tY=int(sizetxt.split("x")[1])
399 os.remove(pin+".size.ts")
400 return [sizeMaxX,sizeMaxY]
401 def getMoviePAR2(pin):
402 ffmpeg=configreader.getConfPath("ffmpeg")
403 os.environ['LANG']="ja_JP.UTF-8"
404 exe=ffmpeg+u" -ss 5 -fs 1 -t 10 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
406 recdblist.addCommandSelfLog(pin, exe)
407 except Exception, inst:
410 print traceback.print_exc(file=sys.stdout)
411 txts = unicode(commands.getoutput(exe.encode('utf-8')), 'utf-8', 'ignore')
412 rT = re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
413 rT2 = re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+SAR\D+(\d+:\d+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
417 txtls = txts.split("\n")
422 sizetxt = rM.group(1)
424 tX = int(sizetxt.split("x")[0])
425 tY = int(sizetxt.split("x")[1])
426 tEX = int(dartxt.split(":")[0])
427 tEY = int(dartxt.split(":")[1])
430 if tX == 1920 and tY == 1080:
438 sizetxt = rM2.group(1)
439 sartxt = rM2.group(2)
440 dartxt = rM2.group(3)
441 tX = int(sizetxt.split("x")[0])
442 tY = int(sizetxt.split("x")[1])
443 tEX = int(dartxt.split(":")[0])
444 tEY = int(dartxt.split(":")[1])##dar=sarxpar
445 tSX = int(sartxt.split(":")[0])
446 tSY = int(sartxt.split(":")[1])##par=dar/sar= darx*pary*sary=dary*parx*sarx
449 if tX == 1920 and tY == 1080:
453 if tEX * tSY == tEY * tSX:
456 elif tEX * tSY * 3 == tEY * tSX * 4:
459 elif tEX * tSY * 4 == tEY * tSX * 3:
462 elif tEX * tSY * 9 == tEY * tSX * 16:
465 elif tEX * tSY * 16 == tEY * tSX * 9:
468 os.remove(pin+".size.ts")
470 def getParSize(pin,y):
471 tSize=getMovieBaseSize2(pin)
473 tX=tSize[0]*10*y/tSize[1]
483 def getX264CoreVersion():
484 x264=configreader.getConfPath("x264")
486 t1=commands.getoutput(x264+" --help|grep core")
488 rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
494 def getX264Commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=-1,bitrate=0,interlaced=0,bluray=0):
495 x264=configreader.getConfPath("x264")
496 os.environ['LANG']="ja_JP.UTF-8"
497 x264_sar="--sar "+sar
498 x264preset=u"--preset "+preset
502 x264_addline=configreader.getConfEnv("x264_addline")
504 x264bitrate=u"--bitrate "+str(bitrate)
506 x264crf=u"--crf "+str(crf)
508 x264interlaced="--tff --nal-hrd vbr"
510 if getX264CoreVersion()<115:
511 x264bluray=" --weightp 1 --nal-hrd vbr --bframes 3 --b-pyramid none --open-gop bluray --slices 4 --aud --colorprim bt709 --transfer bt709 "
513 if fps=="24000/1001 --keyint 24" or fps =="24000/1001":
514 if x264streamsize=="1280x720":
515 x264bluray=u"--ref 6 "+x264bluray
516 if x264streamsize=="1920x1080":
517 x264bluray=u"--ref 4 "+x264bluray
518 elif fps=="30000/1001 --keyint 30" or fps =="30000/1001":
519 if x264streamsize=="1280x720":
520 x264bluray=u"--ref 6 --pulldown double "+x264bluray
521 if x264streamsize=="1920x1080":
523 x264bluray=u"--ref 4 --fake-interlaced --pic-struct "+x264bluray
525 x264bluray=u"--ref 4 "+x264bluray
526 elif getX264CoreVersion()>=115:
527 x264bluray=" --slices 4 --bluray-compat --nal-hrd vbr --bframes 3 --b-pyramid strict --aud --colorprim bt709 --transfer bt709 "
529 if fps=="24000/1001 --keyint 24" or fps =="24000/1001":
530 if x264streamsize=="1280x720":
531 x264bluray=u"--ref 6 "+x264bluray
532 if x264streamsize=="1920x1080":
533 x264bluray=u"--ref 4 "+x264bluray
534 elif fps=="30000/1001 --keyint 30" or fps =="30000/1001":
535 if x264streamsize=="1280x720":
536 x264bluray=u"--ref 6 --pulldown double "+x264bluray
537 if x264streamsize=="1920x1080":
539 x264bluray=u"--ref 4 --fake-interlaced --pic-struct "+x264bluray
541 x264bluray=u"--ref 4 "+x264bluray
544 xtt=configreader.getConfEnv("x264_thread")
550 x264_addline=configreader.getConfEnv("x264_addline")
551 if getX264CoreVersion()>103:
552 x264res=u"--input-res "+x264streamsize
553 exe=u"nice -n 19 "+x264+" --demuxer raw --vbv-maxrate 30000 --vbv-bufsize 25000 "+x264_sar+" "+x264crf+u" "+x264bluray+u" "+x264interlaced+u" "+x264_addline+u" --colormatrix bt709 --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" "+x264res+" -o \""+pout+"\" "+pin
555 exe=u"nice -n 19 "+x264+" --demuxer raw --vbv-maxrate 30000 --vbv-bufsize 25000 "+x264_sar+" "+x264crf+u" "+x264bluray+u" "+x264interlaced+u" "+x264_addline+u" --colormatrix bt709 --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" "+pin+" "+x264streamsize