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="-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"
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="-sws 9 -vf pullup,softskip"
109 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
111 tsize=get_par_size(pin,720)
112 encvf = encvf + ",scale=-3:720::0:3"+harddup
113 x264streamsize=str(tsize[0])+u"x720"
116 tsize=get_par_size(pin,480)
117 encvf = encvf + ",scale=-3:480::0:3"+harddup
118 x264streamsize=str(tsize[0])+u"x480"
120 elif size == "FullHD":
121 tsize=get_par_size(pin,1080)
122 encvf = encvf + ",scale=-3:1080::0:3"+harddup
123 x264streamsize=str(tsize[0])+u"x1080"
125 elif size == "QVGA_BASE":
126 tsize=get_par_size(pin,240)
127 encvf = encvf + ",scale=-3:240::0:3"+harddup
128 x264streamsize=str(tsize[0])+u"x240"
130 elif size == "WVGA_BASE":
131 tsize=get_par_size(pin,480)
132 encvf = encvf + ",scale=-3:480::0:3"+harddup
133 x264streamsize=str(tsize[0])+u"x480"
136 tsize=get_par_size(pin,720)
137 encvf = encvf + ",scale=-3:720::0:3"+harddup
138 x264streamsize=str(tsize[0])+u"x720"
141 ofps="-ofps 30000/1001"
142 fps="-fps 30000/1001"
143 x264fps="--fps 30000/1001"
144 x264tune="--tune animation --tff --nal-hrd vbr"
145 encvf="-vf hqdn3d=2:1:2"
149 x264preset=u"--preset ultrafast"
151 x264preset=u"--preset veryfast"
153 x264preset=u"--preset fast"
155 x264preset=u"--preset medium"
157 x264preset=u"--preset slow"
159 x264preset=u"--preset slower"
160 if size == "WVGA_BASE" or size == "QVGA_BASE":
161 x264profile=" --level 32 --profile baseline "
163 x264profile=" --level 42 --profile high "
164 x264crf=u"--crf "+str(crf)
165 os.environ['LANG']="ja_JP.UTF-8"
168 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 999999)))
169 os.system(u"mkfifo "+streampath)
170 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" &"
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")
188 x264=configreader.getpath("x264")
190 x264fps="--fps 30000/1001"
196 tsar=getMoviePAR(pin)
197 x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
199 xtt=configreader.getenv("x264_thread")
205 x264_addline=configreader.getenv("x264_addline")
207 tsize=get_par_size(pin,720)
208 s = "-s "+str(tsize[0])+"x720 "
209 x264streamsize=str(tsize[0])+u"x720"
212 tsize=get_par_size(pin,480)
213 s = "-s "+str(tsize[0])+"x480 "
214 x264streamsize=str(tsize[0])+u"x480"
216 elif size == "FullHD":
217 tsize=get_par_size(pin,1080)
218 s = "-s "+str(tsize[0])+"x1080 "
219 x264streamsize=str(tsize[0])+u"x1080"
222 tsize=get_par_size(pin,480)
223 s = "-s "+str(tsize[0])+"x480 "
224 x264streamsize=str(tsize[0])+u"x480"
226 elif size == "QVGA_BASE":
227 tsize=get_par_size(pin,240)
228 s = "-s "+str(tsize[0])+"x240 "
229 x264streamsize=str(tsize[0])+u"x240"
231 elif size == "WVGA_BASE":
232 tsize=get_par_size(pin,480)
233 s = "-s "+str(tsize[0])+"x480 "
234 x264streamsize=str(tsize[0])+u"x480"
237 tsize=get_par_size(pin,720)
238 s = "-s "+str(tsize[0])+"x720 "
239 x264streamsize=str(tsize[0])+u"x720"
242 x264preset=u"--preset ultrafast"
244 x264preset=u"--preset veryfast"
246 x264preset=u"--preset fast"
248 x264preset=u"--preset medium"
250 x264preset=u"--preset slow"
252 x264preset=u"--preset slower"
253 if size == "WVGA_BASE" or size == "QVGA_BASE":
254 x264profile=" --level 32 --profile baseline "
256 x264profile=" --level 42 --profile high "
257 x264crf=u"--crf "+str(crf)
259 os.environ['LANG']="ja_JP.UTF-8"
260 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - |"
261 exe=exe+u" nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
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)
270 def getMovieSize(pin):
271 ffmpeg=configreader.getpath("ffmpeg")
272 os.environ['LANG']="ja_JP.UTF-8"
273 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
274 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
275 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
278 txtls=txts.split("\n")
284 tX=int(sizetxt.split("x")[0])
285 tY=int(sizetxt.split("x")[1])
286 tEX=int(partxt.split(":")[0])
287 tEY=int(partxt.split(":")[1])
288 if tX==1920 and tY==1080:
292 if tX2>10*int(tX2/10):
299 return [sizeMaxX,sizeMaxY]
300 def getMovieBaseSize(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")
308 txtls=txts.split("\n")
314 tX=int(sizetxt.split("x")[0])
315 tY=int(sizetxt.split("x")[1])
316 tEX=int(partxt.split(":")[0])
317 tEY=int(partxt.split(":")[1])
321 return [sizeMaxX,sizeMaxY]
322 def getMoviePAR(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+PAR\D+(\d+:\d+)\D+.*\Z")
328 #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
332 txtls=txts.split("\n")
338 tX=int(sizetxt.split("x")[0])
339 tY=int(sizetxt.split("x")[1])
340 tEX=int(dartxt.split(":")[0])
341 tEY=int(dartxt.split(":")[1])
344 if tX==1920 and tY==1080:
351 def getMovieDAR(pin):
352 ffmpeg=configreader.getpath("ffmpeg")
353 os.environ['LANG']="ja_JP.UTF-8"
354 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
355 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
356 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
360 txtls=txts.split("\n")
366 tX=int(sizetxt.split("x")[0])
367 tY=int(sizetxt.split("x")[1])
368 tEX=int(dartxt.split(":")[0])
369 tEY=int(dartxt.split(":")[1])
372 if tX==1920 and tY==1080:
379 def get16_9ffmpeg_s(pin,x,y):
381 tDAR=getMovieDAR(pin)
382 if (tDAR[0]==16 and tDAR[1]==9):
387 tSize=getMovieSize(pin)
388 tn=x-tSize[1]*x/tSize[0]
394 def get_par_size(pin,y):
395 tSize=getMovieBaseSize(pin)
396 tX=tSize[0]*10*y/tSize[1]