3 # Rec10 TS Recording Tools
\r
4 # Copyright (C) 2009-2010 Yukikaze
\r
15 def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
\r
17 pinで指定されたファイルをpoutにx264でエンコードして書き出す
\r
24 quality=int(configreader.getConfEnv("x264_preset"))
\r
25 crf=int(configreader.getConfEnv("crf"))
\r
27 if re.search("H", opts):
\r
29 if re.search("S", opts):
\r
31 if re.search("F", opts):
\r
33 if re.search("W",opts):
\r
35 if re.search("1",opts):
\r
38 if re.search("2", opts):
\r
41 if re.search("v", opts):
\r
43 crf=int(configreader.getConfEnv("animation_crf"))
\r
44 if re.search("a", opts):
\r
46 crf=int(configreader.getConfEnv("animation_crf"))
\r
47 if re.search("I", opts):
\r
49 if re.search("q",opts):
\r
51 if re.search("w",opts):
\r
53 if re.search("e",opts):
\r
55 if re.search("r",opts):
\r
57 if re.search("u",opts):
\r
59 if re.search("i",opts):
\r
61 if re.search("o",opts):
\r
63 if re.search("p",opts):
\r
65 if re.search("d",opts):#二カ国語放送の場合
\r
66 tm2v=pin.replace(".ts",".m2v")
\r
67 encodeFfmpegSar(tm2v,pout,size,is24fps,quality,crf,deinterlace)
\r
68 elif re.search("5",opts):#5.1chの場合
\r
69 encodeFfmpegSar(pin,pout,size,is24fps,quality,crf,deinterlace)
\r
72 encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace)
\r
73 except Exception, inst:
\r
74 recdblist.addCommonlogEX("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
\r
75 def encodeMencoderSar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
\r
76 mencoder=configreader.getConfPath("mencoder")
\r
79 encvf="-sws 9 -vf yadif=0,pp=l5"
\r
80 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
\r
81 ofps="-ofps 30000/1001"
\r
82 fps="-fps 30000/1001"
\r
83 x264fps="30000/1001"
\r
88 tsar=getMoviePAR2(pin)
\r
89 x264sar=str(tsar[0])+":"+str(tsar[1])
\r
91 ofps="-ofps 24000/1001"
\r
92 fps="-fps 30000/1001"
\r
93 x264fps="24000/1001"
\r
94 x264tune="--tune animation"
\r
95 encvf="-sws 9 -vf pullup,softskip"
\r
96 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
\r
98 tsize=getParSize(pin,720)
\r
99 encvf = encvf + ",scale=-3:720::0:3"+harddup
\r
100 x264streamsize=str(tsize[0])+u"x720"
\r
101 x264_bitrate="2500"
\r
102 elif size == "WVGA":
\r
103 tsize=getParSize(pin,480)
\r
104 encvf = encvf + ",scale=-3:480::0:3"+harddup
\r
105 x264streamsize=str(tsize[0])+u"x480"
\r
106 x264_bitrate="1500"
\r
107 elif size == "FullHD":
\r
108 tsize=getParSize(pin,1080)
\r
109 encvf = encvf + ",scale=-3:1080::0:3"+harddup
\r
110 x264streamsize=str(tsize[0])+u"x1080"
\r
111 x264_bitrate="5000"
\r
112 elif size == "QVGA_BASE":
\r
113 tsize=getParSize(pin,240)
\r
114 encvf = encvf + ",scale=-3:240::0:3"+harddup
\r
115 x264streamsize=str(tsize[0])+u"x240"
\r
117 elif size == "WVGA_BASE":
\r
118 tsize=getParSize(pin,480)
\r
119 encvf = encvf + ",scale=-3:480::0:3"+harddup
\r
120 x264streamsize=str(tsize[0])+u"x480"
\r
121 x264_bitrate="1500"
\r
123 tsize=getParSize(pin,720)
\r
124 encvf = encvf + ",scale=-3:720::0:3"+harddup
\r
125 x264streamsize=str(tsize[0])+u"x720"
\r
126 x264_bitrate="2500"
\r
128 tsize=getMovieBaseSize2(pin)
\r
129 ofps="-ofps 30000/1001"
\r
130 #fps="-fps 30000/1001"
\r
132 x264fps="30000/1001"
\r
133 x264tune=x264tune+" --tff --nal-hrd vbr"
\r
134 encvf="-vf hqdn3d=2:1:2"
\r
136 encvf=encvf+harddup
\r
137 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
\r
138 if tsize[0] <= 0 or tsize[1] <= 0:
\r
139 encvf="-sws 9 -vf yadif=0,pp=l5"
\r
140 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
\r
141 ofps="-ofps 30000/1001"
\r
142 fps="-fps 30000/1001"
\r
143 x264fps="30000/1001"
\r
145 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
\r
146 x264streamsize=u"1280x720"
\r
147 x264_bitrate="2500"
\r
149 x264preset=u"ultrafast"
\r
151 x264preset=u"veryfast"
\r
155 x264preset=u"medium"
\r
159 x264preset=u"slower"
\r
160 if size == "WVGA_BASE" or size == "QVGA_BASE":
\r
161 x264profile=" --level 32 --profile baseline "
\r
163 x264profile=" --level 41 --profile high "
\r
165 os.environ['LANG']="ja_JP.UTF-8"
\r
167 random.jumpahead(10)
\r
168 temptime=int(time.time())
\r
169 temptime=temptime % 9697
\r
170 random.jumpahead(temptime)
\r
171 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))
\r
172 os.system(u"mkfifo "+streampath)
\r
173 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" & "
\r
174 encexe=encexe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf)
\r
175 #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
176 encexe=u"nice -n 19 " +encexe
\r
177 recdblist.printutf8(encexe)
\r
180 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8','ignore')
\r
183 os.system("rm "+streampath)
\r
184 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
\r
185 def encodeFfmpegSar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
\r
189 ffmpeg=configreader.getConfPath("ffmpeg")
\r
190 fps=u"-r 29.970030 "
\r
191 x264fps="30000/1001"
\r
195 x264_bitrate="2500"
\r
197 tsar=getMoviePAR2(pin)
\r
198 filter="-deinterlace"
\r
199 x264sar=str(tsar[0])+":"+str(tsar[1])
\r
201 tsize=getParSize(pin,720)
\r
202 s = "-s "+str(tsize[0])+"x720 "
\r
203 x264streamsize=str(tsize[0])+u"x720"
\r
204 x264_bitrate="2500"
\r
205 elif size == "WVGA":
\r
206 tsize=getParSize(pin,480)
\r
207 s = "-s "+str(tsize[0])+"x480 "
\r
208 x264streamsize=str(tsize[0])+u"x480"
\r
209 x264_bitrate="1500"
\r
210 elif size == "FullHD":
\r
211 tsize=getParSize(pin,1080)
\r
212 s = "-s "+str(tsize[0])+"x1080 "
\r
213 x264streamsize=str(tsize[0])+u"x1080"
\r
214 x264_bitrate="5000"
\r
216 tsize=getParSize(pin,480)
\r
217 s = "-s "+str(tsize[0])+"x480 "
\r
218 x264streamsize=str(tsize[0])+u"x480"
\r
219 x264_bitrate="1250"
\r
220 elif size == "QVGA_BASE":
\r
221 tsize=getParSize(pin,240)
\r
222 s = "-s "+str(tsize[0])+"x240 "
\r
223 x264streamsize=str(tsize[0])+u"x240"
\r
225 elif size == "WVGA_BASE":
\r
226 tsize=getParSize(pin,480)
\r
227 s = "-s "+str(tsize[0])+"x480 "
\r
228 x264streamsize=str(tsize[0])+u"x480"
\r
229 x264_bitrate="1500"
\r
231 tsize=getParSize(pin,720)
\r
232 s = "-s "+str(tsize[0])+"x720 "
\r
233 x264streamsize=str(tsize[0])+u"x720"
\r
234 x264_bitrate="2500"
\r
236 tsize=getMovieBaseSize2(pin)
\r
238 s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "
\r
239 x264fps="30000/1001"
\r
240 x264tune=x264tune+" --tff --nal-hrd vbr"
\r
242 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
\r
244 x264preset=u"ultrafast"
\r
246 x264preset=u"veryfast"
\r
250 x264preset=u"medium"
\r
254 x264preset=u"slower"
\r
255 if size == "WVGA_BASE" or size == "QVGA_BASE":
\r
256 x264profile=" --level 32 --profile baseline "
\r
258 x264profile=" --level 41 --profile high "
\r
261 os.environ['LANG']="ja_JP.UTF-8"
\r
262 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - | "
\r
263 exe=exe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)
\r
264 exe = "nice -n 19 " + exe
\r
266 recdblist.printutf8(exe)
\r
268 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
271 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
\r
272 def getMovieBaseSize(pin):
\r
273 ffmpeg=configreader.getConfPath("ffmpeg")
\r
274 os.environ['LANG']="ja_JP.UTF-8"
\r
275 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
\r
276 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
277 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
280 txtls=txts.split("\n")
\r
284 sizetxt=rM.group(1)
\r
286 tX=int(sizetxt.split("x")[0])
\r
287 tY=int(sizetxt.split("x")[1])
\r
288 tEX=int(partxt.split(":")[0])
\r
289 tEY=int(partxt.split(":")[1])
\r
293 return [sizeMaxX,sizeMaxY]
\r
294 def getMovieBaseSize2(pin):##動画開始後すぐに解像度が変更されたときに対処
\r
295 ffmpeg=configreader.getConfPath("ffmpeg")
\r
296 os.environ['LANG']="ja_JP.UTF-8"
\r
297 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
\r
298 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
299 rT=re.compile(u"Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
302 txtls=txts.split("\n")
\r
306 sizetxt=rM.group(1)
\r
308 tX=int(sizetxt.split("x")[0])
\r
309 tY=int(sizetxt.split("x")[1])
\r
310 tEX=int(partxt.split(":")[0])
\r
311 tEY=int(partxt.split(":")[1])
\r
315 os.remove(pin+".size.ts")
\r
316 return [sizeMaxX,sizeMaxY]
\r
317 def getMoviePAR(pin):
\r
318 ffmpeg=configreader.getConfPath("ffmpeg")
\r
319 os.environ['LANG']="ja_JP.UTF-8"
\r
320 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
\r
321 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
322 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
326 txtls=txts.split("\n")
\r
330 sizetxt=rM.group(1)
\r
332 tX=int(sizetxt.split("x")[0])
\r
333 tY=int(sizetxt.split("x")[1])
\r
334 tEX=int(dartxt.split(":")[0])
\r
335 tEY=int(dartxt.split(":")[1])
\r
338 if tX==1920 and tY==1080:
\r
345 def getMoviePAR2(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
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(dartxt.split(":")[0])
\r
363 tEY=int(dartxt.split(":")[1])
\r
366 if tX==1920 and tY==1080:
\r
372 os.remove(pin+".size.ts")
\r
374 def getMovieDAR(pin):
\r
375 ffmpeg=configreader.getConfPath("ffmpeg")
\r
376 os.environ['LANG']="ja_JP.UTF-8"
\r
377 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
\r
378 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
379 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
\r
383 txtls=txts.split("\n")
\r
387 sizetxt=rM.group(1)
\r
389 tX=int(sizetxt.split("x")[0])
\r
390 tY=int(sizetxt.split("x")[1])
\r
391 tEX=int(dartxt.split(":")[0])
\r
392 tEY=int(dartxt.split(":")[1])
\r
395 if tX==1920 and tY==1080:
\r
402 def getMovieDAR2(pin):
\r
403 ffmpeg=configreader.getConfPath("ffmpeg")
\r
404 os.environ['LANG']="ja_JP.UTF-8"
\r
405 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 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+.*DAR\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
429 os.remove(pin+".size.ts")
\r
431 def getParSize(pin,y):
\r
432 tSize=getMovieBaseSize2(pin)
\r
434 tX=tSize[0]*10*y/tSize[1]
\r
436 if tX>int(tX/10)*10:
\r
444 def getX264CoreVersion():
\r
445 x264=configreader.getConfPath("x264")
\r
446 t1=commands.getoutput(x264+" --help|grep core")
\r
447 rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
\r
453 def getX264Commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=-1,bitrate=0):
\r
454 x264=configreader.getConfPath("x264")
\r
455 os.environ['LANG']="ja_JP.UTF-8"
\r
456 x264_sar="--sar "+sar
\r
457 x264preset=u"--preset "+preset
\r
458 x264fps="--fps "+fps
\r
460 x264bitrate=u"--bitrate "+str(bitrate)
\r
462 x264crf=u"--crf "+str(crf)
\r
463 x264_addline=configreader.getConfEnv("x264_addline")
\r
466 xtt=configreader.getConfEnv("x264_thread")
\r
469 x264_thread=str(xtt)
\r
472 x264_addline=configreader.getConfEnv("x264_addline")
\r
473 if getX264CoreVersion()>103:
\r
474 x264res=u"--input-res "+x264streamsize
\r
475 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
477 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