3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
14 def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
16 pinで指定されたファイルをpoutにx264でエンコードして書き出す
23 quality=int(configreader.getenv("x264_preset"))
24 crf=int(configreader.getenv("crf"))
26 if re.search("H", opts):
28 if re.search("S", opts):
30 if re.search("F", opts):
32 if re.search("W",opts):
34 if re.search("MW8", opts):
37 if re.search("MW9", opts):
40 if re.search("v", opts):
42 crf=int(configreader.getenv("animation_crf"))
43 if re.search("a", opts):
45 crf=int(configreader.getenv("animation_crf"))
46 if re.search("I", opts):
48 if re.search("q",opts):
50 if re.search("w",opts):
52 if re.search("e",opts):
54 if re.search("r",opts):
56 if re.search("u",opts):
58 if re.search("i",opts):
60 if re.search("o",opts):
62 if re.search("p",opts):
64 if re.search("d",opts):
65 encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace)
66 elif re.search("5",opts):
67 encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace)
68 elif re.search("b",opts):
70 tm2v=pin.replace(".ts",".m2v")
71 encode_sar(tm2v, pout,size,is24fps,quality,crf,deinterlace)
72 except Exception, inst:
73 recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst),log_level=200)
76 encode_sar(pin, pout,size,is24fps,quality,crf,deinterlace)
77 except Exception, inst:
78 recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst),log_level=200)
79 def encode_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
80 mencoder=configreader.getpath("mencoder")
83 encvf="-sws 9 -vf yadif=0,pp=l5"
84 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
85 ofps="-ofps 30000/1001"
93 x264sar=str(tsar[0])+":"+str(tsar[1])
95 ofps="-ofps 24000/1001"
98 x264tune="--tune animation"
99 encvf="-sws 9 -vf pullup,softskip"
100 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
102 tsize=get_par_size(pin,720)
103 encvf = encvf + ",scale=-3:720::0:3"+harddup
104 x264streamsize=str(tsize[0])+u"x720"
107 tsize=get_par_size(pin,480)
108 encvf = encvf + ",scale=-3:480::0:3"+harddup
109 x264streamsize=str(tsize[0])+u"x480"
111 elif size == "FullHD":
112 tsize=get_par_size(pin,1080)
113 encvf = encvf + ",scale=-3:1080::0:3"+harddup
114 x264streamsize=str(tsize[0])+u"x1080"
116 elif size == "QVGA_BASE":
117 tsize=get_par_size(pin,240)
118 encvf = encvf + ",scale=-3:240::0:3"+harddup
119 x264streamsize=str(tsize[0])+u"x240"
121 elif size == "WVGA_BASE":
122 tsize=get_par_size(pin,480)
123 encvf = encvf + ",scale=-3:480::0:3"+harddup
124 x264streamsize=str(tsize[0])+u"x480"
127 tsize=get_par_size(pin,720)
128 encvf = encvf + ",scale=-3:720::0:3"+harddup
129 x264streamsize=str(tsize[0])+u"x720"
132 tsize=getMovieBaseSize(pin)
133 ofps="-ofps 30000/1001"
134 #fps="-fps 30000/1001"
137 x264tune=x264tune+" --tff --nal-hrd vbr"
138 encvf="-vf hqdn3d=2:1:2"
141 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
142 if tsize[0] <= 0 and tsize[1] <= 0:
145 x264preset=u"ultrafast"
147 x264preset=u"veryfast"
156 if size == "WVGA_BASE" or size == "QVGA_BASE":
157 x264profile=" --level 32 --profile baseline "
159 x264profile=" --level 42 --profile high "
161 os.environ['LANG']="ja_JP.UTF-8"
164 temptime=int(time.time())
165 temptime=temptime % 9697
166 random.jumpahead(temptime)
167 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))
168 os.system(u"mkfifo "+streampath)
169 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" & "
170 encexe=encexe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf)
171 #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
172 encexe=u"nice -n 19 " +encexe
173 recdblist.printutf8(encexe)
176 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
179 os.system("rm "+streampath)
180 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
183 def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
187 ffmpeg=configreader.getpath("ffmpeg")
195 tsar=getMoviePAR(pin)
196 filter="-deinterlace"
197 x264sar=str(tsar[0])+":"+str(tsar[1])
199 tsize=get_par_size(pin,720)
200 s = "-s "+str(tsize[0])+"x720 "
201 x264streamsize=str(tsize[0])+u"x720"
204 tsize=get_par_size(pin,480)
205 s = "-s "+str(tsize[0])+"x480 "
206 x264streamsize=str(tsize[0])+u"x480"
208 elif size == "FullHD":
209 tsize=get_par_size(pin,1080)
210 s = "-s "+str(tsize[0])+"x1080 "
211 x264streamsize=str(tsize[0])+u"x1080"
214 tsize=get_par_size(pin,480)
215 s = "-s "+str(tsize[0])+"x480 "
216 x264streamsize=str(tsize[0])+u"x480"
218 elif size == "QVGA_BASE":
219 tsize=get_par_size(pin,240)
220 s = "-s "+str(tsize[0])+"x240 "
221 x264streamsize=str(tsize[0])+u"x240"
223 elif size == "WVGA_BASE":
224 tsize=get_par_size(pin,480)
225 s = "-s "+str(tsize[0])+"x480 "
226 x264streamsize=str(tsize[0])+u"x480"
229 tsize=get_par_size(pin,720)
230 s = "-s "+str(tsize[0])+"x720 "
231 x264streamsize=str(tsize[0])+u"x720"
234 tsize=getMovieBaseSize(pin)
236 s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "
238 x264tune=x264tune+" --tff --nal-hrd vbr"
240 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
242 x264preset=u"ultrafast"
244 x264preset=u"veryfast"
253 if size == "WVGA_BASE" or size == "QVGA_BASE":
254 x264profile=" --level 32 --profile baseline "
256 x264profile=" --level 42 --profile high "
259 os.environ['LANG']="ja_JP.UTF-8"
260 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - | "
261 exe=exe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)
262 exe = "nice -n 19 " + exe
264 recdblist.printutf8(exe)
266 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
269 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
271 def getMovieBaseSize(pin):
272 ffmpeg=configreader.getpath("ffmpeg")
273 os.environ['LANG']="ja_JP.UTF-8"
274 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
275 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
276 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
279 txtls=txts.split("\n")
285 tX=int(sizetxt.split("x")[0])
286 tY=int(sizetxt.split("x")[1])
287 tEX=int(partxt.split(":")[0])
288 tEY=int(partxt.split(":")[1])
292 return [sizeMaxX,sizeMaxY]
293 def getMoviePAR(pin):
294 ffmpeg=configreader.getpath("ffmpeg")
295 os.environ['LANG']="ja_JP.UTF-8"
296 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
297 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
298 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
299 #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
303 txtls=txts.split("\n")
309 tX=int(sizetxt.split("x")[0])
310 tY=int(sizetxt.split("x")[1])
311 tEX=int(dartxt.split(":")[0])
312 tEY=int(dartxt.split(":")[1])
315 if tX==1920 and tY==1080:
322 def getMovieDAR(pin):
323 ffmpeg=configreader.getpath("ffmpeg")
324 os.environ['LANG']="ja_JP.UTF-8"
325 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
326 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
327 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
331 txtls=txts.split("\n")
337 tX=int(sizetxt.split("x")[0])
338 tY=int(sizetxt.split("x")[1])
339 tEX=int(dartxt.split(":")[0])
340 tEY=int(dartxt.split(":")[1])
343 if tX==1920 and tY==1080:
350 def get_par_size(pin,y):
351 tSize=getMovieBaseSize(pin)
353 tX=tSize[0]*10*y/tSize[1]
364 def get_x264core_version():
365 x264=configreader.getpath("x264")
366 t1=commands.getoutput(x264+" --help|grep core")
367 rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
373 def get_x264_commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=0,bitrate=0):
374 x264=configreader.getpath("x264")
375 os.environ['LANG']="ja_JP.UTF-8"
376 x264_sar="--sar "+sar
377 x264preset=u"--preset "+preset
380 x264bitrate=u"--bitrate "+str(bitrate)
382 x264crf=u"--crf "+str(crf)
383 x264_addline=configreader.getenv("x264_addline")
386 xtt=configreader.getenv("x264_thread")
392 x264_addline=configreader.getenv("x264_addline")
393 if get_x264core_version()>103:
394 x264res=u"--input-res "+x264streamsize
395 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
397 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