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
73 encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace)
\r
74 except Exception, inst:
\r
75 recdblist.addCommonlogEX("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
\r
76 def encodeMencoderSar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
\r
77 mencoder=configreader.getConfPath("mencoder")
\r
78 tee=configreader.getConfPath("tee")
\r
81 encvf="-sws 9 -vf yadif=0,pp=l5"
\r
82 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
\r
83 ofps="-ofps 30000/1001"
\r
84 fps="-fps 30000/1001"
\r
85 x264fps="30000/1001"
\r
90 tsar=getMoviePAR2(pin)
\r
91 x264sar=str(tsar[0])+":"+str(tsar[1])
\r
93 ofps="-ofps 24000/1001"
\r
94 fps="-fps 30000/1001"
\r
95 x264fps="24000/1001"
\r
96 x264tune="--tune animation"
\r
97 encvf="-sws 9 -vf pullup,softskip"
\r
98 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
\r
100 tsize=getParSize(pin,720)
\r
101 encvf = encvf + ",scale=-3:720::0:3"+harddup
\r
102 x264streamsize=str(tsize[0])+u"x720"
\r
103 x264_bitrate="2500"
\r
104 elif size == "WVGA":
\r
105 tsize=getParSize(pin,480)
\r
106 encvf = encvf + ",scale=-3:480::0:3"+harddup
\r
107 x264streamsize=str(tsize[0])+u"x480"
\r
108 x264_bitrate="1500"
\r
109 elif size == "FullHD":
\r
110 tsize=getParSize(pin,1080)
\r
111 encvf = encvf + ",scale=-3:1080::0:3"+harddup
\r
112 x264streamsize=str(tsize[0])+u"x1080"
\r
113 x264_bitrate="5000"
\r
114 elif size == "QVGA_BASE":
\r
115 tsize=getParSize(pin,240)
\r
116 encvf = encvf + ",scale=-3:240::0:3"+harddup
\r
117 x264streamsize=str(tsize[0])+u"x240"
\r
119 elif size == "WVGA_BASE":
\r
120 tsize=getParSize(pin,480)
\r
121 encvf = encvf + ",scale=-3:480::0:3"+harddup
\r
122 x264streamsize=str(tsize[0])+u"x480"
\r
123 x264_bitrate="1500"
\r
125 tsize=getParSize(pin,720)
\r
126 encvf = encvf + ",scale=-3:720::0:3"+harddup
\r
127 x264streamsize=str(tsize[0])+u"x720"
\r
128 x264_bitrate="2500"
\r
130 tsize=getMovieBaseSize2(pin)
\r
131 ofps="-ofps 30000/1001"
\r
132 #fps="-fps 30000/1001"
\r
134 x264fps="30000/1001"
\r
135 x264tune=x264tune+" --tff --nal-hrd vbr"
\r
136 encvf="-vf hqdn3d=2:1:2"
\r
138 encvf=encvf+harddup
\r
139 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
\r
140 if tsize[0] <= 0 or tsize[1] <= 0:
\r
141 encvf="-sws 9 -vf yadif=0,pp=l5"
\r
142 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
\r
143 ofps="-ofps 30000/1001"
\r
144 fps="-fps 30000/1001"
\r
145 x264fps="30000/1001"
\r
147 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
\r
148 x264streamsize=u"1280x720"
\r
149 x264_bitrate="2500"
\r
151 x264preset=u"ultrafast"
\r
153 x264preset=u"veryfast"
\r
157 x264preset=u"medium"
\r
161 x264preset=u"slower"
\r
163 x264preset=u"medium"
\r
164 if size == "WVGA_BASE" or size == "QVGA_BASE":
\r
165 x264profile=" --level 32 --profile baseline "
\r
167 x264profile=" --level 41 --profile high "
\r
169 os.environ['LANG']="ja_JP.UTF-8"
\r
171 random.jumpahead(10)
\r
172 temptime=int(time.time())
\r
173 temptime=temptime % 9697
\r
174 random.jumpahead(temptime)
\r
175 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))
\r
176 unicode(commands.getoutput(u"mkfifo "+streampath),'utf-8','ignore')
\r
177 logmencoder=recdblist.getLogTitle(pin)+".mencoder.log"
\r
178 logx264=recdblist.getLogTitle(pin)+".x264.log"
\r
179 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" 2>&1 | "+tee+" \""+logmencoder+"\" & "
\r
180 encexe=encexe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf)
\r
181 #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
182 encexe=u"nice -n 19 " +encexe+" 2>&1 | "+tee+" \""+logx264+"\""
\r
184 recdblist.addCommandSelfLog(pin, encexe)
\r
185 except Exception, inst:
\r
188 print traceback.print_exc(file=sys.stdout)
\r
189 recdblist.printutf8(encexe)
\r
192 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8','ignore')
\r
195 os.remove(streampath)
\r
196 zip.addFile2FileZip(logmencoder, recdblist.getLogTitle(pin)+".log.zip")
\r
197 zip.addFile2FileZip(logx264, recdblist.getLogTitle(pin)+".log.zip")
\r
198 os.remove(logmencoder)
\r
200 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
\r
201 # recdblist.addCommandLogTar(pin,u"Mencoder","mencoder", encexe,txt)
\r
202 def encodeFfmpegSar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
\r
206 ffmpeg=configreader.getConfPath("ffmpeg")
\r
207 tee=configreader.getConfPath("tee")
\r
208 fps=u"-r 29.970030 "
\r
209 x264fps="30000/1001"
\r
213 x264_bitrate="2500"
\r
215 tsar=getMoviePAR2(pin)
\r
216 filter="-deinterlace"
\r
217 x264sar=str(tsar[0])+":"+str(tsar[1])
\r
219 tsize=getParSize(pin,720)
\r
220 s = "-s "+str(tsize[0])+"x720 "
\r
221 x264streamsize=str(tsize[0])+u"x720"
\r
222 x264_bitrate="2500"
\r
223 elif size == "WVGA":
\r
224 tsize=getParSize(pin,480)
\r
225 s = "-s "+str(tsize[0])+"x480 "
\r
226 x264streamsize=str(tsize[0])+u"x480"
\r
227 x264_bitrate="1500"
\r
228 elif size == "FullHD":
\r
229 tsize=getParSize(pin,1080)
\r
230 s = "-s "+str(tsize[0])+"x1080 "
\r
231 x264streamsize=str(tsize[0])+u"x1080"
\r
232 x264_bitrate="5000"
\r
234 tsize=getParSize(pin,480)
\r
235 s = "-s "+str(tsize[0])+"x480 "
\r
236 x264streamsize=str(tsize[0])+u"x480"
\r
237 x264_bitrate="1250"
\r
238 elif size == "QVGA_BASE":
\r
239 tsize=getParSize(pin,240)
\r
240 s = "-s "+str(tsize[0])+"x240 "
\r
241 x264streamsize=str(tsize[0])+u"x240"
\r
243 elif size == "WVGA_BASE":
\r
244 tsize=getParSize(pin,480)
\r
245 s = "-s "+str(tsize[0])+"x480 "
\r
246 x264streamsize=str(tsize[0])+u"x480"
\r
247 x264_bitrate="1500"
\r
249 tsize=getParSize(pin,720)
\r
250 s = "-s "+str(tsize[0])+"x720 "
\r
251 x264streamsize=str(tsize[0])+u"x720"
\r
252 x264_bitrate="2500"
\r
254 tsize=getMovieBaseSize2(pin)
\r
256 s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "
\r
257 x264fps="30000/1001"
\r
258 x264tune=x264tune+" --tff --nal-hrd vbr"
\r
260 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
\r
262 x264preset=u"ultrafast"
\r
264 x264preset=u"veryfast"
\r
268 x264preset=u"medium"
\r
272 x264preset=u"slower"
\r
274 x264preset=u"medium"
\r
275 if size == "WVGA_BASE" or size == "QVGA_BASE":
\r
276 x264profile=" --level 32 --profile baseline "
\r
278 x264profile=" --level 41 --profile high "
\r
281 os.environ['LANG']="ja_JP.UTF-8"
\r
282 log_ffmpeg=recdblist.getLogTitle(pin)+".ffmpeg.log"
\r
283 log_x264=recdblist.getLogTitle(pin)+".x264.log"
\r
284 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - 2> \""+log_ffmpeg+"\" | "
\r
285 exe=exe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)
\r
286 exe = "nice -n 19 " + exe+" 2>&1 | "+tee+" \""+log_x264+"\""
\r
289 #recdblist.addCommandSelfLog(log_ffmpeg,recdblist.getLogTitle(pin)+".log.zip")
\r
290 #recdblist.addCommandSelfLog(log_x264,recdblist.getLogTitle(pin)+".log.zip")
\r
291 recdblist.printutf8(exe)
\r
293 recdblist.addCommandSelfLog(tspath,exe)
\r
296 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
299 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
\r
300 zip.addFile2FileZip(log_ffmpeg, recdblist.getLogTitle(tspath+".log.zip"))
\r
301 zip.addFile2FileZip(log_x264, recdblist.getLogTitle(tspath+".log.zip"))
\r
302 os.remove(log_ffmpeg)
\r
303 os.remove(log_x264)
\r
304 def getMovieBaseSize(pin):
\r
305 ffmpeg=configreader.getConfPath("ffmpeg")
\r
306 os.environ['LANG']="ja_JP.UTF-8"
\r
307 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
\r
308 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
309 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
312 txtls=txts.split("\n")
\r
316 sizetxt=rM.group(1)
\r
318 tX=int(sizetxt.split("x")[0])
\r
319 tY=int(sizetxt.split("x")[1])
\r
320 tEX=int(partxt.split(":")[0])
\r
321 tEY=int(partxt.split(":")[1])
\r
325 return [sizeMaxX,sizeMaxY]
\r
326 def getMovieBaseSize2(pin):##動画開始後すぐに解像度が変更されたときに対処
\r
327 ffmpeg=configreader.getConfPath("ffmpeg")
\r
328 os.environ['LANG']="ja_JP.UTF-8"
\r
329 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
\r
330 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
331 rT=re.compile(u"Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
334 txtls=txts.split("\n")
\r
338 sizetxt=rM.group(1)
\r
340 tX=int(sizetxt.split("x")[0])
\r
341 tY=int(sizetxt.split("x")[1])
\r
342 tEX=int(partxt.split(":")[0])
\r
343 tEY=int(partxt.split(":")[1])
\r
347 os.remove(pin+".size.ts")
\r
348 return [sizeMaxX,sizeMaxY]
\r
349 def getMoviePAR(pin):
\r
350 ffmpeg=configreader.getConfPath("ffmpeg")
\r
351 os.environ['LANG']="ja_JP.UTF-8"
\r
352 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
\r
353 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
354 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
358 txtls=txts.split("\n")
\r
362 sizetxt=rM.group(1)
\r
364 tX=int(sizetxt.split("x")[0])
\r
365 tY=int(sizetxt.split("x")[1])
\r
366 tEX=int(dartxt.split(":")[0])
\r
367 tEY=int(dartxt.split(":")[1])
\r
370 if tX==1920 and tY==1080:
\r
377 def getMoviePAR2(pin):
\r
378 ffmpeg=configreader.getConfPath("ffmpeg")
\r
379 os.environ['LANG']="ja_JP.UTF-8"
\r
380 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
\r
381 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
382 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
386 txtls=txts.split("\n")
\r
390 sizetxt=rM.group(1)
\r
392 tX=int(sizetxt.split("x")[0])
\r
393 tY=int(sizetxt.split("x")[1])
\r
394 tEX=int(dartxt.split(":")[0])
\r
395 tEY=int(dartxt.split(":")[1])
\r
398 if tX==1920 and tY==1080:
\r
404 os.remove(pin+".size.ts")
\r
406 def getMovieDAR(pin):
\r
407 ffmpeg=configreader.getConfPath("ffmpeg")
\r
408 os.environ['LANG']="ja_JP.UTF-8"
\r
409 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
\r
410 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
411 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
\r
415 txtls=txts.split("\n")
\r
419 sizetxt=rM.group(1)
\r
421 tX=int(sizetxt.split("x")[0])
\r
422 tY=int(sizetxt.split("x")[1])
\r
423 tEX=int(dartxt.split(":")[0])
\r
424 tEY=int(dartxt.split(":")[1])
\r
427 if tX==1920 and tY==1080:
\r
434 def getMovieDAR2(pin):
\r
435 ffmpeg=configreader.getConfPath("ffmpeg")
\r
436 os.environ['LANG']="ja_JP.UTF-8"
\r
437 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
\r
438 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
439 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
\r
443 txtls=txts.split("\n")
\r
447 sizetxt=rM.group(1)
\r
449 tX=int(sizetxt.split("x")[0])
\r
450 tY=int(sizetxt.split("x")[1])
\r
451 tEX=int(dartxt.split(":")[0])
\r
452 tEY=int(dartxt.split(":")[1])
\r
455 if tX==1920 and tY==1080:
\r
461 os.remove(pin+".size.ts")
\r
463 def getParSize(pin,y):
\r
464 tSize=getMovieBaseSize2(pin)
\r
466 tX=tSize[0]*10*y/tSize[1]
\r
468 if tX>int(tX/10)*10:
\r
476 def getX264CoreVersion():
\r
477 x264=configreader.getConfPath("x264")
\r
478 t1=commands.getoutput(x264+" --help|grep core")
\r
479 rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
\r
485 def getX264Commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=-1,bitrate=0):
\r
486 x264=configreader.getConfPath("x264")
\r
487 os.environ['LANG']="ja_JP.UTF-8"
\r
488 x264_sar="--sar "+sar
\r
489 x264preset=u"--preset "+preset
\r
490 x264fps="--fps "+fps
\r
492 x264bitrate=u"--bitrate "+str(bitrate)
\r
494 x264crf=u"--crf "+str(crf)
\r
495 x264_addline=configreader.getConfEnv("x264_addline")
\r
498 xtt=configreader.getConfEnv("x264_thread")
\r
501 x264_thread=str(xtt)
\r
504 x264_addline=configreader.getConfEnv("x264_addline")
\r
505 if getX264CoreVersion()>103:
\r
506 x264res=u"--input-res "+x264streamsize
\r
507 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
509 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