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 tsize=getMovieBaseSize(pin)
142 ofps="-ofps 30000/1001"
143 fps="-fps 30000/1001"
144 x264fps="--fps 30000/1001"
145 x264tune=x264tune+" --tff --nal-hrd vbr"
146 encvf="-vf hqdn3d=2:1:2"
149 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
151 x264preset=u"--preset ultrafast"
153 x264preset=u"--preset veryfast"
155 x264preset=u"--preset fast"
157 x264preset=u"--preset medium"
159 x264preset=u"--preset slow"
161 x264preset=u"--preset slower"
162 if size == "WVGA_BASE" or size == "QVGA_BASE":
163 x264profile=" --level 32 --profile baseline "
165 x264profile=" --level 42 --profile high "
166 x264crf=u"--crf "+str(crf)
167 os.environ['LANG']="ja_JP.UTF-8"
170 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 999999)))
171 os.system(u"mkfifo "+streampath)
172 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" &"
173 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
174 encexe=u"nice -n 19 " +encexe
175 recdblist.printutf8(encexe)
178 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
181 os.system("rm "+streampath)
182 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
185 def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
189 ffmpeg=configreader.getpath("ffmpeg")
190 x264=configreader.getpath("x264")
192 x264fps="--fps 30000/1001"
198 tsar=getMoviePAR(pin)
199 x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
201 xtt=configreader.getenv("x264_thread")
207 x264_addline=configreader.getenv("x264_addline")
209 tsize=get_par_size(pin,720)
210 s = "-s "+str(tsize[0])+"x720 "
211 x264streamsize=str(tsize[0])+u"x720"
214 tsize=get_par_size(pin,480)
215 s = "-s "+str(tsize[0])+"x480 "
216 x264streamsize=str(tsize[0])+u"x480"
218 elif size == "FullHD":
219 tsize=get_par_size(pin,1080)
220 s = "-s "+str(tsize[0])+"x1080 "
221 x264streamsize=str(tsize[0])+u"x1080"
224 tsize=get_par_size(pin,480)
225 s = "-s "+str(tsize[0])+"x480 "
226 x264streamsize=str(tsize[0])+u"x480"
228 elif size == "QVGA_BASE":
229 tsize=get_par_size(pin,240)
230 s = "-s "+str(tsize[0])+"x240 "
231 x264streamsize=str(tsize[0])+u"x240"
233 elif size == "WVGA_BASE":
234 tsize=get_par_size(pin,480)
235 s = "-s "+str(tsize[0])+"x480 "
236 x264streamsize=str(tsize[0])+u"x480"
239 tsize=get_par_size(pin,720)
240 s = "-s "+str(tsize[0])+"x720 "
241 x264streamsize=str(tsize[0])+u"x720"
244 x264preset=u"--preset ultrafast"
246 x264preset=u"--preset veryfast"
248 x264preset=u"--preset fast"
250 x264preset=u"--preset medium"
252 x264preset=u"--preset slow"
254 x264preset=u"--preset slower"
255 if size == "WVGA_BASE" or size == "QVGA_BASE":
256 x264profile=" --level 32 --profile baseline "
258 x264profile=" --level 42 --profile high "
259 x264crf=u"--crf "+str(crf)
261 os.environ['LANG']="ja_JP.UTF-8"
262 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - |"
263 exe=exe+u" nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
264 exe = "nice -n 19 " + exe
266 recdblist.printutf8(exe)
268 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
271 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
273 def getMovieBaseSize(pin):
274 ffmpeg=configreader.getpath("ffmpeg")
275 os.environ['LANG']="ja_JP.UTF-8"
276 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
277 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
278 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
281 txtls=txts.split("\n")
287 tX=int(sizetxt.split("x")[0])
288 tY=int(sizetxt.split("x")[1])
289 tEX=int(partxt.split(":")[0])
290 tEY=int(partxt.split(":")[1])
294 return [sizeMaxX,sizeMaxY]
295 def getMoviePAR(pin):
296 ffmpeg=configreader.getpath("ffmpeg")
297 os.environ['LANG']="ja_JP.UTF-8"
298 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
299 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
300 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
301 #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
305 txtls=txts.split("\n")
311 tX=int(sizetxt.split("x")[0])
312 tY=int(sizetxt.split("x")[1])
313 tEX=int(dartxt.split(":")[0])
314 tEY=int(dartxt.split(":")[1])
317 if tX==1920 and tY==1080:
324 def getMovieDAR(pin):
325 ffmpeg=configreader.getpath("ffmpeg")
326 os.environ['LANG']="ja_JP.UTF-8"
327 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
328 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
329 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
333 txtls=txts.split("\n")
339 tX=int(sizetxt.split("x")[0])
340 tY=int(sizetxt.split("x")[1])
341 tEX=int(dartxt.split(":")[0])
342 tEY=int(dartxt.split(":")[1])
345 if tX==1920 and tY==1080:
352 def get_par_size(pin,y):
353 tSize=getMovieBaseSize(pin)
354 tX=tSize[0]*10*y/tSize[1]