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
28 if re.search("H", opts):
\r
30 if re.search("S", opts):
\r
32 if re.search("F", opts):
\r
34 if re.search("W",opts):
\r
36 if re.search("MW1",opts):
\r
39 if re.search("MW2", opts):
\r
42 if re.search("v", opts):
\r
44 crf=int(configreader.getConfEnv("animation_crf"))
\r
45 if re.search("a", opts):
\r
47 crf=int(configreader.getConfEnv("animation_crf"))
\r
48 if re.search("I", opts):
\r
50 if re.search("q",opts):
\r
52 if re.search("w",opts):
\r
54 if re.search("e",opts):
\r
56 if re.search("r",opts):
\r
58 if re.search("u",opts):
\r
60 if re.search("i",opts):
\r
62 if re.search("o",opts):
\r
64 if re.search("p",opts):
\r
66 if re.search("d",opts):#二カ国語放送の場合
\r
67 tm2v=pin.replace(".ts",".m2v")
\r
68 encodeFfmpegSar(tm2v,pout,size,is24fps,quality,crf,deinterlace)
\r
69 elif re.search("5",opts):#5.1chの場合
\r
70 encodeFfmpegSar(pin,pout,size,is24fps,quality,crf,deinterlace)
\r
71 elif re.search("0",opts):#BonTsDemuxを使いたい場合
\r
72 tm2v=pin.replace(".ts",".m2v")
\r
73 encodeFfmpegSar(tm2v,pout,size,is24fps,quality,crf,deinterlace)
\r
76 encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=1)
\r
77 if os.path.exists(pout):
\r
78 if not os.path.getsize(pout)>1*1000:
\r
79 recdblist.addCommonlogEX("Warning", "Old mencoder option used.","","",log_level=200)
\r
81 encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=0)
\r
82 except Exception, inst:
\r
83 recdblist.addCommonlogEX("Error", "ts2x264_mencoder_old(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
\r
85 recdblist.addCommonlogEX("Warning", "Old mencoder option used.","","",log_level=200)
\r
87 encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=0)
\r
88 except Exception, inst:
\r
89 recdblist.addCommonlogEX("Error", "ts2x264_mencoder_old(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
\r
90 except Exception, inst:
\r
91 recdblist.addCommonlogEX("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
\r
92 def encodeMencoderSar(pin,pout,size,is24fps,quality,crf,deinterlace=1,uselavf=0):
\r
93 mencoder=configreader.getConfPath("mencoder")
\r
94 tee=configreader.getConfPath("tee")
\r
97 encvf="-sws 9 -vf yadif=0,pp=l5"
\r
98 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
\r
99 ofps="-ofps 30000/1001"
\r
100 fps="-fps 30000/1001"
\r
101 x264fps="30000/1001"
\r
105 x264_bitrate="5000"
\r
106 tsar=getMoviePAR2(pin)
\r
107 x264sar=str(tsar[0])+":"+str(tsar[1])
\r
110 ofps="-ofps 24000/1001"
\r
111 fps="-fps 30000/1001"
\r
112 x264fps="24000/1001"
\r
113 x264tune="--tune animation"
\r
114 encvf="-sws 9 -vf pullup,softskip"
\r
115 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
\r
117 tsize=getParSize(pin,720)
\r
118 encvf = encvf + ",scale=-3:720::0:3"+harddup
\r
119 x264streamsize=str(tsize[0])+u"x720"
\r
120 x264_bitrate="2500"
\r
121 elif size == "WVGA":
\r
122 tsize=getParSize(pin,480)
\r
123 encvf = encvf + ",scale=-3:480::0:3"+harddup
\r
124 x264streamsize=str(tsize[0])+u"x480"
\r
125 x264_bitrate="1500"
\r
126 elif size == "FullHD":
\r
127 tsize=getParSize(pin,1080)
\r
128 encvf = encvf + ",scale=-3:1080::0:3"+harddup
\r
129 x264streamsize=str(tsize[0])+u"x1080"
\r
130 x264_bitrate="5000"
\r
131 elif size == "QVGA_BASE":
\r
132 tsize=getParSize(pin,240)
\r
133 encvf = encvf + ",scale=-3:240::0:3"+harddup
\r
134 x264streamsize=str(tsize[0])+u"x240"
\r
136 elif size == "WVGA_BASE":
\r
137 tsize=getParSize(pin,480)
\r
138 encvf = encvf + ",scale=-3:480::0:3"+harddup
\r
139 x264streamsize=str(tsize[0])+u"x480"
\r
140 x264_bitrate="1500"
\r
142 tsize=getParSize(pin,720)
\r
143 encvf = encvf + ",scale=-3:720::0:3"+harddup
\r
144 x264streamsize=str(tsize[0])+u"x720"
\r
145 x264_bitrate="2500"
\r
147 tsize=getMovieBaseSize2(pin)
\r
148 ofps="-ofps 30000/1001"
\r
149 #fps="-fps 30000/1001"
\r
151 x264fps="30000/1001"
\r
152 x264tune=x264tune+" --tff --nal-hrd vbr"
\r
153 encvf="-vf hqdn3d=2:1:2"
\r
155 encvf=encvf+harddup
\r
156 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
\r
157 if tsize[0] <= 0 or tsize[1] <= 0:
\r
158 encvf="-sws 9 -vf yadif=0,pp=l5"
\r
159 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
\r
160 ofps="-ofps 30000/1001"
\r
161 fps="-fps 30000/1001"
\r
162 x264fps="30000/1001"
\r
164 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
\r
165 x264streamsize=u"1280x720"
\r
166 x264_bitrate="2500"
\r
168 x264preset=u"ultrafast"
\r
170 x264preset=u"veryfast"
\r
174 x264preset=u"medium"
\r
178 x264preset=u"slower"
\r
180 x264preset=u"medium"
\r
181 if size == "WVGA_BASE" or size == "QVGA_BASE":
\r
182 x264profile=" --level 32 --profile baseline "
\r
184 x264profile=" --level 41 --profile high "
\r
186 lavft="-demuxer lavf "
\r
188 os.environ['LANG']="ja_JP.UTF-8"
\r
190 random.jumpahead(10)
\r
191 temptime=int(time.time())
\r
192 temptime=temptime % 9697
\r
193 random.jumpahead(temptime)
\r
194 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))
\r
195 unicode(commands.getoutput(u"mkfifo "+streampath),'utf-8','ignore')
\r
196 logmencoder=recdblist.getLogTitle(pin)+".mencoder.log"
\r
197 logx264=recdblist.getLogTitle(pin)+".x264.log"
\r
198 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
199 encexe=encexe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf)
\r
200 #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
201 encexe=u"nice -n 19 " +encexe+" 2>&1 | "+tee+" \""+logx264+"\""
\r
203 recdblist.addCommandSelfLog(pin, encexe)
\r
204 except Exception, inst:
\r
207 print traceback.print_exc(file=sys.stdout)
\r
208 recdblist.printutf8(encexe)
\r
211 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8','ignore')
\r
214 os.remove(streampath)
\r
215 zip.addFile2FileZip(logmencoder, recdblist.getLogTitle(pin)+".log.zip")
\r
216 zip.addFile2FileZip(logx264, recdblist.getLogTitle(pin)+".log.zip")
\r
217 os.remove(logmencoder)
\r
219 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
\r
220 # recdblist.addCommandLogTar(pin,u"Mencoder","mencoder", encexe,txt)
\r
221 def encodeFfmpegSar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
\r
225 ffmpeg=configreader.getConfPath("ffmpeg")
\r
226 tee=configreader.getConfPath("tee")
\r
227 fps=u"-r 29.970030 "
\r
228 x264fps="30000/1001"
\r
232 x264_bitrate="2500"
\r
234 tsar=getMoviePAR2(pin)
\r
235 filter="-deinterlace"
\r
236 x264sar=str(tsar[0])+":"+str(tsar[1])
\r
238 fps=u"-r 23.976023 "
\r
239 x264fps="24000/1001"
\r
240 x264tune="--tune animation"
\r
242 tsize=getParSize(pin,720)
\r
243 s = "-s "+str(tsize[0])+"x720 "
\r
244 x264streamsize=str(tsize[0])+u"x720"
\r
245 x264_bitrate="2500"
\r
246 elif size == "WVGA":
\r
247 tsize=getParSize(pin,480)
\r
248 s = "-s "+str(tsize[0])+"x480 "
\r
249 x264streamsize=str(tsize[0])+u"x480"
\r
250 x264_bitrate="1500"
\r
251 elif size == "FullHD":
\r
252 tsize=getParSize(pin,1080)
\r
253 s = "-s "+str(tsize[0])+"x1080 "
\r
254 x264streamsize=str(tsize[0])+u"x1080"
\r
255 x264_bitrate="5000"
\r
257 tsize=getParSize(pin,480)
\r
258 s = "-s "+str(tsize[0])+"x480 "
\r
259 x264streamsize=str(tsize[0])+u"x480"
\r
260 x264_bitrate="1250"
\r
261 elif size == "QVGA_BASE":
\r
262 tsize=getParSize(pin,240)
\r
263 s = "-s "+str(tsize[0])+"x240 "
\r
264 x264streamsize=str(tsize[0])+u"x240"
\r
266 elif size == "WVGA_BASE":
\r
267 tsize=getParSize(pin,480)
\r
268 s = "-s "+str(tsize[0])+"x480 "
\r
269 x264streamsize=str(tsize[0])+u"x480"
\r
270 x264_bitrate="1500"
\r
272 tsize=getParSize(pin,720)
\r
273 s = "-s "+str(tsize[0])+"x720 "
\r
274 x264streamsize=str(tsize[0])+u"x720"
\r
275 x264_bitrate="2500"
\r
277 tsize=getMovieBaseSize2(pin)
\r
279 s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "
\r
280 x264fps="30000/1001"
\r
281 x264tune=x264tune+" --tff --nal-hrd vbr"
\r
283 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
\r
285 x264preset=u"ultrafast"
\r
287 x264preset=u"veryfast"
\r
291 x264preset=u"medium"
\r
295 x264preset=u"slower"
\r
297 x264preset=u"medium"
\r
298 if size == "WVGA_BASE" or size == "QVGA_BASE":
\r
299 x264profile=" --level 32 --profile baseline "
\r
301 x264profile=" --level 41 --profile high "
\r
304 os.environ['LANG']="ja_JP.UTF-8"
\r
305 log_ffmpeg=recdblist.getLogTitle(pin)+".ffmpeg.log"
\r
306 log_x264=recdblist.getLogTitle(pin)+".x264.log"
\r
307 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - 2> \""+log_ffmpeg+"\" | "
\r
308 exe=exe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)
\r
309 exe = "nice -n 19 " + exe+" 2>&1 | "+tee+" \""+log_x264+"\""
\r
311 #recdblist.addCommandSelfLog(log_ffmpeg,recdblist.getLogTitle(pin)+".log.zip")
\r
312 #recdblist.addCommandSelfLog(log_x264,recdblist.getLogTitle(pin)+".log.zip")
\r
313 recdblist.printutf8(exe)
\r
314 recdblist.addCommandSelfLog(pin,exe)
\r
316 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
319 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
\r
320 zip.addFile2FileZip(log_ffmpeg, recdblist.getLogTitle(pin+".log.zip"))
\r
321 zip.addFile2FileZip(log_x264, recdblist.getLogTitle(pin+".log.zip"))
\r
322 os.remove(log_ffmpeg)
\r
323 os.remove(log_x264)
\r
324 def getMovieBaseSize(pin):
\r
325 ffmpeg=configreader.getConfPath("ffmpeg")
\r
326 os.environ['LANG']="ja_JP.UTF-8"
\r
327 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
\r
328 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
329 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
332 txtls=txts.split("\n")
\r
336 sizetxt=rM.group(1)
\r
338 tX=int(sizetxt.split("x")[0])
\r
339 tY=int(sizetxt.split("x")[1])
\r
340 tEX=int(partxt.split(":")[0])
\r
341 tEY=int(partxt.split(":")[1])
\r
345 return [sizeMaxX,sizeMaxY]
\r
346 def getMovieBaseSize2(pin):##動画開始後すぐに解像度が変更されたときに対処
\r
347 ffmpeg=configreader.getConfPath("ffmpeg")
\r
348 os.environ['LANG']="ja_JP.UTF-8"
\r
349 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
\r
350 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
351 rT=re.compile(u"Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
354 txtls=txts.split("\n")
\r
358 sizetxt=rM.group(1)
\r
360 tX=int(sizetxt.split("x")[0])
\r
361 tY=int(sizetxt.split("x")[1])
\r
362 tEX=int(partxt.split(":")[0])
\r
363 tEY=int(partxt.split(":")[1])
\r
367 os.remove(pin+".size.ts")
\r
368 return [sizeMaxX,sizeMaxY]
\r
369 def getMoviePAR(pin):
\r
370 ffmpeg=configreader.getConfPath("ffmpeg")
\r
371 os.environ['LANG']="ja_JP.UTF-8"
\r
372 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
\r
373 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
374 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
378 txtls=txts.split("\n")
\r
382 sizetxt=rM.group(1)
\r
384 tX=int(sizetxt.split("x")[0])
\r
385 tY=int(sizetxt.split("x")[1])
\r
386 tEX=int(dartxt.split(":")[0])
\r
387 tEY=int(dartxt.split(":")[1])
\r
390 if tX==1920 and tY==1080:
\r
397 def getMoviePAR2(pin):
\r
398 ffmpeg=configreader.getConfPath("ffmpeg")
\r
399 os.environ['LANG']="ja_JP.UTF-8"
\r
400 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
\r
401 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
402 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
406 txtls=txts.split("\n")
\r
410 sizetxt=rM.group(1)
\r
412 tX=int(sizetxt.split("x")[0])
\r
413 tY=int(sizetxt.split("x")[1])
\r
414 tEX=int(dartxt.split(":")[0])
\r
415 tEY=int(dartxt.split(":")[1])
\r
418 if tX==1920 and tY==1080:
\r
424 os.remove(pin+".size.ts")
\r
426 def getMovieDAR(pin):
\r
427 ffmpeg=configreader.getConfPath("ffmpeg")
\r
428 os.environ['LANG']="ja_JP.UTF-8"
\r
429 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
\r
430 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
431 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
\r
435 txtls=txts.split("\n")
\r
439 sizetxt=rM.group(1)
\r
441 tX=int(sizetxt.split("x")[0])
\r
442 tY=int(sizetxt.split("x")[1])
\r
443 tEX=int(dartxt.split(":")[0])
\r
444 tEY=int(dartxt.split(":")[1])
\r
447 if tX==1920 and tY==1080:
\r
454 def getMovieDAR2(pin):
\r
455 ffmpeg=configreader.getConfPath("ffmpeg")
\r
456 os.environ['LANG']="ja_JP.UTF-8"
\r
457 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
\r
458 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
459 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
\r
463 txtls=txts.split("\n")
\r
467 sizetxt=rM.group(1)
\r
469 tX=int(sizetxt.split("x")[0])
\r
470 tY=int(sizetxt.split("x")[1])
\r
471 tEX=int(dartxt.split(":")[0])
\r
472 tEY=int(dartxt.split(":")[1])
\r
475 if tX==1920 and tY==1080:
\r
481 os.remove(pin+".size.ts")
\r
483 def getParSize(pin,y):
\r
484 tSize=getMovieBaseSize2(pin)
\r
486 tX=tSize[0]*10*y/tSize[1]
\r
488 if tX>int(tX/10)*10:
\r
496 def getX264CoreVersion():
\r
497 x264=configreader.getConfPath("x264")
\r
498 t1=commands.getoutput(x264+" --help|grep core")
\r
499 rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
\r
505 def getX264Commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=-1,bitrate=0):
\r
506 x264=configreader.getConfPath("x264")
\r
507 os.environ['LANG']="ja_JP.UTF-8"
\r
508 x264_sar="--sar "+sar
\r
509 x264preset=u"--preset "+preset
\r
510 x264fps="--fps "+fps
\r
512 x264bitrate=u"--bitrate "+str(bitrate)
\r
514 x264crf=u"--crf "+str(crf)
\r
515 x264_addline=configreader.getConfEnv("x264_addline")
\r
518 xtt=configreader.getConfEnv("x264_thread")
\r
521 x264_thread=str(xtt)
\r
524 x264_addline=configreader.getConfEnv("x264_addline")
\r
525 if getX264CoreVersion()>103:
\r
526 x264res=u"--input-res "+x264streamsize
\r
527 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
\r
529 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
\r