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")
81 x264=configreader.getpath("x264")
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"
88 x264fps="--fps 30000/1001"
95 x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
97 xtt=configreader.getenv("x264_thread")
103 x264_addline=configreader.getenv("x264_addline")
105 ofps="-ofps 24000/1001"
106 fps="-fps 30000/1001"
107 x264fps="--fps 24000/1001"
108 x264tune="--tune animation"
109 encvf="-sws 9 -vf pullup,softskip"
110 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
112 tsize=get_par_size(pin,720)
113 encvf = encvf + ",scale=-3:720::0:3"+harddup
114 x264streamsize=str(tsize[0])+u"x720"
117 tsize=get_par_size(pin,480)
118 encvf = encvf + ",scale=-3:480::0:3"+harddup
119 x264streamsize=str(tsize[0])+u"x480"
121 elif size == "FullHD":
122 tsize=get_par_size(pin,1080)
123 encvf = encvf + ",scale=-3:1080::0:3"+harddup
124 x264streamsize=str(tsize[0])+u"x1080"
126 elif size == "QVGA_BASE":
127 tsize=get_par_size(pin,240)
128 encvf = encvf + ",scale=-3:240::0:3"+harddup
129 x264streamsize=str(tsize[0])+u"x240"
131 elif size == "WVGA_BASE":
132 tsize=get_par_size(pin,480)
133 encvf = encvf + ",scale=-3:480::0:3"+harddup
134 x264streamsize=str(tsize[0])+u"x480"
137 tsize=get_par_size(pin,720)
138 encvf = encvf + ",scale=-3:720::0:3"+harddup
139 x264streamsize=str(tsize[0])+u"x720"
142 tsize=getMovieBaseSize(pin)
143 ofps="-ofps 30000/1001"
144 #fps="-fps 30000/1001"
146 x264fps="--fps 30000/1001"
147 x264tune=x264tune+" --tff --nal-hrd vbr"
148 encvf="-vf hqdn3d=2:1:2"
151 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
153 x264preset=u"--preset ultrafast"
155 x264preset=u"--preset veryfast"
157 x264preset=u"--preset fast"
159 x264preset=u"--preset medium"
161 x264preset=u"--preset slow"
163 x264preset=u"--preset slower"
164 if size == "WVGA_BASE" or size == "QVGA_BASE":
165 x264profile=" --level 32 --profile baseline "
167 x264profile=" --level 42 --profile high "
168 x264crf=u"--crf "+str(crf)
169 os.environ['LANG']="ja_JP.UTF-8"
172 temptime=int(time.time())
173 temptime=temptime % 9697
174 random.jumpahead(temptime)
175 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))
176 os.system(u"mkfifo "+streampath)
177 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" &"
178 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
179 encexe=u"nice -n 19 " +encexe
180 recdblist.printutf8(encexe)
183 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
186 os.system("rm "+streampath)
187 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
190 def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
194 ffmpeg=configreader.getpath("ffmpeg")
195 x264=configreader.getpath("x264")
197 x264fps="--fps 30000/1001"
203 tsar=getMoviePAR(pin)
204 x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
206 xtt=configreader.getenv("x264_thread")
212 x264_addline=configreader.getenv("x264_addline")
214 tsize=get_par_size(pin,720)
215 s = "-s "+str(tsize[0])+"x720 "
216 x264streamsize=str(tsize[0])+u"x720"
219 tsize=get_par_size(pin,480)
220 s = "-s "+str(tsize[0])+"x480 "
221 x264streamsize=str(tsize[0])+u"x480"
223 elif size == "FullHD":
224 tsize=get_par_size(pin,1080)
225 s = "-s "+str(tsize[0])+"x1080 "
226 x264streamsize=str(tsize[0])+u"x1080"
229 tsize=get_par_size(pin,480)
230 s = "-s "+str(tsize[0])+"x480 "
231 x264streamsize=str(tsize[0])+u"x480"
233 elif size == "QVGA_BASE":
234 tsize=get_par_size(pin,240)
235 s = "-s "+str(tsize[0])+"x240 "
236 x264streamsize=str(tsize[0])+u"x240"
238 elif size == "WVGA_BASE":
239 tsize=get_par_size(pin,480)
240 s = "-s "+str(tsize[0])+"x480 "
241 x264streamsize=str(tsize[0])+u"x480"
244 tsize=get_par_size(pin,720)
245 s = "-s "+str(tsize[0])+"x720 "
246 x264streamsize=str(tsize[0])+u"x720"
249 x264preset=u"--preset ultrafast"
251 x264preset=u"--preset veryfast"
253 x264preset=u"--preset fast"
255 x264preset=u"--preset medium"
257 x264preset=u"--preset slow"
259 x264preset=u"--preset slower"
260 if size == "WVGA_BASE" or size == "QVGA_BASE":
261 x264profile=" --level 32 --profile baseline "
263 x264profile=" --level 42 --profile high "
264 x264crf=u"--crf "+str(crf)
266 os.environ['LANG']="ja_JP.UTF-8"
267 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - |"
268 exe=exe+u" nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
269 exe = "nice -n 19 " + exe
271 recdblist.printutf8(exe)
273 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
276 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
278 def getMovieBaseSize(pin):
279 ffmpeg=configreader.getpath("ffmpeg")
280 os.environ['LANG']="ja_JP.UTF-8"
281 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
282 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
283 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
286 txtls=txts.split("\n")
292 tX=int(sizetxt.split("x")[0])
293 tY=int(sizetxt.split("x")[1])
294 tEX=int(partxt.split(":")[0])
295 tEY=int(partxt.split(":")[1])
299 return [sizeMaxX,sizeMaxY]
300 def getMoviePAR(pin):
301 ffmpeg=configreader.getpath("ffmpeg")
302 os.environ['LANG']="ja_JP.UTF-8"
303 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
304 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
305 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
306 #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
310 txtls=txts.split("\n")
316 tX=int(sizetxt.split("x")[0])
317 tY=int(sizetxt.split("x")[1])
318 tEX=int(dartxt.split(":")[0])
319 tEY=int(dartxt.split(":")[1])
322 if tX==1920 and tY==1080:
329 def getMovieDAR(pin):
330 ffmpeg=configreader.getpath("ffmpeg")
331 os.environ['LANG']="ja_JP.UTF-8"
332 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
333 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
334 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
338 txtls=txts.split("\n")
344 tX=int(sizetxt.split("x")[0])
345 tY=int(sizetxt.split("x")[1])
346 tEX=int(dartxt.split(":")[0])
347 tEY=int(dartxt.split(":")[1])
350 if tX==1920 and tY==1080:
357 def get_par_size(pin,y):
358 tSize=getMovieBaseSize(pin)
359 tX=tSize[0]*10*y/tSize[1]
367 def get_x264core_version():
368 x264=configreader.getpath("x264")
369 t1=commands.getoutput(x264+" --help|grep core")
370 rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")