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))
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))
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])
143 x264preset=u"ultrafast"
145 x264preset=u"veryfast"
154 if size == "WVGA_BASE" or size == "QVGA_BASE":
155 x264profile=" --level 32 --profile baseline "
157 x264profile=" --level 42 --profile high "
159 os.environ['LANG']="ja_JP.UTF-8"
162 temptime=int(time.time())
163 temptime=temptime % 9697
164 random.jumpahead(temptime)
165 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))
166 os.system(u"mkfifo "+streampath)
167 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" & "
168 encexe=encexe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf)
169 #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
170 encexe=u"nice -n 19 " +encexe
171 recdblist.printutf8(encexe)
174 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
177 os.system("rm "+streampath)
178 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
181 def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
185 ffmpeg=configreader.getpath("ffmpeg")
193 tsar=getMoviePAR(pin)
194 filter="-deinterlace"
195 x264sar=str(tsar[0])+":"+str(tsar[1])
197 tsize=get_par_size(pin,720)
198 s = "-s "+str(tsize[0])+"x720 "
199 x264streamsize=str(tsize[0])+u"x720"
202 tsize=get_par_size(pin,480)
203 s = "-s "+str(tsize[0])+"x480 "
204 x264streamsize=str(tsize[0])+u"x480"
206 elif size == "FullHD":
207 tsize=get_par_size(pin,1080)
208 s = "-s "+str(tsize[0])+"x1080 "
209 x264streamsize=str(tsize[0])+u"x1080"
212 tsize=get_par_size(pin,480)
213 s = "-s "+str(tsize[0])+"x480 "
214 x264streamsize=str(tsize[0])+u"x480"
216 elif size == "QVGA_BASE":
217 tsize=get_par_size(pin,240)
218 s = "-s "+str(tsize[0])+"x240 "
219 x264streamsize=str(tsize[0])+u"x240"
221 elif size == "WVGA_BASE":
222 tsize=get_par_size(pin,480)
223 s = "-s "+str(tsize[0])+"x480 "
224 x264streamsize=str(tsize[0])+u"x480"
227 tsize=get_par_size(pin,720)
228 s = "-s "+str(tsize[0])+"x720 "
229 x264streamsize=str(tsize[0])+u"x720"
232 tsize=getMovieBaseSize(pin)
234 s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "
236 x264tune=x264tune+" --tff --nal-hrd vbr"
238 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
240 x264preset=u"ultrafast"
242 x264preset=u"veryfast"
251 if size == "WVGA_BASE" or size == "QVGA_BASE":
252 x264profile=" --level 32 --profile baseline "
254 x264profile=" --level 42 --profile high "
257 os.environ['LANG']="ja_JP.UTF-8"
258 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - | "
259 exe=exe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)
260 exe = "nice -n 19 " + exe
262 recdblist.printutf8(exe)
264 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
267 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
269 def getMovieBaseSize(pin):
270 ffmpeg=configreader.getpath("ffmpeg")
271 os.environ['LANG']="ja_JP.UTF-8"
272 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
273 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
274 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
277 txtls=txts.split("\n")
283 tX=int(sizetxt.split("x")[0])
284 tY=int(sizetxt.split("x")[1])
285 tEX=int(partxt.split(":")[0])
286 tEY=int(partxt.split(":")[1])
290 return [sizeMaxX,sizeMaxY]
291 def getMoviePAR(pin):
292 ffmpeg=configreader.getpath("ffmpeg")
293 os.environ['LANG']="ja_JP.UTF-8"
294 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
295 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
296 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
297 #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
301 txtls=txts.split("\n")
307 tX=int(sizetxt.split("x")[0])
308 tY=int(sizetxt.split("x")[1])
309 tEX=int(dartxt.split(":")[0])
310 tEY=int(dartxt.split(":")[1])
313 if tX==1920 and tY==1080:
320 def getMovieDAR(pin):
321 ffmpeg=configreader.getpath("ffmpeg")
322 os.environ['LANG']="ja_JP.UTF-8"
323 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
324 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
325 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
329 txtls=txts.split("\n")
335 tX=int(sizetxt.split("x")[0])
336 tY=int(sizetxt.split("x")[1])
337 tEX=int(dartxt.split(":")[0])
338 tEY=int(dartxt.split(":")[1])
341 if tX==1920 and tY==1080:
348 def get_par_size(pin,y):
349 tSize=getMovieBaseSize(pin)
350 tX=tSize[0]*10*y/tSize[1]
358 def get_x264core_version():
359 x264=configreader.getpath("x264")
360 t1=commands.getoutput(x264+" --help|grep core")
361 rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
367 def get_x264_commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=0,bitrate=0):
368 x264=configreader.getpath("x264")
369 os.environ['LANG']="ja_JP.UTF-8"
370 x264_sar="--sar "+sar
371 x264preset=u"--preset "+preset
374 x264bitrate=u"--bitrate "+str(bitrate)
376 x264crf=u"--crf "+str(crf)
377 x264_addline=configreader.getenv("x264_addline")
380 xtt=configreader.getenv("x264_thread")
386 x264_addline=configreader.getenv("x264_addline")
387 if get_x264core_version()>103:
388 x264res=u"--input-res "+x264streamsize
389 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
391 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