3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
15 def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
17 pinで指定されたファイルをpoutにx264でエンコードして書き出す
24 quality=int(configreader.getenv("x264_preset"))
25 crf=int(configreader.getenv("crf"))
27 if re.search("H", opts):
29 if re.search("S", opts):
31 if re.search("F", opts):
33 if re.search("W",opts):
35 if re.search("MW8", opts):
38 if re.search("MW9", opts):
41 if re.search("v", opts):
43 crf=int(configreader.getenv("animation_crf"))
44 if re.search("a", opts):
46 crf=int(configreader.getenv("animation_crf"))
47 if re.search("I", opts):
49 if re.search("q",opts):
51 if re.search("w",opts):
53 if re.search("e",opts):
55 if re.search("r",opts):
57 if re.search("u",opts):
59 if re.search("i",opts):
61 if re.search("o",opts):
63 if re.search("p",opts):
65 if re.search("d",opts):
66 encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace)
67 elif re.search("5",opts):
68 encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace)
69 elif re.search("b",opts):
71 tm2v=pin.replace(".ts",".m2v")
72 encode_sar(tm2v, pout,size,is24fps,quality,crf,deinterlace)
73 except Exception, inst:
74 recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
77 encode_sar(pin, pout,size,is24fps,quality,crf,deinterlace)
78 except Exception, inst:
79 recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
80 def encode_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
81 mencoder=configreader.getpath("mencoder")
84 encvf="-sws 9 -vf yadif=0,pp=l5"
85 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
86 ofps="-ofps 30000/1001"
94 x264sar=str(tsar[0])+":"+str(tsar[1])
96 ofps="-ofps 24000/1001"
99 x264tune="--tune animation"
100 encvf="-sws 9 -vf pullup,softskip"
101 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
103 tsize=get_par_size(pin,720)
104 encvf = encvf + ",scale=-3:720::0:3"+harddup
105 x264streamsize=str(tsize[0])+u"x720"
108 tsize=get_par_size(pin,480)
109 encvf = encvf + ",scale=-3:480::0:3"+harddup
110 x264streamsize=str(tsize[0])+u"x480"
112 elif size == "FullHD":
113 tsize=get_par_size(pin,1080)
114 encvf = encvf + ",scale=-3:1080::0:3"+harddup
115 x264streamsize=str(tsize[0])+u"x1080"
117 elif size == "QVGA_BASE":
118 tsize=get_par_size(pin,240)
119 encvf = encvf + ",scale=-3:240::0:3"+harddup
120 x264streamsize=str(tsize[0])+u"x240"
122 elif size == "WVGA_BASE":
123 tsize=get_par_size(pin,480)
124 encvf = encvf + ",scale=-3:480::0:3"+harddup
125 x264streamsize=str(tsize[0])+u"x480"
128 tsize=get_par_size(pin,720)
129 encvf = encvf + ",scale=-3:720::0:3"+harddup
130 x264streamsize=str(tsize[0])+u"x720"
133 tsize=getMovieBaseSize(pin)
134 ofps="-ofps 30000/1001"
135 #fps="-fps 30000/1001"
138 x264tune=x264tune+" --tff --nal-hrd vbr"
139 encvf="-vf hqdn3d=2:1:2"
142 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
143 if tsize[0] <= 0 or tsize[1] <= 0:
144 encvf="-sws 9 -vf yadif=0,pp=l5"
145 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
146 ofps="-ofps 30000/1001"
147 fps="-fps 30000/1001"
150 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
151 x264streamsize=u"1280x720"
154 x264preset=u"ultrafast"
156 x264preset=u"veryfast"
165 if size == "WVGA_BASE" or size == "QVGA_BASE":
166 x264profile=" --level 32 --profile baseline "
168 x264profile=" --level 42 --profile high "
170 os.environ['LANG']="ja_JP.UTF-8"
173 temptime=int(time.time())
174 temptime=temptime % 9697
175 random.jumpahead(temptime)
176 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))
177 os.system(u"mkfifo "+streampath)
178 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" & "
179 encexe=encexe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf)
180 #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
181 encexe=u"nice -n 19 " +encexe
182 recdblist.printutf8(encexe)
185 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8','ignore')
188 os.system("rm "+streampath)
189 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
192 def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
196 ffmpeg=configreader.getpath("ffmpeg")
204 tsar=getMoviePAR(pin)
205 filter="-deinterlace"
206 x264sar=str(tsar[0])+":"+str(tsar[1])
208 tsize=get_par_size(pin,720)
209 s = "-s "+str(tsize[0])+"x720 "
210 x264streamsize=str(tsize[0])+u"x720"
213 tsize=get_par_size(pin,480)
214 s = "-s "+str(tsize[0])+"x480 "
215 x264streamsize=str(tsize[0])+u"x480"
217 elif size == "FullHD":
218 tsize=get_par_size(pin,1080)
219 s = "-s "+str(tsize[0])+"x1080 "
220 x264streamsize=str(tsize[0])+u"x1080"
223 tsize=get_par_size(pin,480)
224 s = "-s "+str(tsize[0])+"x480 "
225 x264streamsize=str(tsize[0])+u"x480"
227 elif size == "QVGA_BASE":
228 tsize=get_par_size(pin,240)
229 s = "-s "+str(tsize[0])+"x240 "
230 x264streamsize=str(tsize[0])+u"x240"
232 elif size == "WVGA_BASE":
233 tsize=get_par_size(pin,480)
234 s = "-s "+str(tsize[0])+"x480 "
235 x264streamsize=str(tsize[0])+u"x480"
238 tsize=get_par_size(pin,720)
239 s = "-s "+str(tsize[0])+"x720 "
240 x264streamsize=str(tsize[0])+u"x720"
243 tsize=getMovieBaseSize(pin)
245 s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "
247 x264tune=x264tune+" --tff --nal-hrd vbr"
249 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
251 x264preset=u"ultrafast"
253 x264preset=u"veryfast"
262 if size == "WVGA_BASE" or size == "QVGA_BASE":
263 x264profile=" --level 32 --profile baseline "
265 x264profile=" --level 42 --profile high "
268 os.environ['LANG']="ja_JP.UTF-8"
269 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - | "
270 exe=exe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)
271 exe = "nice -n 19 " + exe
273 recdblist.printutf8(exe)
275 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
278 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
280 def getMovieBaseSize(pin):
281 ffmpeg=configreader.getpath("ffmpeg")
282 os.environ['LANG']="ja_JP.UTF-8"
283 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
284 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
285 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
288 txtls=txts.split("\n")
294 tX=int(sizetxt.split("x")[0])
295 tY=int(sizetxt.split("x")[1])
296 tEX=int(partxt.split(":")[0])
297 tEY=int(partxt.split(":")[1])
301 return [sizeMaxX,sizeMaxY]
302 def getMoviePAR(pin):
303 ffmpeg=configreader.getpath("ffmpeg")
304 os.environ['LANG']="ja_JP.UTF-8"
305 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
306 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
307 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
308 #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
312 txtls=txts.split("\n")
318 tX=int(sizetxt.split("x")[0])
319 tY=int(sizetxt.split("x")[1])
320 tEX=int(dartxt.split(":")[0])
321 tEY=int(dartxt.split(":")[1])
324 if tX==1920 and tY==1080:
331 def getMovieDAR(pin):
332 ffmpeg=configreader.getpath("ffmpeg")
333 os.environ['LANG']="ja_JP.UTF-8"
334 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
335 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
336 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
340 txtls=txts.split("\n")
346 tX=int(sizetxt.split("x")[0])
347 tY=int(sizetxt.split("x")[1])
348 tEX=int(dartxt.split(":")[0])
349 tEY=int(dartxt.split(":")[1])
352 if tX==1920 and tY==1080:
359 def get_par_size(pin,y):
360 tSize=getMovieBaseSize(pin)
362 tX=tSize[0]*10*y/tSize[1]
373 def get_x264core_version():
374 x264=configreader.getpath("x264")
375 t1=commands.getoutput(x264+" --help|grep core")
376 rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
382 def get_x264_commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=0,bitrate=0):
383 x264=configreader.getpath("x264")
384 os.environ['LANG']="ja_JP.UTF-8"
385 x264_sar="--sar "+sar
386 x264preset=u"--preset "+preset
389 x264bitrate=u"--bitrate "+str(bitrate)
391 x264crf=u"--crf "+str(crf)
392 x264_addline=configreader.getenv("x264_addline")
395 xtt=configreader.getenv("x264_thread")
401 x264_addline=configreader.getenv("x264_addline")
402 if get_x264core_version()>103:
403 x264res=u"--input-res "+x264streamsize
404 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
406 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