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=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)
270 def getMovieBaseSize(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])
291 return [sizeMaxX,sizeMaxY]
292 def getMoviePAR(pin):
293 ffmpeg=configreader.getpath("ffmpeg")
294 os.environ['LANG']="ja_JP.UTF-8"
295 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
296 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
297 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
298 #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
302 txtls=txts.split("\n")
308 tX=int(sizetxt.split("x")[0])
309 tY=int(sizetxt.split("x")[1])
310 tEX=int(dartxt.split(":")[0])
311 tEY=int(dartxt.split(":")[1])
314 if tX==1920 and tY==1080:
321 def getMovieDAR(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+.*DAR\D+(\d+:\d+)\D+.*\Z")
330 txtls=txts.split("\n")
336 tX=int(sizetxt.split("x")[0])
337 tY=int(sizetxt.split("x")[1])
338 tEX=int(dartxt.split(":")[0])
339 tEY=int(dartxt.split(":")[1])
342 if tX==1920 and tY==1080:
349 def get_par_size(pin,y):
350 tSize=getMovieBaseSize(pin)
351 tX=tSize[0]*10*y/tSize[1]
359 def get_x264core_version():
360 x264=configreader.getpath("x264")
361 t1=commands.getoutput(x264+" --help|grep core")
362 rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
368 def get_x264_commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=0,bitrate=0):
369 x264=configreader.getpath("x264")
370 os.environ['LANG']="ja_JP.UTF-8"
371 x264_sar="--sar "+sar
372 x264preset=u"--preset "+preset
375 x264bitrate=u"--bitrate "+str(bitrate)
377 x264crf=u"--crf "+str(crf)
378 x264_addline=configreader.getenv("x264_addline")
381 xtt=configreader.getenv("x264_thread")
387 x264_addline=configreader.getenv("x264_addline")
388 if get_x264core_version()>103:
389 x264res=u"--input-res "+x264streamsize
390 exe=u"nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" "+x264res+" -o \""+pout+"\" "+pin
392 exe=u"nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" "+pin+" "+x264streamsize