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
75 encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=1)
\r
76 if os.path.exists(pout):
\r
77 if not os.path.getsize(pout)>1*1000:
\r
78 recdblist.addCommonlogEX("Warning", "Old mencoder option used.","","",log_level=200)
\r
80 encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=0)
\r
81 except Exception, inst:
\r
82 recdblist.addCommonlogEX("Error", "ts2x264_mencoder_old(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
\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)
\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
89 except Exception, inst:
\r
90 recdblist.addCommonlogEX("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
\r
91 def encodeMencoderSar(pin,pout,size,is24fps,quality,crf,deinterlace=1,uselavf=0):
\r
92 mencoder=configreader.getConfPath("mencoder")
\r
93 tee=configreader.getConfPath("tee")
\r
96 encvf="-sws 9 -vf yadif=0,pp=l5"
\r
97 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
\r
98 ofps="-ofps 30000/1001"
\r
99 fps="-fps 30000/1001"
\r
100 x264fps="30000/1001"
\r
104 x264_bitrate="5000"
\r
105 tsar=getMoviePAR2(pin)
\r
106 x264sar=str(tsar[0])+":"+str(tsar[1])
\r
109 ofps="-ofps 24000/1001"
\r
110 fps="-fps 30000/1001"
\r
111 x264fps="24000/1001"
\r
112 x264tune="--tune animation"
\r
113 encvf="-sws 9 -vf pullup,softskip"
\r
114 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
\r
116 tsize=getParSize(pin,720)
\r
117 encvf = encvf + ",scale=-3:720::0:3"+harddup
\r
118 x264streamsize=str(tsize[0])+u"x720"
\r
119 x264_bitrate="2500"
\r
120 elif size == "WVGA":
\r
121 tsize=getParSize(pin,480)
\r
122 encvf = encvf + ",scale=-3:480::0:3"+harddup
\r
123 x264streamsize=str(tsize[0])+u"x480"
\r
124 x264_bitrate="1500"
\r
125 elif size == "FullHD":
\r
126 tsize=getParSize(pin,1080)
\r
127 encvf = encvf + ",scale=-3:1080::0:3"+harddup
\r
128 x264streamsize=str(tsize[0])+u"x1080"
\r
129 x264_bitrate="5000"
\r
130 elif size == "QVGA_BASE":
\r
131 tsize=getParSize(pin,240)
\r
132 encvf = encvf + ",scale=-3:240::0:3"+harddup
\r
133 x264streamsize=str(tsize[0])+u"x240"
\r
135 elif size == "WVGA_BASE":
\r
136 tsize=getParSize(pin,480)
\r
137 encvf = encvf + ",scale=-3:480::0:3"+harddup
\r
138 x264streamsize=str(tsize[0])+u"x480"
\r
139 x264_bitrate="1500"
\r
141 tsize=getParSize(pin,720)
\r
142 encvf = encvf + ",scale=-3:720::0:3"+harddup
\r
143 x264streamsize=str(tsize[0])+u"x720"
\r
144 x264_bitrate="2500"
\r
146 tsize=getMovieBaseSize2(pin)
\r
147 ofps="-ofps 30000/1001"
\r
148 #fps="-fps 30000/1001"
\r
150 x264fps="30000/1001"
\r
151 x264tune=x264tune+" --tff --nal-hrd vbr"
\r
152 encvf="-vf hqdn3d=2:1:2"
\r
154 encvf=encvf+harddup
\r
155 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
\r
156 if tsize[0] <= 0 or tsize[1] <= 0:
\r
157 encvf="-sws 9 -vf yadif=0,pp=l5"
\r
158 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
\r
159 ofps="-ofps 30000/1001"
\r
160 fps="-fps 30000/1001"
\r
161 x264fps="30000/1001"
\r
163 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
\r
164 x264streamsize=u"1280x720"
\r
165 x264_bitrate="2500"
\r
167 x264preset=u"ultrafast"
\r
169 x264preset=u"veryfast"
\r
173 x264preset=u"medium"
\r
177 x264preset=u"slower"
\r
179 x264preset=u"medium"
\r
180 if size == "WVGA_BASE" or size == "QVGA_BASE":
\r
181 x264profile=" --level 32 --profile baseline "
\r
183 x264profile=" --level 41 --profile high "
\r
185 lavft="-demuxer lavf "
\r
187 os.environ['LANG']="ja_JP.UTF-8"
\r
189 random.jumpahead(10)
\r
190 temptime=int(time.time())
\r
191 temptime=temptime % 9697
\r
192 random.jumpahead(temptime)
\r
193 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))
\r
194 unicode(commands.getoutput(u"mkfifo "+streampath),'utf-8','ignore')
\r
195 logmencoder=recdblist.getLogTitle(pin)+".mencoder.log"
\r
196 logx264=recdblist.getLogTitle(pin)+".x264.log"
\r
197 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
198 encexe=encexe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf)
\r
199 #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
200 encexe=u"nice -n 19 " +encexe+" 2>&1 | "+tee+" \""+logx264+"\""
\r
202 recdblist.addCommandSelfLog(pin, encexe)
\r
203 except Exception, inst:
\r
206 print traceback.print_exc(file=sys.stdout)
\r
207 recdblist.printutf8(encexe)
\r
210 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8','ignore')
\r
213 os.remove(streampath)
\r
214 zip.addFile2FileZip(logmencoder, recdblist.getLogTitle(pin)+".log.zip")
\r
215 zip.addFile2FileZip(logx264, recdblist.getLogTitle(pin)+".log.zip")
\r
216 os.remove(logmencoder)
\r
218 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
\r
219 # recdblist.addCommandLogTar(pin,u"Mencoder","mencoder", encexe,txt)
\r
220 def encodeFfmpegSar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
\r
224 ffmpeg=configreader.getConfPath("ffmpeg")
\r
225 tee=configreader.getConfPath("tee")
\r
226 fps=u"-r 29.970030 "
\r
227 x264fps="30000/1001"
\r
231 x264_bitrate="2500"
\r
233 tsar=getMoviePAR2(pin)
\r
234 filter="-deinterlace"
\r
235 x264sar=str(tsar[0])+":"+str(tsar[1])
\r
237 fps=u"-r 23.976023 "
\r
238 x264fps="24000/1001"
\r
239 x264tune="--tune animation"
\r
241 tsize=getParSize(pin,720)
\r
242 s = "-s "+str(tsize[0])+"x720 "
\r
243 x264streamsize=str(tsize[0])+u"x720"
\r
244 x264_bitrate="2500"
\r
245 elif size == "WVGA":
\r
246 tsize=getParSize(pin,480)
\r
247 s = "-s "+str(tsize[0])+"x480 "
\r
248 x264streamsize=str(tsize[0])+u"x480"
\r
249 x264_bitrate="1500"
\r
250 elif size == "FullHD":
\r
251 tsize=getParSize(pin,1080)
\r
252 s = "-s "+str(tsize[0])+"x1080 "
\r
253 x264streamsize=str(tsize[0])+u"x1080"
\r
254 x264_bitrate="5000"
\r
256 tsize=getParSize(pin,480)
\r
257 s = "-s "+str(tsize[0])+"x480 "
\r
258 x264streamsize=str(tsize[0])+u"x480"
\r
259 x264_bitrate="1250"
\r
260 elif size == "QVGA_BASE":
\r
261 tsize=getParSize(pin,240)
\r
262 s = "-s "+str(tsize[0])+"x240 "
\r
263 x264streamsize=str(tsize[0])+u"x240"
\r
265 elif size == "WVGA_BASE":
\r
266 tsize=getParSize(pin,480)
\r
267 s = "-s "+str(tsize[0])+"x480 "
\r
268 x264streamsize=str(tsize[0])+u"x480"
\r
269 x264_bitrate="1500"
\r
271 tsize=getParSize(pin,720)
\r
272 s = "-s "+str(tsize[0])+"x720 "
\r
273 x264streamsize=str(tsize[0])+u"x720"
\r
274 x264_bitrate="2500"
\r
276 tsize=getMovieBaseSize2(pin)
\r
278 s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "
\r
279 x264fps="30000/1001"
\r
280 x264tune=x264tune+" --tff --nal-hrd vbr"
\r
282 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
\r
284 x264preset=u"ultrafast"
\r
286 x264preset=u"veryfast"
\r
290 x264preset=u"medium"
\r
294 x264preset=u"slower"
\r
296 x264preset=u"medium"
\r
297 if size == "WVGA_BASE" or size == "QVGA_BASE":
\r
298 x264profile=" --level 32 --profile baseline "
\r
300 x264profile=" --level 41 --profile high "
\r
303 os.environ['LANG']="ja_JP.UTF-8"
\r
304 log_ffmpeg=recdblist.getLogTitle(pin)+".ffmpeg.log"
\r
305 log_x264=recdblist.getLogTitle(pin)+".x264.log"
\r
306 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - 2> \""+log_ffmpeg+"\" | "
\r
307 exe=exe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)
\r
308 exe = "nice -n 19 " + exe+" 2>&1 | "+tee+" \""+log_x264+"\""
\r
310 #recdblist.addCommandSelfLog(log_ffmpeg,recdblist.getLogTitle(pin)+".log.zip")
\r
311 #recdblist.addCommandSelfLog(log_x264,recdblist.getLogTitle(pin)+".log.zip")
\r
312 recdblist.printutf8(exe)
\r
313 recdblist.addCommandSelfLog(pin,exe)
\r
315 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
318 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
\r
319 zip.addFile2FileZip(log_ffmpeg, recdblist.getLogTitle(pin+".log.zip"))
\r
320 zip.addFile2FileZip(log_x264, recdblist.getLogTitle(pin+".log.zip"))
\r
321 os.remove(log_ffmpeg)
\r
322 os.remove(log_x264)
\r
323 def getMovieBaseSize(pin):
\r
324 ffmpeg=configreader.getConfPath("ffmpeg")
\r
325 os.environ['LANG']="ja_JP.UTF-8"
\r
326 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
\r
327 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
328 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
331 txtls=txts.split("\n")
\r
335 sizetxt=rM.group(1)
\r
337 tX=int(sizetxt.split("x")[0])
\r
338 tY=int(sizetxt.split("x")[1])
\r
339 tEX=int(partxt.split(":")[0])
\r
340 tEY=int(partxt.split(":")[1])
\r
344 return [sizeMaxX,sizeMaxY]
\r
345 def getMovieBaseSize2(pin):##動画開始後すぐに解像度が変更されたときに対処
\r
346 ffmpeg=configreader.getConfPath("ffmpeg")
\r
347 os.environ['LANG']="ja_JP.UTF-8"
\r
348 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
\r
349 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
350 rT=re.compile(u"Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
353 txtls=txts.split("\n")
\r
357 sizetxt=rM.group(1)
\r
359 tX=int(sizetxt.split("x")[0])
\r
360 tY=int(sizetxt.split("x")[1])
\r
361 tEX=int(partxt.split(":")[0])
\r
362 tEY=int(partxt.split(":")[1])
\r
366 os.remove(pin+".size.ts")
\r
367 return [sizeMaxX,sizeMaxY]
\r
368 def getMoviePAR(pin):
\r
369 ffmpeg=configreader.getConfPath("ffmpeg")
\r
370 os.environ['LANG']="ja_JP.UTF-8"
\r
371 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
\r
372 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
373 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
377 txtls=txts.split("\n")
\r
381 sizetxt=rM.group(1)
\r
383 tX=int(sizetxt.split("x")[0])
\r
384 tY=int(sizetxt.split("x")[1])
\r
385 tEX=int(dartxt.split(":")[0])
\r
386 tEY=int(dartxt.split(":")[1])
\r
389 if tX==1920 and tY==1080:
\r
396 def getMoviePAR2(pin):
\r
397 ffmpeg=configreader.getConfPath("ffmpeg")
\r
398 os.environ['LANG']="ja_JP.UTF-8"
\r
399 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
\r
400 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
401 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
405 txtls=txts.split("\n")
\r
409 sizetxt=rM.group(1)
\r
411 tX=int(sizetxt.split("x")[0])
\r
412 tY=int(sizetxt.split("x")[1])
\r
413 tEX=int(dartxt.split(":")[0])
\r
414 tEY=int(dartxt.split(":")[1])
\r
417 if tX==1920 and tY==1080:
\r
423 os.remove(pin+".size.ts")
\r
425 def getMovieDAR(pin):
\r
426 ffmpeg=configreader.getConfPath("ffmpeg")
\r
427 os.environ['LANG']="ja_JP.UTF-8"
\r
428 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
\r
429 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
430 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
\r
434 txtls=txts.split("\n")
\r
438 sizetxt=rM.group(1)
\r
440 tX=int(sizetxt.split("x")[0])
\r
441 tY=int(sizetxt.split("x")[1])
\r
442 tEX=int(dartxt.split(":")[0])
\r
443 tEY=int(dartxt.split(":")[1])
\r
446 if tX==1920 and tY==1080:
\r
453 def getMovieDAR2(pin):
\r
454 ffmpeg=configreader.getConfPath("ffmpeg")
\r
455 os.environ['LANG']="ja_JP.UTF-8"
\r
456 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
\r
457 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
458 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
\r
462 txtls=txts.split("\n")
\r
466 sizetxt=rM.group(1)
\r
468 tX=int(sizetxt.split("x")[0])
\r
469 tY=int(sizetxt.split("x")[1])
\r
470 tEX=int(dartxt.split(":")[0])
\r
471 tEY=int(dartxt.split(":")[1])
\r
474 if tX==1920 and tY==1080:
\r
480 os.remove(pin+".size.ts")
\r
482 def getParSize(pin,y):
\r
483 tSize=getMovieBaseSize2(pin)
\r
485 tX=tSize[0]*10*y/tSize[1]
\r
487 if tX>int(tX/10)*10:
\r
495 def getX264CoreVersion():
\r
496 x264=configreader.getConfPath("x264")
\r
497 t1=commands.getoutput(x264+" --help|grep core")
\r
498 rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
\r
504 def getX264Commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=-1,bitrate=0):
\r
505 x264=configreader.getConfPath("x264")
\r
506 os.environ['LANG']="ja_JP.UTF-8"
\r
507 x264_sar="--sar "+sar
\r
508 x264preset=u"--preset "+preset
\r
509 x264fps="--fps "+fps
\r
511 x264bitrate=u"--bitrate "+str(bitrate)
\r
513 x264crf=u"--crf "+str(crf)
\r
514 x264_addline=configreader.getConfEnv("x264_addline")
\r
517 xtt=configreader.getConfEnv("x264_thread")
\r
520 x264_thread=str(xtt)
\r
523 x264_addline=configreader.getConfEnv("x264_addline")
\r
524 if getX264CoreVersion()>103:
\r
525 x264res=u"--input-res "+x264streamsize
\r
526 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
528 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