3 # Rec10 TS Recording Tools
\r
4 # Copyright (C) 2009-2011 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("MW1",opts):
\r
38 if re.search("MW2", 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
161 x264preset=u"medium"
\r
162 if size == "WVGA_BASE" or size == "QVGA_BASE":
\r
163 x264profile=" --level 32 --profile baseline "
\r
165 x264profile=" --level 41 --profile high "
\r
167 os.environ['LANG']="ja_JP.UTF-8"
\r
169 random.jumpahead(10)
\r
170 temptime=int(time.time())
\r
171 temptime=temptime % 9697
\r
172 random.jumpahead(temptime)
\r
173 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))
\r
174 unicode(commands.getoutput(u"mkfifo "+streampath),'utf-8','ignore')
\r
175 logmencoder=recdblist.getLogTitle(pin)+".mencoder.log"
\r
176 logx264=recdblist.getLogTitle(pin)+".x264.log"
\r
177 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" | tee "+logmencoder+"& "
\r
178 encexe=encexe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf)
\r
179 #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
180 encexe=u"nice -n 19 " +encexe+" | tee "+logx264
\r
181 recdblist.addCommandSelfLog(pin, encexe)
\r
182 recdblist.printutf8(encexe)
\r
185 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8','ignore')
\r
188 os.remove(streampath)
\r
189 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
\r
190 recdblist.addCommandLogTar(pin,u"Mencoder","mencoder", encext,txt)
\r
191 def encodeFfmpegSar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
\r
195 ffmpeg=configreader.getConfPath("ffmpeg")
\r
196 fps=u"-r 29.970030 "
\r
197 x264fps="30000/1001"
\r
201 x264_bitrate="2500"
\r
203 tsar=getMoviePAR2(pin)
\r
204 filter="-deinterlace"
\r
205 x264sar=str(tsar[0])+":"+str(tsar[1])
\r
207 tsize=getParSize(pin,720)
\r
208 s = "-s "+str(tsize[0])+"x720 "
\r
209 x264streamsize=str(tsize[0])+u"x720"
\r
210 x264_bitrate="2500"
\r
211 elif size == "WVGA":
\r
212 tsize=getParSize(pin,480)
\r
213 s = "-s "+str(tsize[0])+"x480 "
\r
214 x264streamsize=str(tsize[0])+u"x480"
\r
215 x264_bitrate="1500"
\r
216 elif size == "FullHD":
\r
217 tsize=getParSize(pin,1080)
\r
218 s = "-s "+str(tsize[0])+"x1080 "
\r
219 x264streamsize=str(tsize[0])+u"x1080"
\r
220 x264_bitrate="5000"
\r
222 tsize=getParSize(pin,480)
\r
223 s = "-s "+str(tsize[0])+"x480 "
\r
224 x264streamsize=str(tsize[0])+u"x480"
\r
225 x264_bitrate="1250"
\r
226 elif size == "QVGA_BASE":
\r
227 tsize=getParSize(pin,240)
\r
228 s = "-s "+str(tsize[0])+"x240 "
\r
229 x264streamsize=str(tsize[0])+u"x240"
\r
231 elif size == "WVGA_BASE":
\r
232 tsize=getParSize(pin,480)
\r
233 s = "-s "+str(tsize[0])+"x480 "
\r
234 x264streamsize=str(tsize[0])+u"x480"
\r
235 x264_bitrate="1500"
\r
237 tsize=getParSize(pin,720)
\r
238 s = "-s "+str(tsize[0])+"x720 "
\r
239 x264streamsize=str(tsize[0])+u"x720"
\r
240 x264_bitrate="2500"
\r
242 tsize=getMovieBaseSize2(pin)
\r
244 s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "
\r
245 x264fps="30000/1001"
\r
246 x264tune=x264tune+" --tff --nal-hrd vbr"
\r
248 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
\r
250 x264preset=u"ultrafast"
\r
252 x264preset=u"veryfast"
\r
256 x264preset=u"medium"
\r
260 x264preset=u"slower"
\r
262 x264preset=u"medium"
\r
263 if size == "WVGA_BASE" or size == "QVGA_BASE":
\r
264 x264profile=" --level 32 --profile baseline "
\r
266 x264profile=" --level 41 --profile high "
\r
269 os.environ['LANG']="ja_JP.UTF-8"
\r
270 log_ffmpeg=recdblist.getLogTitle(pin)+".ffmpeg.log"
\r
271 log_x264=recdblist.getLogTitle(pin)+".x264.log"
\r
272 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - 2> "+log_ffmpeg+"| "
\r
273 exe=exe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)
\r
274 exe = "nice -n 19 " + exe+" 2>&1 | tee "+log_x264
\r
276 recdblist.addCommandSelfLog(pin,exe)
\r
277 recdblist.printutf8(exe)
\r
279 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
282 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
\r
283 recdblist.addCommandLogTar(pin,u"ffmpeg","ffmpeg", exe,txt)
\r
284 def getMovieBaseSize(pin):
\r
285 ffmpeg=configreader.getConfPath("ffmpeg")
\r
286 os.environ['LANG']="ja_JP.UTF-8"
\r
287 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
\r
288 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
289 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
292 txtls=txts.split("\n")
\r
296 sizetxt=rM.group(1)
\r
298 tX=int(sizetxt.split("x")[0])
\r
299 tY=int(sizetxt.split("x")[1])
\r
300 tEX=int(partxt.split(":")[0])
\r
301 tEY=int(partxt.split(":")[1])
\r
305 return [sizeMaxX,sizeMaxY]
\r
306 def getMovieBaseSize2(pin):##動画開始後すぐに解像度が変更されたときに対処
\r
307 ffmpeg=configreader.getConfPath("ffmpeg")
\r
308 os.environ['LANG']="ja_JP.UTF-8"
\r
309 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
\r
310 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
311 rT=re.compile(u"Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
314 txtls=txts.split("\n")
\r
318 sizetxt=rM.group(1)
\r
320 tX=int(sizetxt.split("x")[0])
\r
321 tY=int(sizetxt.split("x")[1])
\r
322 tEX=int(partxt.split(":")[0])
\r
323 tEY=int(partxt.split(":")[1])
\r
327 os.remove(pin+".size.ts")
\r
328 return [sizeMaxX,sizeMaxY]
\r
329 def getMoviePAR(pin):
\r
330 ffmpeg=configreader.getConfPath("ffmpeg")
\r
331 os.environ['LANG']="ja_JP.UTF-8"
\r
332 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
\r
333 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
334 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
\r
338 txtls=txts.split("\n")
\r
342 sizetxt=rM.group(1)
\r
344 tX=int(sizetxt.split("x")[0])
\r
345 tY=int(sizetxt.split("x")[1])
\r
346 tEX=int(dartxt.split(":")[0])
\r
347 tEY=int(dartxt.split(":")[1])
\r
350 if tX==1920 and tY==1080:
\r
357 def getMoviePAR2(pin):
\r
358 ffmpeg=configreader.getConfPath("ffmpeg")
\r
359 os.environ['LANG']="ja_JP.UTF-8"
\r
360 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 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
366 txtls=txts.split("\n")
\r
370 sizetxt=rM.group(1)
\r
372 tX=int(sizetxt.split("x")[0])
\r
373 tY=int(sizetxt.split("x")[1])
\r
374 tEX=int(dartxt.split(":")[0])
\r
375 tEY=int(dartxt.split(":")[1])
\r
378 if tX==1920 and tY==1080:
\r
384 os.remove(pin+".size.ts")
\r
386 def getMovieDAR(pin):
\r
387 ffmpeg=configreader.getConfPath("ffmpeg")
\r
388 os.environ['LANG']="ja_JP.UTF-8"
\r
389 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
\r
390 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
391 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
\r
395 txtls=txts.split("\n")
\r
399 sizetxt=rM.group(1)
\r
401 tX=int(sizetxt.split("x")[0])
\r
402 tY=int(sizetxt.split("x")[1])
\r
403 tEX=int(dartxt.split(":")[0])
\r
404 tEY=int(dartxt.split(":")[1])
\r
407 if tX==1920 and tY==1080:
\r
414 def getMovieDAR2(pin):
\r
415 ffmpeg=configreader.getConfPath("ffmpeg")
\r
416 os.environ['LANG']="ja_JP.UTF-8"
\r
417 exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"
\r
418 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
\r
419 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
\r
423 txtls=txts.split("\n")
\r
427 sizetxt=rM.group(1)
\r
429 tX=int(sizetxt.split("x")[0])
\r
430 tY=int(sizetxt.split("x")[1])
\r
431 tEX=int(dartxt.split(":")[0])
\r
432 tEY=int(dartxt.split(":")[1])
\r
435 if tX==1920 and tY==1080:
\r
441 os.remove(pin+".size.ts")
\r
443 def getParSize(pin,y):
\r
444 tSize=getMovieBaseSize2(pin)
\r
446 tX=tSize[0]*10*y/tSize[1]
\r
448 if tX>int(tX/10)*10:
\r
456 def getX264CoreVersion():
\r
457 x264=configreader.getConfPath("x264")
\r
458 t1=commands.getoutput(x264+" --help|grep core")
\r
459 rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
\r
465 def getX264Commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=-1,bitrate=0):
\r
466 x264=configreader.getConfPath("x264")
\r
467 os.environ['LANG']="ja_JP.UTF-8"
\r
468 x264_sar="--sar "+sar
\r
469 x264preset=u"--preset "+preset
\r
470 x264fps="--fps "+fps
\r
472 x264bitrate=u"--bitrate "+str(bitrate)
\r
474 x264crf=u"--crf "+str(crf)
\r
475 x264_addline=configreader.getConfEnv("x264_addline")
\r
478 xtt=configreader.getConfEnv("x264_thread")
\r
481 x264_thread=str(xtt)
\r
484 x264_addline=configreader.getConfEnv("x264_addline")
\r
485 if getX264CoreVersion()>103:
\r
486 x264res=u"--input-res "+x264streamsize
\r
487 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
489 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