3 # Rec10 TS Recording Tools
\r
4 # Copyright (C) 2009-2011 Yukikaze
\r
16 def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
\r
18 pinで指定されたファイルをpoutにx264でエンコードして書き出す
\r
25 quality=int(configreader.getConfEnv("x264_preset"))
\r
26 crf=int(configreader.getConfEnv("crf"))
\r
30 if re.search("H", opts):
\r
32 if re.search("S", opts):
\r
34 if re.search("F", opts):
\r
36 if re.search("W",opts):
\r
38 if re.search("MW1",opts):
\r
41 if re.search("MW2", opts):
\r
44 if re.search("v", opts):
\r
46 crf=int(configreader.getConfEnv("animation_crf"))
\r
47 if re.search("a", opts):
\r
49 crf=int(configreader.getConfEnv("animation_crf"))
\r
50 if re.search("I", opts):
\r
52 if re.search("B", opts):
\r
55 if re.search("q",opts):
\r
57 if re.search("w",opts):
\r
59 if re.search("e",opts):
\r
61 if re.search("r",opts):
\r
63 if re.search("u",opts):
\r
65 if re.search("i",opts):
\r
67 if re.search("o",opts):
\r
69 if re.search("p",opts):
\r
71 if re.search("d",opts):#二カ国語放送の場合
\r
72 tm2v=pin.replace(".ts",".m2v")
\r
73 encodeFfmpegSar(tm2v,pout,size,is24fps,quality,crf,deinterlace=deinterlace,usebluray=bluray,usesar=sar)
\r
74 elif re.search("5",opts):#5.1chの場合
\r
75 encodeFfmpegSar(pin,pout,size,is24fps,quality,crf,deinterlace=deinterlace,usebluray=bluray,usesar=sar)
\r
76 elif re.search("b",opts):#BonTsDemuxを使いたい場合
\r
77 tm2v=pin.replace(".ts",".m2v")
\r
78 encodeFfmpegSar(tm2v,pout,size,is24fps,quality,crf,deinterlace=deinterlace,usebluray=bluray,usesar=sar)
\r
81 encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=1,usebluray=bluray,usesar=sar)
\r
82 if os.path.exists(pout):
\r
83 if not os.path.getsize(pout)>1*1000:
\r
84 recdblist.addCommonlogEX("Warning", "Old mencoder option used.","","",log_level=200)
\r
86 encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=0,usebluray=bluray,usesar=sar)
\r
87 except Exception, inst:
\r
88 recdblist.addCommonlogEX("Error", "ts2x264_mencoder_old(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
\r
90 recdblist.addCommonlogEX("Warning", "Old mencoder option used.","","",log_level=200)
\r
92 encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=0,usebluray=bluray,usesar=sar)
\r
93 except Exception, inst:
\r
94 recdblist.addCommonlogEX("Error", "ts2x264_mencoder_old(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
\r
95 except Exception, inst:
\r
96 recdblist.addCommonlogEX("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
\r
97 def encodeMencoderSar(pin,pout,size,is24fps,quality,crf,deinterlace=1,uselavf=0,usebluray=0,usesar=1):
\r
98 mencoder=configreader.getConfPath("mencoder")
\r
99 tee=configreader.getConfPath("tee")
\r
102 encvf="-sws 9 -vf yadif=0,pp=l5"
\r
103 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
\r
104 ofps="-ofps 30000/1001"
\r
105 fps="-fps 30000/1001"
\r
106 x264fps="30000/1001 --keyint 30"
\r
110 x264_bitrate="5000"
\r
113 tsar=getMoviePAR2(pin)
\r
115 x264sar=str(tsar[0])+u":"+str(tsar[1])
\r
118 ofps="-ofps 24000/1001"
\r
119 fps="-fps 30000/1001"
\r
120 x264fps="24000/1001 --keyint 24"
\r
121 x264tune="--tune animation"
\r
122 encvf="-sws 9 -vf pullup,softskip"
\r
123 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
\r
125 tsize=getParSize(pin,720)
\r
127 encvf = encvf + ",scale=-3:720::0:3"+harddup
\r
128 x264streamsize=str(tsize[0])+u"x720"
\r
130 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
\r
131 x264streamsize=u"1280x720"
\r
132 x264_bitrate="2500"
\r
133 elif size == "WVGA":
\r
134 tsize=getParSize(pin,480)
\r
136 encvf = encvf + ",scale=-3:480::0:3"+harddup
\r
137 x264streamsize=str(tsize[0])+u"x480"
\r
139 encvf = encvf + ",scale=-2:480::0:3,expand=854:480"+harddup
\r
140 x264streamsize=u"854x480"
\r
141 x264_bitrate="1500"
\r
142 elif size == "FullHD":
\r
143 tsize=getParSize(pin,1080)
\r
145 encvf = encvf + ",scale=-3:1080::0:3"+harddup
\r
146 x264streamsize=str(tsize[0])+u"x1080"
\r
148 encvf = encvf + ",scale=-2:1080::0:3,expand=1920:1080"+harddup
\r
149 x264streamsize=u"1920x1080"
\r
150 x264_bitrate="5000"
\r
151 elif size == "QVGA_BASE":
\r
152 tsize=getParSize(pin,240)
\r
154 encvf = encvf + ",scale=-3:240::0:3"+harddup
\r
155 x264streamsize=str(tsize[0])+u"x240"
\r
157 encvf = encvf + ",scale=320:-2::0:3,expand=320:240"+harddup
\r
158 x264streamsize=u"320x240"
\r
160 elif size == "WVGA_BASE":
\r
161 tsize=getParSize(pin,480)
\r
163 encvf = encvf + ",scale=-3:480::0:3"+harddup
\r
164 x264streamsize=str(tsize[0])+u"x480"
\r
166 encvf = encvf + ",scale=-2:480::0:3,expand=854:480"+harddup
\r
167 x264streamsize=u"854x480"
\r
168 x264_bitrate="1500"
\r
170 tsize=getParSize(pin,720)
\r
172 encvf = encvf + ",scale=-3:720::0:3"+harddup
\r
173 x264streamsize=str(tsize[0])+u"x720"
\r
175 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
\r
176 x264streamsize=u"1280x720"
\r
177 x264_bitrate="2500"
\r
179 tsize=getMovieBaseSize2(pin)
\r
180 ofps="-ofps 30000/1001"
\r
181 #fps="-fps 30000/1001"
\r
183 x264fps="30000/1001 --keyint 30"
\r
185 encvf="-vf hqdn3d=2:1:2"
\r
187 encvf=encvf+harddup
\r
188 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
\r
189 if tsize[0] <= 0 or tsize[1] <= 0:
\r
190 encvf="-sws 9 -vf yadif=0,pp=l5"
\r
191 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
\r
192 ofps="-ofps 30000/1001"
\r
193 fps="-fps 30000/1001"
\r
194 x264fps="30000/1001 --keyint 30"
\r
196 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
\r
197 x264streamsize=u"1280x720"
\r
198 x264_bitrate="2500"
\r
200 x264preset=u"ultrafast"
\r
202 x264preset=u"veryfast"
\r
206 x264preset=u"medium"
\r
210 x264preset=u"slower"
\r
212 x264preset=u"medium"
\r
213 if size == "WVGA_BASE" or size == "QVGA_BASE":
\r
214 x264profile=" --level 32 --profile baseline "
\r
216 x264profile=" --level 41 --profile high "
\r
218 lavft="-demuxer lavf "
\r
220 os.environ['LANG']="ja_JP.UTF-8"
\r
222 random.jumpahead(10)
\r
223 temptime=int(time.time())
\r
224 temptime=temptime % 9697
\r
225 random.jumpahead(temptime)
\r
226 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))
\r
227 unicode(commands.getoutput(u"mkfifo "+streampath),'utf-8','ignore')
\r
228 logmencoder=recdblist.getLogTitle(pin)+".mencoder.log"
\r
229 logx264=recdblist.getLogTitle(pin)+".x264.log"
\r
230 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+"\" & "
\r
231 encexe=encexe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf,interlaced=x264interlaced,bluray=usebluray)
\r
232 #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
\r
233 encexe=u"nice -n 19 " +encexe+" 2>&1 | "+tee+" \""+logx264+"\""
\r
235 recdblist.addCommandSelfLog(pin, encexe)
\r
236 except Exception, inst:
\r
239 print traceback.print_exc(file=sys.stdout)
\r
240 recdblist.printutf8(encexe)
\r
243 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8','ignore')
\r
246 os.remove(streampath)
\r
247 zip.addFile2FileZip(logmencoder, recdblist.getLogTitle(pin)+".log.zip")
\r
248 zip.addFile2FileZip(logx264, recdblist.getLogTitle(pin)+".log.zip")
\r
249 os.remove(logmencoder)
\r
251 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
\r
252 # recdblist.addCommandLogTar(pin,u"Mencoder","mencoder", encexe,txt)
\r
253 def encodeFfmpegSar(pin,pout,size,is24fps,quality,crf,deinterlace=1,usebluray=0,usesar=1):
\r
257 ffmpeg=configreader.getConfPath("ffmpeg")
\r
258 tee=configreader.getConfPath("tee")
\r
259 fps=u"-r 29.970030 "
\r
260 x264fps="30000/1001 --keyint 30"
\r
264 x264_bitrate="2500"
\r
267 tsar=getMoviePAR2(pin)
\r
268 filter="-deinterlace"
\r
269 x264sar=str(tsar[0])+":"+str(tsar[1])
\r
271 fps=u"-r 23.976023 "
\r
272 x264fps="24000/1001 --keyint 24"
\r
273 x264tune="--tune animation"
\r
275 tsize=getParSize(pin,720)
\r
276 s = "-s "+str(tsize[0])+"x720 "
\r
277 x264streamsize=str(tsize[0])+u"x720"
\r
278 x264_bitrate="2500"
\r
279 elif size == "WVGA":
\r
280 tsize=getParSize(pin,480)
\r
281 s = "-s "+str(tsize[0])+"x480 "
\r
282 x264streamsize=str(tsize[0])+u"x480"
\r
283 x264_bitrate="1500"
\r
284 elif size == "FullHD":
\r
285 tsize=getParSize(pin,1080)
\r
286 s = "-s "+str(tsize[0])+"x1080 "
\r
287 x264streamsize=str(tsize[0])+u"x1080"
\r
288 x264_bitrate="5000"
\r
290 tsize=getParSize(pin,480)
\r
291 s = "-s "+str(tsize[0])+"x480 "
\r
292 x264streamsize=str(tsize[0])+u"x480"
\r
293 x264_bitrate="1250"
\r
294 elif size == "QVGA_BASE":
\r
295 tsize=getParSize(pin,240)
\r
296 s = "-s "+str(tsize[0])+"x240 "
\r
297 x264streamsize=str(tsize[0])+u"x240"
\r
299 elif size == "WVGA_BASE":
\r
300 tsize=getParSize(pin,480)
\r
301 s = "-s "+str(tsize[0])+"x480 "
\r
302 x264streamsize=str(tsize[0])+u"x480"
\r
303 x264_bitrate="1500"
\r
305 tsize=getParSize(pin,720)
\r
306 s = "-s "+str(tsize[0])+"x720 "
\r
307 x264streamsize=str(tsize[0])+u"x720"
\r
308 x264_bitrate="2500"
\r
310 tsize=getMovieBaseSize2(pin)
\r
312 s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "
\r
313 x264fps="30000/1001 --keyint 30"
\r
316 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
\r
318 x264preset=u"ultrafast"
\r
320 x264preset=u"veryfast"
\r
324 x264preset=u"medium"
\r
328 x264preset=u"slower"
\r
330 x264preset=u"medium"
\r
331 if size == "WVGA_BASE" or size == "QVGA_BASE":
\r
332 x264profile=" --level 32 --profile baseline "
\r
334 x264profile=" --level 41 --profile high "
\r
337 os.environ['LANG']="ja_JP.UTF-8"
\r
338 log_ffmpeg=recdblist.getLogTitle(pin)+".ffmpeg.log"
\r
339 log_x264=recdblist.getLogTitle(pin)+".x264.log"
\r
340 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - 2> \""+log_ffmpeg+"\" | "
\r
341 exe=exe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf,interlaced=x264interlaced,bluray=usebluray)
\r
342 exe = "nice -n 19 " + exe+" 2>&1 | "+tee+" \""+log_x264+"\""
\r
344 #recdblist.addCommandSelfLog(log_ffmpeg,recdblist.getLogTitle(pin)+".log.zip")
\r
345 #recdblist.addCommandSelfLog(log_x264,recdblist.getLogTitle(pin)+".log.zip")
\r
346 recdblist.printutf8(exe)
\r
347 recdblist.addCommandSelfLog(pin,exe)
\r
349 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
352 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
\r
353 zip.addFile2FileZip(log_ffmpeg, recdblist.getLogTitle(pin+".log.zip"))
\r
354 zip.addFile2FileZip(log_x264, recdblist.getLogTitle(pin+".log.zip"))
\r
355 os.remove(log_ffmpeg)
\r
356 os.remove(log_x264)
\r
357 def getMovieBaseSize(pin):
\r
358 ffmpeg=configreader.getConfPath("ffmpeg")
\r
359 os.environ['LANG']="ja_JP.UTF-8"
\r
360 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
\r
361 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
362 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
365 txtls=txts.split("\n")
\r
369 sizetxt=rM.group(1)
\r
371 tX=int(sizetxt.split("x")[0])
\r
372 tY=int(sizetxt.split("x")[1])
\r
373 tEX=int(partxt.split(":")[0])
\r
374 tEY=int(partxt.split(":")[1])
\r
378 return [sizeMaxX,sizeMaxY]
\r
379 def getMovieBaseSize2(pin):##動画開始後すぐに解像度が変更されたときに対処
\r
380 ffmpeg=configreader.getConfPath("ffmpeg")
\r
381 os.environ['LANG']="ja_JP.UTF-8"
\r
382 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
\r
383 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
384 rT=re.compile(u"Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
387 txtls=txts.split("\n")
\r
391 sizetxt=rM.group(1)
\r
393 tX=int(sizetxt.split("x")[0])
\r
394 tY=int(sizetxt.split("x")[1])
\r
395 tEX=int(partxt.split(":")[0])
\r
396 tEY=int(partxt.split(":")[1])
\r
400 os.remove(pin+".size.ts")
\r
401 return [sizeMaxX,sizeMaxY]
\r
402 def getMoviePAR(pin):
\r
403 ffmpeg=configreader.getConfPath("ffmpeg")
\r
404 os.environ['LANG']="ja_JP.UTF-8"
\r
405 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
\r
406 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
407 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
411 txtls=txts.split("\n")
\r
415 sizetxt=rM.group(1)
\r
417 tX=int(sizetxt.split("x")[0])
\r
418 tY=int(sizetxt.split("x")[1])
\r
419 tEX=int(dartxt.split(":")[0])
\r
420 tEY=int(dartxt.split(":")[1])
\r
423 if tX==1920 and tY==1080:
\r
430 def getMoviePAR2(pin):
\r
431 ffmpeg=configreader.getConfPath("ffmpeg")
\r
432 os.environ['LANG']="ja_JP.UTF-8"
\r
433 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
\r
434 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
435 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
439 txtls=txts.split("\n")
\r
443 sizetxt=rM.group(1)
\r
445 tX=int(sizetxt.split("x")[0])
\r
446 tY=int(sizetxt.split("x")[1])
\r
447 tEX=int(dartxt.split(":")[0])
\r
448 tEY=int(dartxt.split(":")[1])
\r
451 if tX==1920 and tY==1080:
\r
457 os.remove(pin+".size.ts")
\r
459 def getMovieDAR(pin):
\r
460 ffmpeg=configreader.getConfPath("ffmpeg")
\r
461 os.environ['LANG']="ja_JP.UTF-8"
\r
462 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
\r
463 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
464 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
\r
468 txtls=txts.split("\n")
\r
472 sizetxt=rM.group(1)
\r
474 tX=int(sizetxt.split("x")[0])
\r
475 tY=int(sizetxt.split("x")[1])
\r
476 tEX=int(dartxt.split(":")[0])
\r
477 tEY=int(dartxt.split(":")[1])
\r
480 if tX==1920 and tY==1080:
\r
487 def getMovieDAR2(pin):
\r
488 ffmpeg=configreader.getConfPath("ffmpeg")
\r
489 os.environ['LANG']="ja_JP.UTF-8"
\r
490 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
\r
491 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
492 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
\r
496 txtls=txts.split("\n")
\r
500 sizetxt=rM.group(1)
\r
502 tX=int(sizetxt.split("x")[0])
\r
503 tY=int(sizetxt.split("x")[1])
\r
504 tEX=int(dartxt.split(":")[0])
\r
505 tEY=int(dartxt.split(":")[1])
\r
508 if tX==1920 and tY==1080:
\r
514 os.remove(pin+".size.ts")
\r
516 def getParSize(pin,y):
\r
517 tSize=getMovieBaseSize2(pin)
\r
519 tX=tSize[0]*10*y/tSize[1]
\r
521 if tX>int(tX/10)*10:
\r
529 def getX264CoreVersion():
\r
530 x264=configreader.getConfPath("x264")
\r
531 t1=commands.getoutput(x264+" --help|grep core")
\r
532 rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
\r
538 def getX264Commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=-1,bitrate=0,interlaced=0,bluray=0):
\r
539 x264=configreader.getConfPath("x264")
\r
540 os.environ['LANG']="ja_JP.UTF-8"
\r
541 x264_sar="--sar "+sar
\r
542 x264preset=u"--preset "+preset
\r
543 x264fps="--fps "+fps
\r
546 x264_addline=configreader.getConfEnv("x264_addline")
\r
548 x264bitrate=u"--bitrate "+str(bitrate)
\r
550 x264crf=u"--crf "+str(crf)
\r
552 x264interlaced="--tff --nal-hrd vbr"
\r
554 x264bluray="--weightp 1 --bframes 3 --nal-hrd vbr --b-pyramid none --open-gop bluray --slices 4 --aud --colorprim bt709 --transfer bt709 "
\r
558 xtt=configreader.getConfEnv("x264_thread")
\r
561 x264_thread=str(xtt)
\r
564 x264_addline=configreader.getConfEnv("x264_addline")
\r
565 if getX264CoreVersion()>103:
\r
566 x264res=u"--input-res "+x264streamsize
\r
567 exe=u"nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264bluray+u" "+x264interlaced+u" "+x264_addline+u" --colormatrix bt709 --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" "+x264res+" -o \""+pout+"\" "+pin
\r
569 exe=u"nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264bluray+u" "+x264interlaced+u" "+x264_addline+u" --colormatrix bt709 --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" "+pin+" "+x264streamsize
\r