3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
15 def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
17 pinで指定されたファイルをpoutにx264でエンコードして書き出す
24 quality=int(configreader.getenv("x264_preset"))
25 crf=int(configreader.getenv("crf"))
27 if re.search("H", opts):
29 if re.search("S", opts):
31 if re.search("F", opts):
33 if re.search("W",opts):
35 if re.search("MW8", opts):
38 if re.search("MW9", opts):
41 if re.search("v", opts):
43 crf=int(configreader.getenv("animation_crf"))
44 if re.search("a", opts):
46 crf=int(configreader.getenv("animation_crf"))
47 if re.search("I", opts):
49 if re.search("q",opts):
51 if re.search("w",opts):
53 if re.search("e",opts):
55 if re.search("r",opts):
57 if re.search("u",opts):
59 if re.search("i",opts):
61 if re.search("o",opts):
63 if re.search("p",opts):
65 if re.search("d",opts):#二カ国語放送の場合
66 encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace)
67 elif re.search("5",opts):#5.1chの場合
68 encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace)
69 elif re.search("b",opts):#m2vテスト
71 tm2v=pin.replace(".ts",".m2v")
72 encode_sar(tm2v, pout,size,is24fps,quality,crf,deinterlace)
73 except Exception, inst:
74 recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
75 elif re.search("x",opts):#videoframe補完テスト
76 tm2v=pin.replace(".ts",".m2v")
77 encode_ffmpeg_sar(tm2v,pout,size,is24fps,quality,crf,deinterlace)
80 encode_sar(pin, pout,size,is24fps,quality,crf,deinterlace)
81 except Exception, inst:
82 recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)
83 def encode_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
84 mencoder=configreader.getpath("mencoder")
87 encvf="-sws 9 -vf yadif=0,pp=l5"
88 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
89 ofps="-ofps 30000/1001"
97 x264sar=str(tsar[0])+":"+str(tsar[1])
99 ofps="-ofps 24000/1001"
100 fps="-fps 30000/1001"
102 x264tune="--tune animation"
103 encvf="-sws 9 -vf pullup,softskip"
104 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
106 tsize=get_par_size(pin,720)
107 encvf = encvf + ",scale=-3:720::0:3"+harddup
108 x264streamsize=str(tsize[0])+u"x720"
111 tsize=get_par_size(pin,480)
112 encvf = encvf + ",scale=-3:480::0:3"+harddup
113 x264streamsize=str(tsize[0])+u"x480"
115 elif size == "FullHD":
116 tsize=get_par_size(pin,1080)
117 encvf = encvf + ",scale=-3:1080::0:3"+harddup
118 x264streamsize=str(tsize[0])+u"x1080"
120 elif size == "QVGA_BASE":
121 tsize=get_par_size(pin,240)
122 encvf = encvf + ",scale=-3:240::0:3"+harddup
123 x264streamsize=str(tsize[0])+u"x240"
125 elif size == "WVGA_BASE":
126 tsize=get_par_size(pin,480)
127 encvf = encvf + ",scale=-3:480::0:3"+harddup
128 x264streamsize=str(tsize[0])+u"x480"
131 tsize=get_par_size(pin,720)
132 encvf = encvf + ",scale=-3:720::0:3"+harddup
133 x264streamsize=str(tsize[0])+u"x720"
136 tsize=getMovieBaseSize(pin)
137 ofps="-ofps 30000/1001"
138 #fps="-fps 30000/1001"
141 x264tune=x264tune+" --tff --nal-hrd vbr"
142 encvf="-vf hqdn3d=2:1:2"
145 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
146 if tsize[0] <= 0 or tsize[1] <= 0:
147 encvf="-sws 9 -vf yadif=0,pp=l5"
148 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
149 ofps="-ofps 30000/1001"
150 fps="-fps 30000/1001"
153 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
154 x264streamsize=u"1280x720"
157 x264preset=u"ultrafast"
159 x264preset=u"veryfast"
168 if size == "WVGA_BASE" or size == "QVGA_BASE":
169 x264profile=" --level 32 --profile baseline "
171 x264profile=" --level 42 --profile high "
173 os.environ['LANG']="ja_JP.UTF-8"
176 temptime=int(time.time())
177 temptime=temptime % 9697
178 random.jumpahead(temptime)
179 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))
180 os.system(u"mkfifo "+streampath)
181 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" & "
182 encexe=encexe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf)
183 #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
184 encexe=u"nice -n 19 " +encexe
185 recdblist.printutf8(encexe)
188 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8','ignore')
191 os.system("rm "+streampath)
192 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
195 def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
199 ffmpeg=configreader.getpath("ffmpeg")
207 tsar=getMoviePAR(pin)
208 filter="-deinterlace"
209 x264sar=str(tsar[0])+":"+str(tsar[1])
211 tsize=get_par_size(pin,720)
212 s = "-s "+str(tsize[0])+"x720 "
213 x264streamsize=str(tsize[0])+u"x720"
216 tsize=get_par_size(pin,480)
217 s = "-s "+str(tsize[0])+"x480 "
218 x264streamsize=str(tsize[0])+u"x480"
220 elif size == "FullHD":
221 tsize=get_par_size(pin,1080)
222 s = "-s "+str(tsize[0])+"x1080 "
223 x264streamsize=str(tsize[0])+u"x1080"
226 tsize=get_par_size(pin,480)
227 s = "-s "+str(tsize[0])+"x480 "
228 x264streamsize=str(tsize[0])+u"x480"
230 elif size == "QVGA_BASE":
231 tsize=get_par_size(pin,240)
232 s = "-s "+str(tsize[0])+"x240 "
233 x264streamsize=str(tsize[0])+u"x240"
235 elif size == "WVGA_BASE":
236 tsize=get_par_size(pin,480)
237 s = "-s "+str(tsize[0])+"x480 "
238 x264streamsize=str(tsize[0])+u"x480"
241 tsize=get_par_size(pin,720)
242 s = "-s "+str(tsize[0])+"x720 "
243 x264streamsize=str(tsize[0])+u"x720"
246 tsize=getMovieBaseSize(pin)
248 s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "
250 x264tune=x264tune+" --tff --nal-hrd vbr"
252 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
254 x264preset=u"ultrafast"
256 x264preset=u"veryfast"
265 if size == "WVGA_BASE" or size == "QVGA_BASE":
266 x264profile=" --level 32 --profile baseline "
268 x264profile=" --level 42 --profile high "
271 os.environ['LANG']="ja_JP.UTF-8"
272 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - | "
273 exe=exe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)
274 exe = "nice -n 19 " + exe
276 recdblist.printutf8(exe)
278 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
281 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
283 def getMovieBaseSize(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','ignore')
288 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
291 txtls=txts.split("\n")
297 tX=int(sizetxt.split("x")[0])
298 tY=int(sizetxt.split("x")[1])
299 tEX=int(partxt.split(":")[0])
300 tEY=int(partxt.split(":")[1])
304 return [sizeMaxX,sizeMaxY]
305 def getMoviePAR(pin):
306 ffmpeg=configreader.getpath("ffmpeg")
307 os.environ['LANG']="ja_JP.UTF-8"
308 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
309 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
310 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
311 #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
315 txtls=txts.split("\n")
321 tX=int(sizetxt.split("x")[0])
322 tY=int(sizetxt.split("x")[1])
323 tEX=int(dartxt.split(":")[0])
324 tEY=int(dartxt.split(":")[1])
327 if tX==1920 and tY==1080:
334 def getMovieDAR(pin):
335 ffmpeg=configreader.getpath("ffmpeg")
336 os.environ['LANG']="ja_JP.UTF-8"
337 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
338 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')
339 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
343 txtls=txts.split("\n")
349 tX=int(sizetxt.split("x")[0])
350 tY=int(sizetxt.split("x")[1])
351 tEX=int(dartxt.split(":")[0])
352 tEY=int(dartxt.split(":")[1])
355 if tX==1920 and tY==1080:
362 def get_par_size(pin,y):
363 tSize=getMovieBaseSize(pin)
365 tX=tSize[0]*10*y/tSize[1]
376 def get_x264core_version():
377 x264=configreader.getpath("x264")
378 t1=commands.getoutput(x264+" --help|grep core")
379 rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
385 def get_x264_commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=0,bitrate=0):
386 x264=configreader.getpath("x264")
387 os.environ['LANG']="ja_JP.UTF-8"
388 x264_sar="--sar "+sar
389 x264preset=u"--preset "+preset
392 x264bitrate=u"--bitrate "+str(bitrate)
394 x264crf=u"--crf "+str(crf)
395 x264_addline=configreader.getenv("x264_addline")
398 xtt=configreader.getenv("x264_thread")
404 x264_addline=configreader.getenv("x264_addline")
405 if get_x264core_version()>103:
406 x264res=u"--input-res "+x264streamsize
407 exe=u"nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --colormatrix bt709 --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" "+x264res+" -o \""+pout+"\" "+pin
409 exe=u"nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --colormatrix bt709 --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" "+pin+" "+x264streamsize