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"))
24 if re.search("H", opts):
26 if re.search("S", opts):
28 if re.search("F", opts):
30 if re.search("W",opts):
32 if re.search("MW8", opts):
35 if re.search("MW9", opts):
38 if re.search("v", opts):
40 crf=int(configreader.getenv("animation_crf"))
41 if re.search("a", opts):
43 crf=int(configreader.getenv("animation_crf"))
44 if re.search("q",opts):
46 if re.search("w",opts):
48 if re.search("e",opts):
50 if re.search("r",opts):
52 if re.search("u",opts):
54 if re.search("i",opts):
56 if re.search("o",opts):
58 if re.search("p",opts):
60 if re.search("d",opts):
61 #encode_ffmpeg_kai(pin,pout,size,is24fps,quality,crf)
62 encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf)
63 elif re.search("5",opts):
64 #encode_ffmpeg_kai(pin,pout,size,is24fps,quality,crf)
65 encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf)
66 elif re.search("Y",opts):
67 encode_sar(pin,pout,size,is24fps,quality,crf)
68 elif re.search("Z",opts):
69 encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf)
70 elif re.search("b",opts):
72 tm2v=pin.replace(".ts",".m2v")
73 #encode(tm2v, pout,size,is24fps,quality,crf)
74 encode_sar(tm2v, pout,size,is24fps,quality,crf)
75 except Exception, inst:
76 recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst))
79 #encode(pin, pout,size,is24fps,quality,crf)
80 encode_sar(pin, pout,size,is24fps,quality,crf)
81 except Exception, inst:
82 recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst))
83 def encode(pin,pout,size,is24fps,quality,crf):
84 mencoder=configreader.getpath("mencoder")
85 x264=configreader.getpath("x264")
88 encvf="-vf yadif=0,pp=l5"
89 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
90 ofps="-ofps 30000/1001"
92 x264fps="--fps 30000/1001"
99 xtt=configreader.getenv("x264_thread")
105 x264_addline=configreader.getenv("x264_addline")
107 ofps="-ofps 24000/1001"
108 fps="-fps 30000/1001"
109 x264fps="--fps 24000/1001"
110 x264tune="--tune animation"
111 encvf="-vf pullup,softskip"
112 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
114 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
115 x264streamsize=u"1280x720"
118 encvf = encvf + ",scale=-2:480::0:3,expand=854:480"+harddup
119 x264streamsize=u"854x480"
121 elif size == "FullHD":
122 encvf = encvf + ",scale=-2:1080::0:3,expand=1920:1080"+harddup
123 x264streamsize=u"1920x1080"
126 encvf = encvf + ",scale=720:-2::0:3,expand=720:480"+harddup
127 x264streamsize=u"720x480"
129 elif size == "QVGA_BASE":
130 encvf = encvf + ",scale=320:-2::0:3,expand=320:240"+harddup
131 x264streamsize=u"320x240"
133 elif size == "WVGA_BASE":
134 encvf = encvf + ",scale=-2:480::0:3,expand=854:480"+harddup
135 x264streamsize=u"854x480"
138 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
139 x264streamsize=u"1280x720"
142 x264preset=u"--preset ultrafast"
144 x264preset=u"--preset veryfast"
146 x264preset=u"--preset fast"
148 x264preset=u"--preset medium"
150 x264preset=u"--preset slow"
152 x264preset=u"--preset slower"
153 if size == "WVGA_BASE" or size == "QVGA_BASE":
154 x264profile=" --level 32 --profile baseline "
156 x264profile=" --level 41 --profile high "
157 x264crf=u"--crf "+str(crf)
158 os.environ['LANG']="ja_JP.UTF-8"
161 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 999999)))
162 os.system(u"mkfifo "+streampath)
163 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet -sws 9 "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" &"
164 encexe=encexe+u" nice -n 19 "+x264+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" "+streampath+" "+x264streamsize
165 encexe=u"nice -n 19 " +encexe
166 recdblist.printutf8(encexe)
169 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
172 os.system("rm "+streampath)
173 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
174 def encode_sar(pin,pout,size,is24fps,quality,crf):
175 mencoder=configreader.getpath("mencoder")
176 x264=configreader.getpath("x264")
179 encvf="-vf yadif=0,pp=l5"
180 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
181 ofps="-ofps 30000/1001"
182 fps="-fps 30000/1001"
183 x264fps="--fps 30000/1001"
189 tsar=getMoviePAR(pin)
190 x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
192 xtt=configreader.getenv("x264_thread")
198 x264_addline=configreader.getenv("x264_addline")
200 ofps="-ofps 24000/1001"
201 fps="-fps 30000/1001"
202 x264fps="--fps 24000/1001"
203 x264tune="--tune animation"
204 encvf="-vf pullup,softskip"
205 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
207 tsize=get_par_size(pin,720)
208 encvf = encvf + ",scale=-3:720::0:3"+harddup
209 x264streamsize=str(tsize[0])+u"x720"
212 tsize=get_par_size(pin,480)
213 encvf = encvf + ",scale=-3:480::0:3"+harddup
214 x264streamsize=str(tsize[0])+u"x480"
216 elif size == "FullHD":
217 tsize=get_par_size(pin,1080)
218 encvf = encvf + ",scale=-3:1080::0:3"+harddup
219 x264streamsize=str(tsize[0])+u"x1080"
221 elif size == "QVGA_BASE":
222 tsize=get_par_size(pin,240)
223 encvf = encvf + ",scale=-3:240::0:3"+harddup
224 x264streamsize=str(tsize[0])+u"x240"
226 elif size == "WVGA_BASE":
227 tsize=get_par_size(pin,480)
228 encvf = encvf + ",scale=-3:480::0:3"+harddup
229 x264streamsize=str(tsize[0])+u"x480"
232 tsize=get_par_size(pin,720)
233 encvf = encvf + ",scale=-3:720::0:3"+harddup
234 x264streamsize=str(tsize[0])+u"x720"
237 x264preset=u"--preset ultrafast"
239 x264preset=u"--preset veryfast"
241 x264preset=u"--preset fast"
243 x264preset=u"--preset medium"
245 x264preset=u"--preset slow"
247 x264preset=u"--preset slower"
248 if size == "WVGA_BASE" or size == "QVGA_BASE":
249 x264profile=" --level 32 --profile baseline "
251 x264profile=" --level 41 --profile high "
252 x264crf=u"--crf "+str(crf)
253 os.environ['LANG']="ja_JP.UTF-8"
256 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 999999)))
257 os.system(u"mkfifo "+streampath)
258 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet -sws 9 "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" &"
259 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
260 encexe=u"nice -n 19 " +encexe
261 recdblist.printutf8(encexe)
264 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
267 os.system("rm "+streampath)
268 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
270 def encode_ffmpeg(pin,pout,size,is24fps,quality,crf):
274 ffmpeg=configreader.getpath("ffmpeg")
275 x264=configreader.getpath("x264")
277 x264fps="--fps 30000/1001"
284 xtt=configreader.getenv("x264_thread")
290 x264_addline=configreader.getenv("x264_addline")
293 x264streamsize=u"1280x720"
297 x264streamsize=u"854x480"
299 elif size == "FullHD":
301 x264streamsize=u"1920x1080"
305 x264streamsize=u"720x480"
307 elif size == "QVGA_BASE":
309 x264streamsize=u"320x240"
311 elif size == "WVGA_BASE":
313 x264streamsize=u"854x480"
317 x264streamsize=u"1280x720"
320 x264preset=u"--preset ultrafast"
322 x264preset=u"--preset veryfast"
324 x264preset=u"--preset fast"
326 x264preset=u"--preset medium"
328 x264preset=u"--preset slow"
330 x264preset=u"--preset slower"
331 if size == "WVGA_BASE" or size == "QVGA_BASE":
332 x264profile=" --level 32 --profile baseline "
334 x264profile=" --level 41 --profile high "
335 x264crf=u"--crf "+str(crf)
337 os.environ['LANG']="ja_JP.UTF-8"
338 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - |"
339 exe=exe+u" nice -n 19 "+x264+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
340 exe = "nice -n 19 " + exe
342 recdblist.printutf8(exe)
344 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
347 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
348 def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf):
352 ffmpeg=configreader.getpath("ffmpeg")
353 x264=configreader.getpath("x264")
355 x264fps="--fps 30000/1001"
361 tsar=getMoviePAR(pin)
362 x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
364 xtt=configreader.getenv("x264_thread")
370 x264_addline=configreader.getenv("x264_addline")
372 tsize=get_par_size(pin,720)
373 s = "-s "+str(tsize[0])+"x720 "
374 x264streamsize=str(tsize[0])+u"x720"
377 tsize=get_par_size(pin,480)
378 s = "-s "+str(tsize[0])+"x480 "
379 x264streamsize=str(tsize[0])+u"x480"
381 elif size == "FullHD":
382 tsize=get_par_size(pin,1080)
383 s = "-s "+str(tsize[0])+"x1080 "
384 x264streamsize=str(tsize[0])+u"x1080"
387 tsize=get_par_size(pin,480)
388 s = "-s "+str(tsize[0])+"x480 "
389 x264streamsize=str(tsize[0])+u"x480"
391 elif size == "QVGA_BASE":
392 tsize=get_par_size(pin,240)
393 s = "-s "+str(tsize[0])+"x240 "
394 x264streamsize=str(tsize[0])+u"x240"
396 elif size == "WVGA_BASE":
397 tsize=get_par_size(pin,480)
398 s = "-s "+str(tsize[0])+"x480 "
399 x264streamsize=str(tsize[0])+u"x480"
402 tsize=get_par_size(pin,720)
403 s = "-s "+str(tsize[0])+"x720 "
404 x264streamsize=str(tsize[0])+u"x720"
407 x264preset=u"--preset ultrafast"
409 x264preset=u"--preset veryfast"
411 x264preset=u"--preset fast"
413 x264preset=u"--preset medium"
415 x264preset=u"--preset slow"
417 x264preset=u"--preset slower"
418 if size == "WVGA_BASE" or size == "QVGA_BASE":
419 x264profile=" --level 32 --profile baseline "
421 x264profile=" --level 41 --profile high "
422 x264crf=u"--crf "+str(crf)
424 os.environ['LANG']="ja_JP.UTF-8"
425 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - |"
426 exe=exe+u" nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
427 exe = "nice -n 19 " + exe
429 recdblist.printutf8(exe)
431 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
434 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
435 def encode_ffmpeg_kai(pin,pout,size,is24fps,quality,crf):
439 ffmpeg=configreader.getpath("ffmpeg")
440 x264=configreader.getpath("x264")
442 x264fps="--fps 30000/1001"
449 xtt=configreader.getenv("x264_thread")
455 x264_addline=configreader.getenv("x264_addline")
457 padx=get16_9ffmpeg_s(pin,1280,720)
458 s = "-s "+str(1280-2*padx)+"x720 "
460 s=s+"-padleft "+str(padx)+" -padright "+str(padx)+" "
461 x264streamsize=u"1280x720"
464 padx=get16_9ffmpeg_s(pin,854,480)
465 s = "-s "+str(854-2*padx)+"x480 "
467 s=s+"-padleft "+str(padx)+" -padright "+str(padx)+" "
468 x264streamsize=u"854x480"
470 elif size == "FullHD":
471 padx=get16_9ffmpeg_s(pin,1920,1080)
472 s = "-s "+str(1920-2*padx)+"x1080 "
474 s=s+"-padleft "+str(padx)+" -padright "+str(padx)+" "
475 x264streamsize=u"1920x1080"
479 x264streamsize=u"720x480"
481 elif size == "QVGA_BASE":
483 x264streamsize=u"320x240"
485 elif size == "WVGA_BASE":
487 x264streamsize=u"854x480"
490 padx=get16_9ffmpeg_s(pin,1280,720)
491 s = "-s "+str(1280-2*padx)+"x720 "
493 s=s+"-padleft "+str(padx)+" -padright "+str(padx)+" "
494 x264streamsize=u"1280x720"
497 x264preset=u"--preset ultrafast"
499 x264preset=u"--preset veryfast"
501 x264preset=u"--preset fast"
503 x264preset=u"--preset medium"
505 x264preset=u"--preset slow"
507 x264preset=u"--preset slower"
508 if size == "WVGA_BASE" or size == "QVGA_BASE":
509 x264profile=" --level 32 --profile baseline "
511 x264profile=" --level 41 --profile high "
512 x264crf=u"--crf "+str(crf)
514 os.environ['LANG']="ja_JP.UTF-8"
515 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - |"
516 exe=exe+u" nice -n 19 "+x264+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
517 exe = "nice -n 19 " + exe
519 recdblist.printutf8(exe)
521 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
524 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
525 def getMovieSize(pin):
526 ffmpeg=configreader.getpath("ffmpeg")
527 os.environ['LANG']="ja_JP.UTF-8"
528 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
529 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
530 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
533 txtls=txts.split("\n")
539 tX=int(sizetxt.split("x")[0])
540 tY=int(sizetxt.split("x")[1])
541 tEX=int(partxt.split(":")[0])
542 tEY=int(partxt.split(":")[1])
543 if tX==1920 and tY==1080:
547 if tX2>10*int(tX2/10):
554 return [sizeMaxX,sizeMaxY]
555 def getMovieBaseSize(pin):
556 ffmpeg=configreader.getpath("ffmpeg")
557 os.environ['LANG']="ja_JP.UTF-8"
558 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
559 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
560 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
563 txtls=txts.split("\n")
569 tX=int(sizetxt.split("x")[0])
570 tY=int(sizetxt.split("x")[1])
571 tEX=int(partxt.split(":")[0])
572 tEY=int(partxt.split(":")[1])
576 return [sizeMaxX,sizeMaxY]
577 def getMoviePAR(pin):
578 ffmpeg=configreader.getpath("ffmpeg")
579 os.environ['LANG']="ja_JP.UTF-8"
580 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
581 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
582 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
583 #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
587 txtls=txts.split("\n")
593 tX=int(sizetxt.split("x")[0])
594 tY=int(sizetxt.split("x")[1])
595 tEX=int(dartxt.split(":")[0])
596 tEY=int(dartxt.split(":")[1])
599 if tX==1920 and tY==1080:
606 def getMovieDAR(pin):
607 ffmpeg=configreader.getpath("ffmpeg")
608 os.environ['LANG']="ja_JP.UTF-8"
609 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
610 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
611 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
615 txtls=txts.split("\n")
621 tX=int(sizetxt.split("x")[0])
622 tY=int(sizetxt.split("x")[1])
623 tEX=int(dartxt.split(":")[0])
624 tEY=int(dartxt.split(":")[1])
627 if tX==1920 and tY==1080:
634 def get16_9ffmpeg_s(pin,x,y):
636 tDAR=getMovieDAR(pin)
637 if (tDAR[0]==16 and tDAR[1]==9):
642 tSize=getMovieSize(pin)
643 tn=x-tSize[1]*x/tSize[0]
649 def get_par_size(pin,y):
650 tSize=getMovieBaseSize(pin)
651 tX=tSize[0]*10*y/tSize[1]