#!/usr/bin/python
# coding: UTF-8
# Rec10 TS Recording Tools
-# Copyright (C) 2009 Yukikaze
+# Copyright (C) 2009-2010 Yukikaze
import commands
+import configreader
import os
+import os.path
import re
+import random
import time
-import tv2avi
+import recdblist
def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
- pout = pout.encode('utf-8')
- pin = pin.encode('utf-8')
"""
pinで指定されたファイルをpoutにx264でエンコードして書き出す
"""
- isAnime = 0
- size = "SD"
- txt=""
- if re.search("a", opts):
- isAnime = 1
- #print "isAnime"
dualpass = 0
- if re.search("2", opts):
- dualpass = 1
- if re.search("Q", opts):
- size = "WQVGA"
- if re.search("F", opts):
- size = "FHD"
+ is24fps=0
+ size="HD"
+ crf=18
+ quality=4
+ quality=int(configreader.getenv("x264_preset"))
+ crf=int(configreader.getenv("crf"))
+ deinterlace=1
if re.search("H", opts):
size = "HD"
if re.search("S", opts):
- size = "SD"
- if isAnime == 1:
- encvf = "-vf pullup,softskip,pp=l5,"
+ size = "WVGA"
+ if re.search("F", opts):
+ size = "FullHD"
+ if re.search("W",opts):
+ size = "WVGA"
+ if re.search("MW8", opts):
+ size = "QVGA_BASE"
+ crf=crf+4
+ if re.search("MW9", opts):
+ size = "WVGA_BASE"
+ crf=crf+2
+ if re.search("v", opts):
+ is24fps=1
+ crf=int(configreader.getenv("animation_crf"))
+ if re.search("a", opts):
+ is24fps=1
+ crf=int(configreader.getenv("animation_crf"))
+ if re.search("I", opts):
+ deinterlace=0
+ if re.search("q",opts):
+ quality=quality-2
+ if re.search("w",opts):
+ quality=quality-1
+ if re.search("e",opts):
+ quality=quality+1
+ if re.search("r",opts):
+ quality=quality+2
+ if re.search("u",opts):
+ crf=crf+2
+ if re.search("i",opts):
+ crf=crf+1
+ if re.search("o",opts):
+ crf=crf-1
+ if re.search("p",opts):
+ crf=crf-2
+ if re.search("d",opts):
+ encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace)
+ elif re.search("5",opts):
+ encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace)
+ elif re.search("b",opts):
+ try:
+ tm2v=pin.replace(".ts",".m2v")
+ encode_sar(tm2v, pout,size,is24fps,quality,crf,deinterlace)
+ except Exception, inst:
+ recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst),log_level=200)
+ else:
+ try:
+ encode_sar(pin, pout,size,is24fps,quality,crf,deinterlace)
+ except Exception, inst:
+ recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst),log_level=200)
+def encode_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
+ mencoder=configreader.getpath("mencoder")
+ encvf=""
+ txt=""
+ encvf="-sws 9 -vf yadif=0,pp=l5"
+ harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
+ ofps="-ofps 30000/1001"
+ fps="-fps 30000/1001"
+ x264fps="30000/1001"
+ x264streamsize=""
+ x264preset=""
+ x264tune=""
+ x264_bitrate="5000"
+ tsar=getMoviePAR(pin)
+ x264sar=str(tsar[0])+":"+str(tsar[1])
+ if is24fps==1:
+ ofps="-ofps 24000/1001"
+ fps="-fps 30000/1001"
+ x264fps="24000/1001"
+ x264tune="--tune animation"
+ encvf="-sws 9 -vf pullup,softskip"
+ harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
+ if size == "HD":
+ tsize=get_par_size(pin,720)
+ encvf = encvf + ",scale=-3:720::0:3"+harddup
+ x264streamsize=str(tsize[0])+u"x720"
+ x264_bitrate="2500"
+ elif size == "WVGA":
+ tsize=get_par_size(pin,480)
+ encvf = encvf + ",scale=-3:480::0:3"+harddup
+ x264streamsize=str(tsize[0])+u"x480"
+ x264_bitrate="1500"
+ elif size == "FullHD":
+ tsize=get_par_size(pin,1080)
+ encvf = encvf + ",scale=-3:1080::0:3"+harddup
+ x264streamsize=str(tsize[0])+u"x1080"
+ x264_bitrate="5000"
+ elif size == "QVGA_BASE":
+ tsize=get_par_size(pin,240)
+ encvf = encvf + ",scale=-3:240::0:3"+harddup
+ x264streamsize=str(tsize[0])+u"x240"
+ x264_bitrate="300"
+ elif size == "WVGA_BASE":
+ tsize=get_par_size(pin,480)
+ encvf = encvf + ",scale=-3:480::0:3"+harddup
+ x264streamsize=str(tsize[0])+u"x480"
+ x264_bitrate="1500"
+ else:
+ tsize=get_par_size(pin,720)
+ encvf = encvf + ",scale=-3:720::0:3"+harddup
+ x264streamsize=str(tsize[0])+u"x720"
+ x264_bitrate="2500"
+ if deinterlace==0:
+ tsize=getMovieBaseSize(pin)
+ ofps="-ofps 30000/1001"
+ #fps="-fps 30000/1001"
+ fps=""
+ x264fps="30000/1001"
+ x264tune=x264tune+" --tff --nal-hrd vbr"
+ encvf="-vf hqdn3d=2:1:2"
+ harddup=",harddup"
+ encvf=encvf+harddup
+ x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
+ if tsize[0] <= 0 or tsize[1] <= 0:
+ encvf="-sws 9 -vf yadif=0,pp=l5"
+ harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
+ ofps="-ofps 30000/1001"
+ fps="-fps 30000/1001"
+ x264fps="30000/1001"
+ tsize=[1280,720]
+ encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
+ x264streamsize=u"1280x720"
+ x264_bitrate="2500"
+ if quality==1:
+ x264preset=u"ultrafast"
+ elif quality==2:
+ x264preset=u"veryfast"
+ elif quality==3:
+ x264preset=u"fast"
+ elif quality==4:
+ x264preset=u"medium"
+ elif quality==5:
+ x264preset=u"slow"
+ elif quality==6:
+ x264preset=u"slower"
+ if size == "WVGA_BASE" or size == "QVGA_BASE":
+ x264profile=" --level 32 --profile baseline "
+ else:
+ x264profile=" --level 42 --profile high "
+ x264crf=str(crf)
+ os.environ['LANG']="ja_JP.UTF-8"
+ random.seed(pin)
+ random.jumpahead(10)
+ temptime=int(time.time())
+ temptime=temptime % 9697
+ random.jumpahead(temptime)
+ streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))
+ os.system(u"mkfifo "+streampath)
+ encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" & "
+ encexe=encexe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf)
+ #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
+ encexe=u"nice -n 19 " +encexe
+ recdblist.printutf8(encexe)
+ txt=""
+ try:
+ txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
+ except:
+ ""
+ os.system("rm "+streampath)
+ recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
+
+
+def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf,deinterlace=1):
+ """
+
+ """
+ ffmpeg=configreader.getpath("ffmpeg")
+ fps=u"-r 29.970030 "
+ x264fps="30000/1001"
+ x264streamsize=""
+ x264preset=""
+ x264tune=""
+ x264_bitrate="2500"
+ x264_thread="auto"
+ tsar=getMoviePAR(pin)
+ filter="-deinterlace"
+ x264sar=str(tsar[0])+":"+str(tsar[1])
+ if size == "HD":
+ tsize=get_par_size(pin,720)
+ s = "-s "+str(tsize[0])+"x720 "
+ x264streamsize=str(tsize[0])+u"x720"
+ x264_bitrate="2500"
+ elif size == "WVGA":
+ tsize=get_par_size(pin,480)
+ s = "-s "+str(tsize[0])+"x480 "
+ x264streamsize=str(tsize[0])+u"x480"
+ x264_bitrate="1500"
+ elif size == "FullHD":
+ tsize=get_par_size(pin,1080)
+ s = "-s "+str(tsize[0])+"x1080 "
+ x264streamsize=str(tsize[0])+u"x1080"
+ x264_bitrate="5000"
+ elif size == "SD":
+ tsize=get_par_size(pin,480)
+ s = "-s "+str(tsize[0])+"x480 "
+ x264streamsize=str(tsize[0])+u"x480"
+ x264_bitrate="1250"
+ elif size == "QVGA_BASE":
+ tsize=get_par_size(pin,240)
+ s = "-s "+str(tsize[0])+"x240 "
+ x264streamsize=str(tsize[0])+u"x240"
+ x264_bitrate="300"
+ elif size == "WVGA_BASE":
+ tsize=get_par_size(pin,480)
+ s = "-s "+str(tsize[0])+"x480 "
+ x264streamsize=str(tsize[0])+u"x480"
+ x264_bitrate="1500"
else:
- encvf = "-vf pp=l5,"
- if size == "SD":
- encvf = encvf + "scale=720:480,harddup"
- bitrate = "bitrate=" + tv2avi.Bitrate_SD
- elif size == "HD":
- encvf = encvf + "scale=1280:720,harddup"
- bitrate = "bitrate=" + tv2avi.Bitrate_HD
- elif size == "FHD":
- encvf = encvf + "scale=1920:1080,harddup"
- bitrate = "bitrate=" + tv2avi.Bitrate_FHD
- elif size == "WQVGA":
- encvf = encvf + "scale=400:240,harddup"
- bitrate = "bitrate=" + tv2avi.Bitrate_WQVGA
+ tsize=get_par_size(pin,720)
+ s = "-s "+str(tsize[0])+"x720 "
+ x264streamsize=str(tsize[0])+u"x720"
+ x264_bitrate="2500"
+ if deinterlace==0:
+ tsize=getMovieBaseSize(pin)
+ fps=""
+ s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "
+ x264fps="30000/1001"
+ x264tune=x264tune+" --tff --nal-hrd vbr"
+ filter=""
+ x264streamsize=str(tsize[0])+u"x"+str(tsize[1])
+ if quality==1:
+ x264preset=u"ultrafast"
+ elif quality==2:
+ x264preset=u"veryfast"
+ elif quality==3:
+ x264preset=u"fast"
+ elif quality==4:
+ x264preset=u"medium"
+ elif quality==5:
+ x264preset=u"slow"
+ elif quality==6:
+ x264preset=u"slower"
+ if size == "WVGA_BASE" or size == "QVGA_BASE":
+ x264profile=" --level 32 --profile baseline "
else:
- encvf = encvf + "scale=1280:720,harddup"
- bitrate = "bitrate=" + tv2avi.Bitrate_HD
- if dualpass == 1:
- pas1exe = "mencoder \'" + pin + "\' -quiet -ovc x264 " + encvf + " -x264encopts " + bitrate + ":threads=auto:pass=1:turbo=2 -passlogfile \'" + pin + ".log\' " + "-oac mp3lame -lameopts cbr:br=128 -o /dev/null"
- pas2exe = "mencoder \'" + pin + "\' -quiet -ovc x264 " + encvf + " -x264encopts " + bitrate + ":threads=auto:pass=2 -passlogfile \'" + pin + ".log\' " + "-oac mp3lame -lameopts cbr:br=128 -o \'" + pout + "\'"
- pas1exe = "nice -n 19 " + pas1exe
- pas2exe = "nice -n 19 " + pas2exe
- print pas1exe
- txt=txt+commands.getoutput(pas1exe)
- #os.system(pas1exe)
- print pas2exe
- time.sleep(5)
- txt=txt+commands.getoutput(pas2exe)
- #os.system(pas2exe)
+ x264profile=" --level 42 --profile high "
+ x264crf=str(crf)
+ txt=""
+ os.environ['LANG']="ja_JP.UTF-8"
+ exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - | "
+ exe=exe+get_x264_commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf)
+ exe = "nice -n 19 " + exe
+ txt=""
+ recdblist.printutf8(exe)
+ try:
+ txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
+ except:
+ ""
+ recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
+
+def getMovieBaseSize(pin):
+ ffmpeg=configreader.getpath("ffmpeg")
+ os.environ['LANG']="ja_JP.UTF-8"
+ exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
+ txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
+ rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
+ sizeMaxX=0
+ sizeMaxY=0
+ txtls=txts.split("\n")
+ for t in txtls:
+ rM=rT.match(t)
+ if rM:
+ sizetxt=rM.group(1)
+ partxt=rM.group(2)
+ tX=int(sizetxt.split("x")[0])
+ tY=int(sizetxt.split("x")[1])
+ tEX=int(partxt.split(":")[0])
+ tEY=int(partxt.split(":")[1])
+ if sizeMaxX<tX:
+ sizeMaxX=tX
+ sizeMaxY=tY
+ return [sizeMaxX,sizeMaxY]
+def getMoviePAR(pin):
+ ffmpeg=configreader.getpath("ffmpeg")
+ os.environ['LANG']="ja_JP.UTF-8"
+ exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
+ txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
+ rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")
+ #rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
+ sizeMaxX=0
+ parx=0
+ pary=0
+ txtls=txts.split("\n")
+ for t in txtls:
+ rM=rT.match(t)
+ if rM:
+ sizetxt=rM.group(1)
+ dartxt=rM.group(2)
+ tX=int(sizetxt.split("x")[0])
+ tY=int(sizetxt.split("x")[1])
+ tEX=int(dartxt.split(":")[0])
+ tEY=int(dartxt.split(":")[1])
+ if sizeMaxX<tX:
+ sizeMaxX=tX
+ if tX==1920 and tY==1080:
+ parx=1
+ pary=1
+ else:
+ parx=tEX
+ pary=tEY
+ return [parx,pary]
+def getMovieDAR(pin):
+ ffmpeg=configreader.getpath("ffmpeg")
+ os.environ['LANG']="ja_JP.UTF-8"
+ exe=ffmpeg+u" -i \""+pin+"\" 2>&1"
+ txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
+ rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")
+ sizeMaxX=0
+ darx=0
+ dary=0
+ txtls=txts.split("\n")
+ for t in txtls:
+ rM=rT.match(t)
+ if rM:
+ sizetxt=rM.group(1)
+ dartxt=rM.group(2)
+ tX=int(sizetxt.split("x")[0])
+ tY=int(sizetxt.split("x")[1])
+ tEX=int(dartxt.split(":")[0])
+ tEY=int(dartxt.split(":")[1])
+ if sizeMaxX<tX:
+ sizeMaxX=tX
+ if tX==1920 and tY==1080:
+ darx=16
+ dary=9
+ else:
+ darx=tEX
+ dary=tEY
+ return [darx,dary]
+def get_par_size(pin,y):
+ tSize=getMovieBaseSize(pin)
+ if tSize[1] != 0 :
+ tX=tSize[0]*10*y/tSize[1]
+ tY=y
+ if tX>int(tX/10)*10:
+ tX=tX/10+1
+ else:
+ tX=tX/10
+ else:
+ tX=-1
+ tY=-1
+ return [tX,tY]
+
+def get_x264core_version():
+ x264=configreader.getpath("x264")
+ t1=commands.getoutput(x264+" --help|grep core")
+ rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")
+ rM=rT.match(t1)
+ v=-1
+ if rM:
+ v=int(rM.group(1))
+ return v
+def get_x264_commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=0,bitrate=0):
+ x264=configreader.getpath("x264")
+ os.environ['LANG']="ja_JP.UTF-8"
+ x264_sar="--sar "+sar
+ x264preset=u"--preset "+preset
+ x264fps="--fps "+fps
+ if crf==0:
+ x264bitrate=u"--bitrate "+str(bitrate)
else:
- pas1exe = "mencoder \'" + pin + "\' -quiet -ovc x264 " + encvf + " -x264encopts " + bitrate + ":threads=auto -oac mp3lame -lameopts cbr:br=128 -o \'" + pout + "\'"
- pas1exe = "nice -n 19 " + pas1exe
- print pas1exe
- txt=txt+commands.getoutput(pas1exe)
- logo=pin
- logo=logo.replace(".sa.avi",".ts")
- logo=logo.replace(".ts",".log")
- f=open(logo,'a')
- s=len(txt)
- stxt=""
- if s>100:
- stxt=txt[s-300:]
+ x264crf=u"--crf "+str(crf)
+ x264_addline=configreader.getenv("x264_addline")
+ x264_thread="auto"
+ try:
+ xtt=configreader.getenv("x264_thread")
+ xtt=int(xtt)
+ if xtt>0:
+ x264_thread=str(xtt)
+ except:
+ x264_thread="auto"
+ x264_addline=configreader.getenv("x264_addline")
+ if get_x264core_version()>103:
+ x264res=u"--input-res "+x264streamsize
+ 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
else:
- stxt=txt
- s=stxt.find("\n")
- if s>0:
- s=s+len("\n")
- stxt=stxt[s:]
- txt="\n####mencoder-log####\n"+stxt
- f.write(txt)
- f.close()
-
\ No newline at end of file
+ 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
+ return exe
\ No newline at end of file