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")
81 x264=configreader.getpath("x264")
84 encvf="-sws 9 -vf yadif=0,pp=l5"
85 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
86 ofps="-ofps 30000/1001"
88 x264fps="--fps 30000/1001"
95 x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
97 xtt=configreader.getenv("x264_thread")
103 x264_addline=configreader.getenv("x264_addline")
105 ofps="-ofps 24000/1001"
106 fps="-fps 30000/1001"
107 x264fps="--fps 24000/1001"
108 x264tune="--tune animation"
109 encvf="-sws 9 -vf pullup,softskip"
110 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
112 tsize=get_par_size(pin,720)
113 encvf = encvf + ",scale=-3:720::0:3"+harddup
114 x264streamsize=str(tsize[0])+u"x720"
117 tsize=get_par_size(pin,480)
118 encvf = encvf + ",scale=-3:480::0:3"+harddup
119 x264streamsize=str(tsize[0])+u"x480"
121 elif size == "FullHD":
122 tsize=get_par_size(pin,1080)
123 encvf = encvf + ",scale=-3:1080::0:3"+harddup
124 x264streamsize=str(tsize[0])+u"x1080"
126 elif size == "QVGA_BASE":
127 tsize=get_par_size(pin,240)
128 encvf = encvf + ",scale=-3:240::0:3"+harddup
129 x264streamsize=str(tsize[0])+u"x240"
131 elif size == "WVGA_BASE":
132 tsize=get_par_size(pin,480)
133 encvf = encvf + ",scale=-3:480::0:3"+harddup
134 x264streamsize=str(tsize[0])+u"x480"
137 tsize=get_par_size(pin,720)
138 encvf = encvf + ",scale=-3:720::0:3"+harddup
139 x264streamsize=str(tsize[0])+u"x720"
142 tsize=getMovieBaseSize(pin)
143 ofps="-ofps 30000/1001"
144 fps="-fps 30000/1001"
145 x264fps="--fps 30000/1001"
146 x264tune=x264tune+" --tff --nal-hrd vbr"
147 encvf="-vf hqdn3d=2:1:2"
150 x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
152 x264preset=u"--preset ultrafast"
154 x264preset=u"--preset veryfast"
156 x264preset=u"--preset fast"
158 x264preset=u"--preset medium"
160 x264preset=u"--preset slow"
162 x264preset=u"--preset slower"
163 if size == "WVGA_BASE" or size == "QVGA_BASE":
164 x264profile=" --level 32 --profile baseline "
166 x264profile=" --level 42 --profile high "
167 x264crf=u"--crf "+str(crf)
168 os.environ['LANG']="ja_JP.UTF-8"
171 temptime=int(time.time())
172 temptime=temptime % 9697
173 random.jumpahead(temptime)
174 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))
175 os.system(u"mkfifo "+streampath)
176 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" &"
177 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
178 encexe=u"nice -n 19 " +encexe
179 recdblist.printutf8(encexe)
182 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
185 os.system("rm "+streampath)
186 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
189 def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
193 ffmpeg=configreader.getpath("ffmpeg")
194 x264=configreader.getpath("x264")
196 x264fps="--fps 30000/1001"
202 tsar=getMoviePAR(pin)
203 x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
205 xtt=configreader.getenv("x264_thread")
211 x264_addline=configreader.getenv("x264_addline")
213 tsize=get_par_size(pin,720)
214 s = "-s "+str(tsize[0])+"x720 "
215 x264streamsize=str(tsize[0])+u"x720"
218 tsize=get_par_size(pin,480)
219 s = "-s "+str(tsize[0])+"x480 "
220 x264streamsize=str(tsize[0])+u"x480"
222 elif size == "FullHD":
223 tsize=get_par_size(pin,1080)
224 s = "-s "+str(tsize[0])+"x1080 "
225 x264streamsize=str(tsize[0])+u"x1080"
228 tsize=get_par_size(pin,480)
229 s = "-s "+str(tsize[0])+"x480 "
230 x264streamsize=str(tsize[0])+u"x480"
232 elif size == "QVGA_BASE":
233 tsize=get_par_size(pin,240)
234 s = "-s "+str(tsize[0])+"x240 "
235 x264streamsize=str(tsize[0])+u"x240"
237 elif size == "WVGA_BASE":
238 tsize=get_par_size(pin,480)
239 s = "-s "+str(tsize[0])+"x480 "
240 x264streamsize=str(tsize[0])+u"x480"
243 tsize=get_par_size(pin,720)
244 s = "-s "+str(tsize[0])+"x720 "
245 x264streamsize=str(tsize[0])+u"x720"
248 x264preset=u"--preset ultrafast"
250 x264preset=u"--preset veryfast"
252 x264preset=u"--preset fast"
254 x264preset=u"--preset medium"
256 x264preset=u"--preset slow"
258 x264preset=u"--preset slower"
259 if size == "WVGA_BASE" or size == "QVGA_BASE":
260 x264profile=" --level 32 --profile baseline "
262 x264profile=" --level 42 --profile high "
263 x264crf=u"--crf "+str(crf)
265 os.environ['LANG']="ja_JP.UTF-8"
266 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - |"
267 exe=exe+u" nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
268 exe = "nice -n 19 " + exe
270 recdblist.printutf8(exe)
272 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
275 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
277 def getMovieBaseSize(pin):
278 ffmpeg=configreader.getpath("ffmpeg")
279 os.environ['LANG']="ja_JP.UTF-8"
280 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
281 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
282 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
285 txtls=txts.split("\n")
291 tX=int(sizetxt.split("x")[0])
292 tY=int(sizetxt.split("x")[1])
293 tEX=int(partxt.split(":")[0])
294 tEY=int(partxt.split(":")[1])
298 return [sizeMaxX,sizeMaxY]
299 def getMoviePAR(pin):
300 ffmpeg=configreader.getpath("ffmpeg")
301 os.environ['LANG']="ja_JP.UTF-8"
302 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
303 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
304 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
305 #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
309 txtls=txts.split("\n")
315 tX=int(sizetxt.split("x")[0])
316 tY=int(sizetxt.split("x")[1])
317 tEX=int(dartxt.split(":")[0])
318 tEY=int(dartxt.split(":")[1])
321 if tX==1920 and tY==1080:
328 def getMovieDAR(pin):
329 ffmpeg=configreader.getpath("ffmpeg")
330 os.environ['LANG']="ja_JP.UTF-8"
331 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
332 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
333 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
337 txtls=txts.split("\n")
343 tX=int(sizetxt.split("x")[0])
344 tY=int(sizetxt.split("x")[1])
345 tEX=int(dartxt.split(":")[0])
346 tEY=int(dartxt.split(":")[1])
349 if tX==1920 and tY==1080:
356 def get_par_size(pin,y):
357 tSize=getMovieBaseSize(pin)
358 tX=tSize[0]*10*y/tSize[1]