OSDN Git Service

fix dual audio bug.
[rec10/rec10-git.git] / rec10 / trunk / src / ts2x264.py
index e6da110..8a838bf 100644 (file)
 #!/usr/bin/python
 # coding: UTF-8
 # Rec10 TS Recording Tools
-# Copyright (C) 2009 Yukikaze
+# Copyright (C) 2009-2010 Yukikaze
+import commands
 import os
-import time
 import re
-
-def ts2x264(pin,pout,opts):#sizeは"HD"か"SD"
-    pout = pout.encode('utf-8')
-    pin = pin.encode('utf-8')
+import time
+import tv2avi
+import recdblist
+def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
     """
     pinで指定されたファイルをpoutにx264でエンコードして書き出す
     """
-    isAnime=0
-    size="SD"
-
-    makemini=0
-    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"
-    if re.search("H",opts):
-        size="HD"
-    if re.search("S",opts):
-        size="SD"
-    if re.search("t",opts):
-        makemini=1
-    if isAnime==1:
-        encvf="-vf pullup,softskip,pp=l5,"
+    dualpass = 0
+    isvfr=0
+    is24fps=0
+    islight=0
+    size="Tall"
+    quality=5
+    if re.search("2", opts):
+        dualpass = 1
+    if re.search("H", opts):
+        size = "HD"
+        quality=7
+    if re.search("S", opts):
+        size = "SD"
+        quality=3
+    if re.search("F", opts):
+        size = "FHD"
+        quality=5
+    if re.search("L", opts):
+        size = "Short"
+        quality=3
+    if re.search("G", opts):
+        size = "LowHD"
+        quality=5
+    if re.search("v", opts):
+        is24fps=1
+    if re.search("a", opts):
+        is24fps=1
+        quality=quality+2
+    if re.search("l",opts):
+        quality=quality-2
+    if re.search("h",opts):
+        quality=quality+2
+    if re.search("d",opts):
+        encode_ffmpeg(pin, pout, size, dualpass ,is24fps , quality)
+    elif re.search("5",opts):
+        encode_ffmpeg(pin, pout, size, dualpass ,is24fps , quality)
     else:
-        encvf="-vf pp=l5,"
-    if size=="SD":
-        encvf=encvf+"scale=720:480,harddup"
-        bitrate="bitrate=1250"
-    elif size=="HD":
-        encvf=encvf+"scale=1280:720,harddup"
-        bitrate="bitrate=2000"
-    elif size=="FHD":
-        encvf=encvf+"scale=1920:1080,harddup"
-        bitrate="bitrate=2500"
-    elif size=="WQVGA":
-        encvf=encvf+"scale=400:240,harddup"
-        bitrate="bitrate=200"
+        encode(pin, pout,size,dualpass,is24fps,quality)
+def encode(pin,pout,size,is2pass,is24fps,quality):
+    encvf=""
+    txt=""
+    encvf="-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"
+    bm=1
+    scale=""
+    bnum=2000
+    if size == "HD":
+        encvf = encvf + ",scale=1280:720::0:3"+harddup
+        bitrate = "bitrate=" + str(tv2avi.Bitrate_HD*bm)
+        bnum=tv2avi.Bitrate_HD*bm
+        crf="crf=20"
+    elif size == "SD":
+        encvf = encvf + ",scale=720:480::0:3"+harddup
+        bitrate = "bitrate=" + str(tv2avi.Bitrate_SD*bm)
+        bnum=tv2avi.Bitrate_SD*bm
+        crf="crf=20"
+    elif size == "FHD":
+        encvf = encvf + ",scale=1920:1080::0:3"+harddup
+        bitrate = "bitrate=" + str(tv2avi.Bitrate_FHD*bm)
+        bnum=tv2avi.Bitrate_FHD*bm
+        crf="crf=22"
+    elif size == "Short":
+        encvf = encvf +harddup
+        bitrate = "bitrate=" + str(tv2avi.Bitrate_Short*bm)
+        bnum=tv2avi.Bitrate_Short*bm
+        crf="crf=20"
+    elif size == "LowHD":
+        encvf = encvf + ",scale=1280:720::0:3"+harddup
+        bitrate = "bitrate=" + str(tv2avi.Bitrate_LowHD*bm)
+        bnum=tv2avi.Bitrate_LowHD*bm
+        crf="crf=22"
     else:
-        encvf=encvf+"scale=1280:720,harddup"
-        bitrate="bitrate=2000"
+        encvf = encvf + ",scale=1280:720::0:3"+harddup
+        bitrate = "bitrate=" + str(tv2avi.Bitrate_HD*bm)
+        bnum=tv2avi.Bitrate_HD*bm
+        crf="crf=22"
+    x264enc=""
+    x264enc1=""
+    x264enc2=""
 
-    if dualpass==1:
-        pas1exe="mencoder \'"+pin+"\' -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+"\' -ovc x264 "+encvf+" -x264encopts "+bitrate+":threads=auto:pass=2 -passlogfile "+pin+".log "+"-oac mp3lame -lameopts cbr:br=128 -o \'"+pout+"\'"
-        pas1exe="/usr/bin/nice -n 19 "+pas1exe
-        pas2exe="/usr/bin/nice -n 19 "+pas2exe
-        #exe="mencoder \'"+pin+"\' -ovc x264 "+encvf+" -x264encopts "+bitrate+":threads=auto -oac mp3lame -lameopts cbr:br=128 -o \'"+pout+"\'"
-        #exe="/usr/bin/nice -n 19 "+exe
-        #print "Now. encoding..."+exe
-        print pas2exe
-        os.system(pas1exe)
-        print "1pass終了"
+    if quality<3:
+        x264enc=":me=dia:partitions=none:frameref=1:subme=0:scenecut=0:trellis=0"
+        x264enc1=x264enc
+        x264enc2=x264enc1
+    elif quality<5:
+        x264enc=":me=dia:partitions=i8x8,i4x4:frameref=1:subme=1:trellis=0"
+        x264enc1=x264enc
+        x264enc2=x264enc1
+    elif quality<7:
+        x264enc=""
+        x264enc1=x264enc
+        x264enc2=x264enc1
+    elif quality<9:
+        x264enc=":direct_pred=auto:frameref=5:subme=8:bframes=1:trellis=1"
+        x264enc1=x264enc
+        x264enc2=x264enc1
+    elif quality<11:
+        x264enc=":frameref=5:subme=8:subq=7:trellis=2:partitions=all:8x8dct:direct_pred=auto:me=umh:bframes=3"
+        x264enc1=x264enc
+        x264enc2=x264enc1
+    crf=""
+    if is24fps==1:
+        ofps="-ofps 24000/1001"
+        fps="-fps 30000/1001"
+        encvf="-vf pullup,softskip"
+        harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
+        bm=1
+        if quality<3:
+            vbv=":vbv_maxrate="+str(bnum*2)
+            x264enc=":me=dia:aq-strength=0.6:deblock=1,1:psy-rd=0.4:partitions=none:frameref=1:subme=0:scenecut=0:trellis=0"
+            x264enc1=x264enc+vbv
+            x264enc2=x264enc1
+        elif quality<5:
+            vbv=":vbv_maxrate="+str(bnum*2)
+            x264enc=":me=dia:aq-strength=0.6:deblock=1,1:psy-rd=0.4:partitions=i8x8,i4x4:frameref=1:subme=1:trellis=0"
+            x264enc1=x264enc+vbv
+            x264enc2=x264enc1
+        elif quality<7:
+            vbv=":vbv_maxrate="+str(bnum*2)
+            x264enc=":aq-strength=0.6:deblock=1,1:psy-rd=0.4"
+            x264enc1=x264enc+vbv
+            x264enc2=x264enc1
+        elif quality<9:
+            vbv=":vbv_maxrate="+str(bnum*2)
+            x264enc=":direct_pred=auto:aq-strength=0.6:deblock=1,1:psy-rd=0.4:frameref=5:subme=8:bframes=1:trellis=1"
+            x264enc1=x264enc+vbv
+            x264enc2=x264enc1
+        elif quality<11:
+            vbv=":vbv_maxrate="+str(bnum*2)
+            x264enc=":frameref=5:subme=8:subq=7:trellis=2:aq-strength=0.6:deblock=1,1:psy-rd=0.4:partitions=all:8x8dct:direct_pred=auto:me=umh:bframes=3"
+            x264enc1=x264enc+vbv
+            x264enc2=x264enc1
+    if is2pass == 1:
+        pas1exe = "mencoder \'" + pin + "\' -vfm ffmpeg -quiet -nosound -sws 9 -ovc x264 " + encvf +" "+fps+" "+ofps+" -x264encopts " + bitrate +x264enc1+ ":threads=auto:level_idc=42:pass=1:turbo=2 -passlogfile \'" + pin + ".log\' " + " -o /dev/null"
+        pas2exe = "mencoder \'" + pin + "\' -vfm ffmpeg -quiet -sws 9 -ovc x264 " + encvf +" "+fps+ " "+ofps+" -x264encopts " + bitrate +x264enc2+ ":level_idc=42:threads=auto:pass=2 -passlogfile \'" + pin + ".log\' -oac mp3lame -lameopts cbr:br=256 -o \'" + pout + "\'"
+        pas1exe = "nice -n 19 " + pas1exe
+        pas2exe = "nice -n 19 " + pas2exe
+        recdblist.printutf8(pas1exe)
+        txt=txt+"Cmd : "+pas1exe+"\n"
+        txtt=""
+        try:
+            txtt=unicode(commands.getoutput(pas1exe.encode('utf-8')),'utf-8')
+        except:
+            ""
+        txt=txt+txtt
+        recdblist.printutf8(pas2exe)
         time.sleep(5)
-        os.system(pas2exe)
+        txtt=""
+        try:
+            txtt=unicode(commands.getoutput(pas2exe.encode('utf-8')),'utf-8')
+        except:
+            ""
+        txt=txt+txtt
     else:
-        pas1exe="mencoder \'"+pin+"\' -ovc x264 "+encvf+" -x264encopts "+bitrate+":threads=auto -oac mp3lame -lameopts cbr:br=128  -o \'"+pout+"\'"
-        os.system(pas1exe)
-    if makemini==1:
-        extt = os.path.splitext(pout)
-        minipout=extt[0]+"mini"+".flv"
-        ts2minix264(pin,minipout)
-        #miniexe="mencoder 咲-Saki- 第6話「開幕」[S].ts -ovc x264 -vf pullup,softskip,pp=l5,scale=1280:720,harddup -x264encopts bitrate=2000:crf=20:threads=auto -oac mp3lame -lameopts cbr:br=128 -o
-
-
-def ts2minix264(pin,pout):
-    miniexe="mencoder \'"+pin+"\' -ovc x264 -vf pullup,softskip,pp=l5,scale=512:384,harddup "
-    miniexe=miniexe+"-x264encopts bitrate=300:threads=auto -oac mp3lame -lameopts cbr:br=64 -o \'"+pout+"\'"
-    os.system(miniexe)
+        pas1exe = "mencoder \'" + pin + "\' -vfm ffmpeg -quiet -sws 9 -ovc x264 " + encvf+" "+fps + " "+ofps+" -x264encopts " + bitrate +x264enc+":level_idc=42:threads=auto -oac mp3lame -lameopts cbr:br=256 -o \'" + pout + "\'"
+        pas1exe = "nice -n 19 " + pas1exe
+        recdblist.printutf8(pas1exe)
+        txt=txt+"Cmd : "+pas1exe+"\n"
+        txtt=""
+        try:
+            txtt=unicode(commands.getoutput(pas1exe.encode('utf-8')),'utf-8')
+        except:
+            ""
+        txt=txt+txtt
+    recdblist.addlog( pin, txt, "mencoder-log")
 
-def ts2minix264(pin):
-    extt = os.path.splitext(pin)
-    pout=extt[0]+"mini"+".flv"
-    miniexe="mencoder \'"+pin+"\' -ovc x264 -vf pullup,softskip,pp=l5,scale=512:384,harddup "
-    miniexe=miniexe+"-x264encopts bitrate=300:threads=auto -oac mp3lame -lameopts cbr:br=64 -o \'"+pout+"\'"
-    os.system(miniexe)
-"""    
-anime=0
-pin=""
-pout=""
-size=""
-for opt, args in optlist:
-       if opt in ("-i", "--input"):
-               pin=args
-       if opt in ("-o", "--output"):
-               pout=args
-       if opt in ("-s", "--size"):
-               size=args
-       if opt in ("-a", "--anime"):
-               anime=1
-
-#ts2x264(pin,pout,size,anime)
-"""
+def encode_ffmpeg(pin,pout,size,is2pass,is24fps,quality):
+    """
+    
+    """
+    ffmpeg="ffmpeg"
+    if quality<3:
+        x264enc="-vpre hq -level 42 -me_method dia -partitions none -refs 1 -scenecut 0 -trellis 0 "
+        x264enc1=x264enc
+        x264enc2=x264enc1
+    elif quality<5:
+        x264enc="-vpre hq -level 42 -me_method dia -partitions parti8x8+parti4x4 -refs 1 -trellis 0 "
+        x264enc1="-vpre hq -level 42 -me_method dia -partitions none -refs 1 -trellis 0 "
+        x264enc2=x264enc
+    elif quality<7:
+        x264enc="-vpre hq -level 42 "
+        x264enc1="-vpre hq -level 42 -me_method dia -partitions none "
+        x264enc2=x264enc
+    elif quality<9:
+        x264enc="-vpre hq -level 42 -directpred auto -refs 5 -subq 6 -bf 1 -trellis 1 "
+        x264enc1="-vpre hq -level 42 -me_method dia -partitions none -refs 1 -subq 6 -bf 1 -trellis 1 "
+        x264enc2=x264enc
+    elif quality<11:
+        x264enc="-vpre hq -level 42 -refs 5 -subq 7 -trellis 2 -partitions all -directpred auto -me_method umh -bf 3 "
+        x264enc1="-vpre hq -level 42 -me_method dia -partitions none -refs 1 -subq 7 -trellis 2 -directpred auto -bf 3 "
+        x264enc2=x264enc
+    if is24fps==1:
+        if quality<3:
+            x264enc="-vpre hq -level 42 -me_method dia -partitions none -refs 1 -scenecut 0 -trellis 0 -deblockalpha 1 -deblockbeta 1 "
+            x264enc1="-vpre hq -level 42 -me_method dia -partitions none -refs 1 -scenecut 0 -trellis 0 -deblockalpha 1 -deblockbeta 1 "
+            x264enc2=x264enc
+        elif quality<5:
+            x264enc="-vpre hq -level 42 -me_method dia -partitions parti8x8+parti4x4 -refs 1 -trellis 0 -deblockalpha 1 -deblockbeta 1 "
+            x264enc1="-vpre hq -level 42 -me_method dia -partitions none -refs 1 -trellis 0 -deblockalpha 1 -deblockbeta 1 "
+            x264enc2=x264enc
+        elif quality<7:
+            x264enc="-vpre hq -level 42 -deblockalpha 1 -deblockbeta 1 "
+            x264enc1="-me_method dia -partitions none -refs 1 -vpre hq -level 42 -deblockalpha 1 -deblockbeta 1 "
+            x264enc2=x264enc
+        elif quality<9:
+            x264enc="-vpre hq -level 42 -directpred auto -refs 5 -subq 6 -bf 1 -trellis 1 -deblockalpha 1 -deblockbeta 1 "
+            x264enc1="-vpre hq -level 42 -directpred auto -me_method dia -partitions none -refs 1 -subq 6 -bf 1 -trellis 1 -deblockalpha 1 -deblockbeta 1 "
+            x264enc2=x264enc
+        elif quality<11:
+            x264enc="-vpre hq -level 42 -refs 5 -subq 7 -trellis 2 -partitions all -directpred auto -me_method umh -bf 3 -deblockalpha 1 -deblockbeta 1 "
+            x264enc1="-vpre hq -level 42 -refs 1 -subq 7 -trellis 2 -partitions none -directpred auto -me_method dia -bf 3 -deblockalpha 1 -deblockbeta 1 "
+            x264enc2=x264enc
+    if size == "HD":
+        s = "-s 1280x720 "
+        bitrate = str(tv2avi.Bitrate_HD)
+        crf="crf=20"
+    elif size == "SD":
+        s ="-s 720x480 "
+        bitrate = str(tv2avi.Bitrate_SD)
+        crf="crf=20"
+    elif size == "FHD":
+        s ="-s 1920x1080 "
+        bitrate = str(tv2avi.Bitrate_FHD)
+        crf="crf=22"
+    elif size == "Short":
+        s= ""
+        bitrate = str(tv2avi.Bitrate_Short)
+        crf="crf=20"
+    elif size == "LowHD":
+        s = "-s 1280x720 "
+        bitrate = str(tv2avi.Bitrate_LowHD)
+        crf="crf=22"
+    else:
+        s = "-s 1280x720 "
+        bitrate = str(tv2avi.Bitrate_LowHD)
+        crf="crf=22"
+    txt=""
+    if is2pass==1:
+        os.environ['LANG']="ja_JP.UTF-8"
+        exe1=ffmpeg+" -y -i \""+pin+"\" -pass 1 -vsync 400 -vcodec libx264 -b "+bitrate+"k "+x264enc1+s+"-deinterlace -an -f avi -passlogfile \'" + pin + ".log\' " + " /dev/null"
+        exe2=ffmpeg+" -y -i \""+pin+"\" -pass 2 -vsync 400 -vcodec libx264 -b "+bitrate+"k "+x264enc2+s+"-deinterlace -an -f avi -passlogfile \'" + pin + ".log\' \"" +pout+"\"" #aviにすべきかどうか
+        recdblist.printutf8(exe1)
+        exe1 = "nice -n 19 " + exe1
+        exe2 = "nice -n 19 " + exe2
+        txt=txt+u"Cmd : "+exe1+"\n"+exe2+"\n"
+        txtt=""
+        try:
+            txtt=unicode(commands.getoutput(exe1.encode('utf-8')),'utf-8')
+        except:
+            ""
+        txt=txt+txtt
+        txtt=""
+        try:
+            txtt=unicode(commands.getoutput(exe2.encode('utf-8')),'utf-8')
+        except:
+            ""
+        txt=txt+txtt
+    else:
+        os.environ['LANG']="ja_JP.UTF-8"
+        exe=ffmpeg+" -y -i \""+pin+"\" -vsync 400 -vcodec libx264 -b "+bitrate+"k "+x264enc+s+"-deinterlace -an \"" +pout+"\""
+        txt=txt+"Cmd : "+exe+"\n"
+        exe = "nice -n 19 " + exe
+        txt=txt+"Cmd : "+exe+"\n"
+        txtt=""
+        try:
+            txtt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
+        except:
+            ""
+        txt=txt+txtt
+    recdblist.addlog( pin, txt, "dual audio ffmpeg-log")
+    
\ No newline at end of file