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"
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 x264sar=str(tsar[0])+":"+str(tsar[1])
196 tsize=get_par_size(pin,720)
197 s = "-s "+str(tsize[0])+"x720 "
198 x264streamsize=str(tsize[0])+u"x720"
201 tsize=get_par_size(pin,480)
202 s = "-s "+str(tsize[0])+"x480 "
203 x264streamsize=str(tsize[0])+u"x480"
205 elif size == "FullHD":
206 tsize=get_par_size(pin,1080)
207 s = "-s "+str(tsize[0])+"x1080 "
208 x264streamsize=str(tsize[0])+u"x1080"
211 tsize=get_par_size(pin,480)
212 s = "-s "+str(tsize[0])+"x480 "
213 x264streamsize=str(tsize[0])+u"x480"
215 elif size == "QVGA_BASE":
216 tsize=get_par_size(pin,240)
217 s = "-s "+str(tsize[0])+"x240 "
218 x264streamsize=str(tsize[0])+u"x240"
220 elif size == "WVGA_BASE":
221 tsize=get_par_size(pin,480)
222 s = "-s "+str(tsize[0])+"x480 "
223 x264streamsize=str(tsize[0])+u"x480"
226 tsize=get_par_size(pin,720)
227 s = "-s "+str(tsize[0])+"x720 "
228 x264streamsize=str(tsize[0])+u"x720"
231 x264preset=u"ultrafast"
233 x264preset=u"veryfast"
242 if size == "WVGA_BASE" or size == "QVGA_BASE":
243 x264profile=" --level 32 --profile baseline "
245 x264profile=" --level 42 --profile high "
248 os.environ['LANG']="ja_JP.UTF-8"
249 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - | "
250 exe=exe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)
251 #exe=exe+u" nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
252 exe = "nice -n 19 " + exe
254 recdblist.printutf8(exe)
256 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
259 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
261 def getMovieBaseSize(pin):
262 ffmpeg=configreader.getpath("ffmpeg")
263 os.environ['LANG']="ja_JP.UTF-8"
264 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
265 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
266 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
269 txtls=txts.split("\n")
275 tX=int(sizetxt.split("x")[0])
276 tY=int(sizetxt.split("x")[1])
277 tEX=int(partxt.split(":")[0])
278 tEY=int(partxt.split(":")[1])
282 return [sizeMaxX,sizeMaxY]
283 def getMoviePAR(pin):
284 ffmpeg=configreader.getpath("ffmpeg")
285 os.environ['LANG']="ja_JP.UTF-8"
286 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
287 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
288 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
289 #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
293 txtls=txts.split("\n")
299 tX=int(sizetxt.split("x")[0])
300 tY=int(sizetxt.split("x")[1])
301 tEX=int(dartxt.split(":")[0])
302 tEY=int(dartxt.split(":")[1])
305 if tX==1920 and tY==1080:
312 def getMovieDAR(pin):
313 ffmpeg=configreader.getpath("ffmpeg")
314 os.environ['LANG']="ja_JP.UTF-8"
315 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
316 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
317 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
321 txtls=txts.split("\n")
327 tX=int(sizetxt.split("x")[0])
328 tY=int(sizetxt.split("x")[1])
329 tEX=int(dartxt.split(":")[0])
330 tEY=int(dartxt.split(":")[1])
333 if tX==1920 and tY==1080:
340 def get_par_size(pin,y):
341 tSize=getMovieBaseSize(pin)
342 tX=tSize[0]*10*y/tSize[1]
350 def get_x264core_version():
351 x264=configreader.getpath("x264")
352 t1=commands.getoutput(x264+" --help|grep core")
353 rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
359 def get_x264_commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=0,bitrate=0):
360 x264=configreader.getpath("x264")
361 os.environ['LANG']="ja_JP.UTF-8"
362 x264_sar="--sar "+sar
363 x264preset=u"--preset "+preset
366 x264bitrate=u"--bitrate "+str(bitrate)
368 x264crf=u"--crf "+str(crf)
369 x264_addline=configreader.getenv("x264_addline")
372 xtt=configreader.getenv("x264_thread")
378 x264_addline=configreader.getenv("x264_addline")
379 if get_x264core_version()>103:
380 x264res=u"--input-res "+x264streamsize
381 exe=u"nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" "+x264res+" -o \""+pout+"\" "+pin
383 exe=u"nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" "+pin+" "+x264streamsize