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 elif re.search("5",opts):
63 encode_ffmpeg_kai(pin,pout,size,is24fps,quality,crf)
64 elif re.search("Y",opts):
65 encode_sar(pin,pout,size,is24fps,quality,crf)
66 elif re.search("Z",opts):
67 encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf)
68 elif re.search("b",opts):
70 tm2v=pin.replace(".ts",".m2v")
71 encode(tm2v, pout,size,is24fps,quality,crf)
72 except Exception, inst:
73 recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst))
76 encode(pin, pout,size,is24fps,quality,crf)
77 except Exception, inst:
78 recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst))
79 def encode(pin,pout,size,is24fps,quality,crf):
80 mencoder=configreader.getpath("mencoder")
81 x264=configreader.getpath("x264")
84 encvf="-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 xtt=configreader.getenv("x264_thread")
101 x264_addline=configreader.getenv("x264_addline")
103 ofps="-ofps 24000/1001"
104 fps="-fps 30000/1001"
105 x264fps="--fps 24000/1001"
106 x264tune="--tune animation"
107 encvf="-vf pullup,softskip"
108 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
110 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
111 x264streamsize=u"1280x720"
114 encvf = encvf + ",scale=-2:480::0:3,expand=854:480"+harddup
115 x264streamsize=u"854x480"
117 elif size == "FullHD":
118 encvf = encvf + ",scale=-2:1080::0:3,expand=1920:1080"+harddup
119 x264streamsize=u"1920x1080"
122 encvf = encvf + ",scale=720:-2::0:3,expand=720:480"+harddup
123 x264streamsize=u"720x480"
125 elif size == "QVGA_BASE":
126 encvf = encvf + ",scale=320:-2::0:3,expand=320:240"+harddup
127 x264streamsize=u"320x240"
129 elif size == "WVGA_BASE":
130 encvf = encvf + ",scale=-2:480::0:3,expand=854:480"+harddup
131 x264streamsize=u"854x480"
134 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
135 x264streamsize=u"1280x720"
138 x264preset=u"--preset ultrafast"
140 x264preset=u"--preset veryfast"
142 x264preset=u"--preset fast"
144 x264preset=u"--preset medium"
146 x264preset=u"--preset slow"
148 x264preset=u"--preset slower"
149 if size == "WVGA_BASE" or size == "QVGA_BASE":
150 x264profile=" --level 32 --profile baseline "
152 x264profile=" --level 41 --profile high "
153 x264crf=u"--crf "+str(crf)
154 os.environ['LANG']="ja_JP.UTF-8"
157 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 999999)))
158 os.system(u"mkfifo "+streampath)
159 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet -sws 9 "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" &"
160 encexe=encexe+u" nice -n 19 "+x264+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" "+streampath+" "+x264streamsize
161 encexe=u"nice -n 19 " +encexe
162 recdblist.printutf8(encexe)
165 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
168 os.system("rm "+streampath)
169 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
170 def encode_sar(pin,pout,size,is24fps,quality,crf):
171 mencoder=configreader.getpath("mencoder")
172 x264=configreader.getpath("x264")
175 encvf="-vf yadif=0,pp=l5"
176 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
177 ofps="-ofps 30000/1001"
178 fps="-fps 30000/1001"
179 x264fps="--fps 30000/1001"
185 tsar=getMoviePAR(pin)
186 x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
188 xtt=configreader.getenv("x264_thread")
194 x264_addline=configreader.getenv("x264_addline")
196 ofps="-ofps 24000/1001"
197 fps="-fps 30000/1001"
198 x264fps="--fps 24000/1001"
199 x264tune="--tune animation"
200 encvf="-vf pullup,softskip"
201 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
203 tsize=get_par_size(pin,720)
204 encvf = encvf + ",scale=-3:720::0:3"+harddup
205 x264streamsize=str(tsize[0])+u"x720"
208 tsize=get_par_size(pin,480)
209 encvf = encvf + ",scale=-3:480::0:3"+harddup
210 x264streamsize=str(tsize[0])+u"x480"
212 elif size == "FullHD":
213 tsize=get_par_size(pin,1080)
214 encvf = encvf + ",scale=-3:1080::0:3"+harddup
215 x264streamsize=str(tsize[0])+u"x1080"
217 elif size == "QVGA_BASE":
218 tsize=get_par_size(pin,240)
219 encvf = encvf + ",scale=-3:240::0:3"+harddup
220 x264streamsize=str(tsize[0])+u"x240"
222 elif size == "WVGA_BASE":
223 tsize=get_par_size(pin,480)
224 encvf = encvf + ",scale=-3:480::0:3"+harddup
225 x264streamsize=str(tsize[0])+u"x480"
228 tsize=get_par_size(pin,720)
229 encvf = encvf + ",scale=-3:720::0:3"+harddup
230 x264streamsize=str(tsize[0])+u"x720"
233 x264preset=u"--preset ultrafast"
235 x264preset=u"--preset veryfast"
237 x264preset=u"--preset fast"
239 x264preset=u"--preset medium"
241 x264preset=u"--preset slow"
243 x264preset=u"--preset slower"
244 if size == "WVGA_BASE" or size == "QVGA_BASE":
245 x264profile=" --level 32 --profile baseline "
247 x264profile=" --level 41 --profile high "
248 x264crf=u"--crf "+str(crf)
249 os.environ['LANG']="ja_JP.UTF-8"
252 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 999999)))
253 os.system(u"mkfifo "+streampath)
254 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet -sws 9 "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" &"
255 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
256 encexe=u"nice -n 19 " +encexe
257 recdblist.printutf8(encexe)
260 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
263 os.system("rm "+streampath)
264 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
266 def encode_ffmpeg(pin,pout,size,is24fps,quality,crf):
270 ffmpeg=configreader.getpath("ffmpeg")
271 x264=configreader.getpath("x264")
273 x264fps="--fps 30000/1001"
280 xtt=configreader.getenv("x264_thread")
286 x264_addline=configreader.getenv("x264_addline")
289 x264streamsize=u"1280x720"
293 x264streamsize=u"854x480"
295 elif size == "FullHD":
297 x264streamsize=u"1920x1080"
301 x264streamsize=u"720x480"
303 elif size == "QVGA_BASE":
305 x264streamsize=u"320x240"
307 elif size == "WVGA_BASE":
309 x264streamsize=u"854x480"
313 x264streamsize=u"1280x720"
316 x264preset=u"--preset ultrafast"
318 x264preset=u"--preset veryfast"
320 x264preset=u"--preset fast"
322 x264preset=u"--preset medium"
324 x264preset=u"--preset slow"
326 x264preset=u"--preset slower"
327 if size == "WVGA_BASE" or size == "QVGA_BASE":
328 x264profile=" --level 32 --profile baseline "
330 x264profile=" --level 41 --profile high "
331 x264crf=u"--crf "+str(crf)
333 os.environ['LANG']="ja_JP.UTF-8"
334 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - |"
335 exe=exe+u" nice -n 19 "+x264+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
336 exe = "nice -n 19 " + exe
338 recdblist.printutf8(exe)
340 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
343 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
344 def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf):
348 ffmpeg=configreader.getpath("ffmpeg")
349 x264=configreader.getpath("x264")
351 x264fps="--fps 30000/1001"
357 tsar=getMoviePAR(pin)
358 x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
360 xtt=configreader.getenv("x264_thread")
366 x264_addline=configreader.getenv("x264_addline")
368 tsize=get_par_size(pin,720)
369 s = "-s "+str(tsize[0])+"x720 "
370 x264streamsize=str(tsize[0])+u"x720"
373 tsize=get_par_size(pin,480)
374 s = "-s "+str(tsize[0])+"x480 "
375 x264streamsize=str(tsize[0])+u"x480"
377 elif size == "FullHD":
378 tsize=get_par_size(pin,1080)
379 s = "-s "+str(tsize[0])+"x1080 "
380 x264streamsize=str(tsize[0])+u"x1080"
383 tsize=get_par_size(pin,480)
384 s = "-s "+str(tsize[0])+"x480 "
385 x264streamsize=str(tsize[0])+u"x480"
387 elif size == "QVGA_BASE":
388 tsize=get_par_size(pin,240)
389 s = "-s "+str(tsize[0])+"x240 "
390 x264streamsize=str(tsize[0])+u"x240"
392 elif size == "WVGA_BASE":
393 tsize=get_par_size(pin,480)
394 s = "-s "+str(tsize[0])+"x480 "
395 x264streamsize=str(tsize[0])+u"x480"
398 tsize=get_par_size(pin,720)
399 s = "-s "+str(tsize[0])+"x720 "
400 x264streamsize=str(tsize[0])+u"x720"
403 x264preset=u"--preset ultrafast"
405 x264preset=u"--preset veryfast"
407 x264preset=u"--preset fast"
409 x264preset=u"--preset medium"
411 x264preset=u"--preset slow"
413 x264preset=u"--preset slower"
414 if size == "WVGA_BASE" or size == "QVGA_BASE":
415 x264profile=" --level 32 --profile baseline "
417 x264profile=" --level 41 --profile high "
418 x264crf=u"--crf "+str(crf)
420 os.environ['LANG']="ja_JP.UTF-8"
421 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - |"
422 exe=exe+u" nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
423 exe = "nice -n 19 " + exe
425 recdblist.printutf8(exe)
427 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
430 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
431 def encode_ffmpeg_kai(pin,pout,size,is24fps,quality,crf):
435 ffmpeg=configreader.getpath("ffmpeg")
436 x264=configreader.getpath("x264")
438 x264fps="--fps 30000/1001"
445 xtt=configreader.getenv("x264_thread")
451 x264_addline=configreader.getenv("x264_addline")
453 padx=get16_9ffmpeg_s(pin,1280,720)
454 s = "-s "+str(1280-2*padx)+"x720 "
456 s=s+"-padleft "+str(padx)+" -padright "+str(padx)+" "
457 x264streamsize=u"1280x720"
460 padx=get16_9ffmpeg_s(pin,854,480)
461 s = "-s "+str(854-2*padx)+"x480 "
463 s=s+"-padleft "+str(padx)+" -padright "+str(padx)+" "
464 x264streamsize=u"854x480"
466 elif size == "FullHD":
467 padx=get16_9ffmpeg_s(pin,1920,1080)
468 s = "-s "+str(1920-2*padx)+"x1080 "
470 s=s+"-padleft "+str(padx)+" -padright "+str(padx)+" "
471 x264streamsize=u"1920x1080"
475 x264streamsize=u"720x480"
477 elif size == "QVGA_BASE":
479 x264streamsize=u"320x240"
481 elif size == "WVGA_BASE":
483 x264streamsize=u"854x480"
486 padx=get16_9ffmpeg_s(pin,1280,720)
487 s = "-s "+str(1280-2*padx)+"x720 "
489 s=s+"-padleft "+str(padx)+" -padright "+str(padx)+" "
490 x264streamsize=u"1280x720"
493 x264preset=u"--preset ultrafast"
495 x264preset=u"--preset veryfast"
497 x264preset=u"--preset fast"
499 x264preset=u"--preset medium"
501 x264preset=u"--preset slow"
503 x264preset=u"--preset slower"
504 if size == "WVGA_BASE" or size == "QVGA_BASE":
505 x264profile=" --level 32 --profile baseline "
507 x264profile=" --level 41 --profile high "
508 x264crf=u"--crf "+str(crf)
510 os.environ['LANG']="ja_JP.UTF-8"
511 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - |"
512 exe=exe+u" nice -n 19 "+x264+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
513 exe = "nice -n 19 " + exe
515 recdblist.printutf8(exe)
517 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
520 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
521 def getMovieSize(pin):
522 ffmpeg=configreader.getpath("ffmpeg")
523 os.environ['LANG']="ja_JP.UTF-8"
524 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
525 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
526 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
529 txtls=txts.split("\n")
535 tX=int(sizetxt.split("x")[0])
536 tY=int(sizetxt.split("x")[1])
537 tEX=int(partxt.split(":")[0])
538 tEY=int(partxt.split(":")[1])
539 if tX==1920 and tY==1080:
543 if tX2>10*int(tX2/10):
550 return [sizeMaxX,sizeMaxY]
551 def getMovieBaseSize(pin):
552 ffmpeg=configreader.getpath("ffmpeg")
553 os.environ['LANG']="ja_JP.UTF-8"
554 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
555 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
556 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
559 txtls=txts.split("\n")
565 tX=int(sizetxt.split("x")[0])
566 tY=int(sizetxt.split("x")[1])
567 tEX=int(partxt.split(":")[0])
568 tEY=int(partxt.split(":")[1])
572 return [sizeMaxX,sizeMaxY]
573 def getMoviePAR(pin):
574 ffmpeg=configreader.getpath("ffmpeg")
575 os.environ['LANG']="ja_JP.UTF-8"
576 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
577 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
578 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
579 #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
583 txtls=txts.split("\n")
589 tX=int(sizetxt.split("x")[0])
590 tY=int(sizetxt.split("x")[1])
591 tEX=int(dartxt.split(":")[0])
592 tEY=int(dartxt.split(":")[1])
595 if tX==1920 and tY==1080:
602 def getMovieDAR(pin):
603 ffmpeg=configreader.getpath("ffmpeg")
604 os.environ['LANG']="ja_JP.UTF-8"
605 exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
606 txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
607 rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
611 txtls=txts.split("\n")
617 tX=int(sizetxt.split("x")[0])
618 tY=int(sizetxt.split("x")[1])
619 tEX=int(dartxt.split(":")[0])
620 tEY=int(dartxt.split(":")[1])
623 if tX==1920 and tY==1080:
630 def get16_9ffmpeg_s(pin,x,y):
632 tDAR=getMovieDAR(pin)
633 if (tDAR[0]==16 and tDAR[1]==9):
638 tSize=getMovieSize(pin)
639 tn=x-tSize[1]*x/tSize[0]
645 def get_par_size(pin,y):
646 tSize=getMovieBaseSize(pin)
647 tX=tSize[0]*10*y/tSize[1]