3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
13 def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
15 pinで指定されたファイルをpoutにx264でエンコードして書き出す
22 quality=int(configreader.getenv("x264_preset"))
23 crf=int(configreader.getenv("crf"))
25 if re.search("H", opts):
27 if re.search("S", opts):
29 if re.search("F", opts):
31 if re.search("W",opts):
33 if re.search("MW8", opts):
36 if re.search("MW9", opts):
39 if re.search("v", opts):
41 crf=int(configreader.getenv("animation_crf"))
42 if re.search("a", opts):
44 crf=int(configreader.getenv("animation_crf"))
45 if re.search("I", opts):
47 if re.search("q",opts):
49 if re.search("w",opts):
51 if re.search("e",opts):
53 if re.search("r",opts):
55 if re.search("u",opts):
57 if re.search("i",opts):
59 if re.search("o",opts):
61 if re.search("p",opts):
63 if re.search("d",opts):
64 encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace)
65 elif re.search("5",opts):
66 encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace)
67 elif re.search("b",opts):
69 tm2v=pin.replace(".ts",".m2v")
70 encode_sar(tm2v, pout,size,is24fps,quality,crf,deinterlace)
71 except Exception, inst:
72 recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst))
75 encode_sar(pin, pout,size,is24fps,quality,crf,deinterlace)
76 except Exception, inst:
77 recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst))
78 def encode_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
79 mencoder=configreader.getpath("mencoder")
80 x264=configreader.getpath("x264")
83 encvf="-vf yadif=0,pp=l5"
84 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
85 ofps="-ofps 30000/1001"
87 x264fps="--fps 30000/1001"
94 x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
96 xtt=configreader.getenv("x264_thread")
102 x264_addline=configreader.getenv("x264_addline")
104 ofps="-ofps 24000/1001"
105 fps="-fps 30000/1001"
106 x264fps="--fps 24000/1001"
107 x264tune="--tune animation"
108 encvf="-vf pullup,softskip"
109 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
111 ofps="-ofps 60000/1001"
112 fps="-fps 60000/1001"
113 x264fps="--fps 60000/1001"
114 x264tune="--tune animation --tff --nal-hrd vbr"
115 encvf="-vf hqdn3d=2:1:2"
118 tsize=get_par_size(pin,720)
119 encvf = encvf + ",scale=-3:720::0:3"+harddup
120 x264streamsize=str(tsize[0])+u"x720"
123 tsize=get_par_size(pin,480)
124 encvf = encvf + ",scale=-3:480::0:3"+harddup
125 x264streamsize=str(tsize[0])+u"x480"
127 elif size == "FullHD":
128 tsize=get_par_size(pin,1080)
129 encvf = encvf + ",scale=-3:1080::0:3"+harddup
130 x264streamsize=str(tsize[0])+u"x1080"
132 elif size == "QVGA_BASE":
133 tsize=get_par_size(pin,240)
134 encvf = encvf + ",scale=-3:240::0:3"+harddup
135 x264streamsize=str(tsize[0])+u"x240"
137 elif size == "WVGA_BASE":
138 tsize=get_par_size(pin,480)
139 encvf = encvf + ",scale=-3:480::0:3"+harddup
140 x264streamsize=str(tsize[0])+u"x480"
143 tsize=get_par_size(pin,720)
144 encvf = encvf + ",scale=-3:720::0:3"+harddup
145 x264streamsize=str(tsize[0])+u"x720"
148 x264preset=u"--preset ultrafast"
150 x264preset=u"--preset veryfast"
152 x264preset=u"--preset fast"
154 x264preset=u"--preset medium"
156 x264preset=u"--preset slow"
158 x264preset=u"--preset slower"
159 if size == "WVGA_BASE" or size == "QVGA_BASE":
160 x264profile=" --level 32 --profile baseline "
162 x264profile=" --level 42 --profile high "
163 x264crf=u"--crf "+str(crf)
164 os.environ['LANG']="ja_JP.UTF-8"
167 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 999999)))
168 os.system(u"mkfifo "+streampath)
169 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet -sws 9 "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" &"
170 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
171 encexe=u"nice -n 19 " +encexe
172 recdblist.printutf8(encexe)
175 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
178 os.system("rm "+streampath)
179 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
182 def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
186 ffmpeg=configreader.getpath("ffmpeg")
187 x264=configreader.getpath("x264")
189 x264fps="--fps 30000/1001"
195 tsar=getMoviePAR(pin)
196 x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
198 xtt=configreader.getenv("x264_thread")
204 x264_addline=configreader.getenv("x264_addline")
206 tsize=get_par_size(pin,720)
207 s = "-s "+str(tsize[0])+"x720 "
208 x264streamsize=str(tsize[0])+u"x720"
211 tsize=get_par_size(pin,480)
212 s = "-s "+str(tsize[0])+"x480 "
213 x264streamsize=str(tsize[0])+u"x480"
215 elif size == "FullHD":
216 tsize=get_par_size(pin,1080)
217 s = "-s "+str(tsize[0])+"x1080 "
218 x264streamsize=str(tsize[0])+u"x1080"
221 tsize=get_par_size(pin,480)
222 s = "-s "+str(tsize[0])+"x480 "
223 x264streamsize=str(tsize[0])+u"x480"
225 elif size == "QVGA_BASE":
226 tsize=get_par_size(pin,240)
227 s = "-s "+str(tsize[0])+"x240 "
228 x264streamsize=str(tsize[0])+u"x240"
230 elif size == "WVGA_BASE":
231 tsize=get_par_size(pin,480)
232 s = "-s "+str(tsize[0])+"x480 "
233 x264streamsize=str(tsize[0])+u"x480"
236 tsize=get_par_size(pin,720)
237 s = "-s "+str(tsize[0])+"x720 "
238 x264streamsize=str(tsize[0])+u"x720"
241 x264preset=u"--preset ultrafast"
243 x264preset=u"--preset veryfast"
245 x264preset=u"--preset fast"
247 x264preset=u"--preset medium"
249 x264preset=u"--preset slow"
251 x264preset=u"--preset slower"
252 if size == "WVGA_BASE" or size == "QVGA_BASE":
253 x264profile=" --level 32 --profile baseline "
255 x264profile=" --level 42 --profile high "
256 x264crf=u"--crf "+str(crf)
258 os.environ['LANG']="ja_JP.UTF-8"
259 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - |"
260 exe=exe+u" nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
261 exe = "nice -n 19 " + exe
263 recdblist.printutf8(exe)
265 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
268 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
269 def getMovieSize(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])
287 if tX==1920 and tY==1080:
291 if tX2>10*int(tX2/10):
298 return [sizeMaxX,sizeMaxY]
299 def getMovieBaseSize(pin):
300 ffmpeg=configreader.getpath("ffmpeg")
301 os.environ['LANG']="ja_JP.UTF-8"
302 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
303 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
304 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
307 txtls=txts.split("\n")
313 tX=int(sizetxt.split("x")[0])
314 tY=int(sizetxt.split("x")[1])
315 tEX=int(partxt.split(":")[0])
316 tEY=int(partxt.split(":")[1])
320 return [sizeMaxX,sizeMaxY]
321 def getMoviePAR(pin):
322 ffmpeg=configreader.getpath("ffmpeg")
323 os.environ['LANG']="ja_JP.UTF-8"
324 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
325 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
326 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
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 getMovieDAR(pin):
351 ffmpeg=configreader.getpath("ffmpeg")
352 os.environ['LANG']="ja_JP.UTF-8"
353 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
354 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
355 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
359 txtls=txts.split("\n")
365 tX=int(sizetxt.split("x")[0])
366 tY=int(sizetxt.split("x")[1])
367 tEX=int(dartxt.split(":")[0])
368 tEY=int(dartxt.split(":")[1])
371 if tX==1920 and tY==1080:
378 def get16_9ffmpeg_s(pin,x,y):
380 tDAR=getMovieDAR(pin)
381 if (tDAR[0]==16 and tDAR[1]==9):
386 tSize=getMovieSize(pin)
387 tn=x-tSize[1]*x/tSize[0]
393 def get_par_size(pin,y):
394 tSize=getMovieBaseSize(pin)
395 tX=tSize[0]*10*y/tSize[1]