OSDN Git Service

implement sar using encode function.
authorgn64_jp <gn64_jp@4e526526-5e11-4fc0-8910-f8fd03428081>
Wed, 23 Jun 2010 00:54:13 +0000 (00:54 +0000)
committergn64_jp <gn64_jp@4e526526-5e11-4fc0-8910-f8fd03428081>
Wed, 23 Jun 2010 00:54:13 +0000 (00:54 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/rec10@626 4e526526-5e11-4fc0-8910-f8fd03428081

rec10/trunk/src/ts2x264.py

index 578c174..a466d0b 100644 (file)
@@ -62,7 +62,9 @@ def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
     elif re.search("5",opts):
         encode_ffmpeg_kai(pin,pout,size,is24fps,quality,crf)
     elif re.search("Y",opts):
-        encode_ffmpeg_kai(pin,pout,size,is24fps,quality,crf)
+        encode_sar(pin,pout,size,is24fps,quality,crf)
+    elif re.search("Z",opts):
+        encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf)
     elif re.search("b",opts):
         try:
             tm2v=pin.replace(".ts",".m2v")
@@ -165,6 +167,101 @@ def encode(pin,pout,size,is24fps,quality,crf):
         ""
     os.system("rm "+streampath)
     recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
+def encode_sar(pin,pout,size,is24fps,quality,crf):
+    mencoder=configreader.getpath("mencoder")
+    x264=configreader.getpath("x264")
+    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"
+    x264fps="--fps 30000/1001"
+    x264streamsize=""
+    x264preset=""
+    x264tune=""
+    x264_bitrate="5000"
+    x264_thread="auto"
+    tsar=getMoviePAR(pin)
+    x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
+    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 is24fps==1:
+        ofps="-ofps 24000/1001"
+        fps="-fps 30000/1001"
+        x264fps="--fps 24000/1001"
+        x264tune="--tune animation"
+        encvf="-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 quality==1:
+        x264preset=u"--preset ultrafast"
+    elif quality==2:
+        x264preset=u"--preset veryfast"
+    elif quality==3:
+        x264preset=u"--preset fast"
+    elif quality==4:
+        x264preset=u"--preset medium"
+    elif quality==5:
+        x264preset=u"--preset slow"
+    elif quality==6:
+        x264preset=u"--preset slower"
+    if size == "WVGA_BASE" or size == "QVGA_BASE":
+        x264profile=" --level 32 --profile baseline "
+    else:
+        x264profile=" --level 41 --profile high "
+    x264crf=u"--crf "+str(crf)
+    os.environ['LANG']="ja_JP.UTF-8"
+    random.seed(pin)
+    random.jumpahead(10)
+    streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 999999)))
+    os.system(u"mkfifo "+streampath)
+    encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet -sws 9 "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" &"
+    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(pin,pout,size,is24fps,quality,crf):
     """
@@ -244,6 +341,93 @@ def encode_ffmpeg(pin,pout,size,is24fps,quality,crf):
     except:
         ""
     recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)
+def encode_ffmpeg_sar(pin,pout,size,is24fps,quality,crf):
+    """
+
+    """
+    ffmpeg=configreader.getpath("ffmpeg")
+    x264=configreader.getpath("x264")
+    fps=u"-r 29.970030 "
+    x264fps="--fps 30000/1001"
+    x264streamsize=""
+    x264preset=""
+    x264tune=""
+    x264_bitrate="2500"
+    x264_thread="auto"
+    tsar=getMoviePAR(pin)
+    x264_sar="--sar "+str(tsar[0])+":"+str(tsar[1])
+    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 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:
+        tsize=get_par_size(pin,720)
+        s = "-s "+str(tsize[0])+"x720 "
+        x264streamsize=str(tsize[0])+u"x720"
+        x264_bitrate="2500"
+    if quality==1:
+        x264preset=u"--preset ultrafast"
+    elif quality==2:
+        x264preset=u"--preset veryfast"
+    elif quality==3:
+        x264preset=u"--preset fast"
+    elif quality==4:
+        x264preset=u"--preset medium"
+    elif quality==5:
+        x264preset=u"--preset slow"
+    elif quality==6:
+        x264preset=u"--preset slower"
+    if size == "WVGA_BASE" or size == "QVGA_BASE":
+        x264profile=" --level 32 --profile baseline "
+    else:
+        x264profile=" --level 41 --profile high "
+    x264crf=u"--crf "+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+"-deinterlace -an -f rawvideo - |"
+    exe=exe+u" nice -n 19 "+x264+" "+x264_sar+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
+    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 encode_ffmpeg_kai(pin,pout,size,is24fps,quality,crf):
     """
 
@@ -357,10 +541,57 @@ def getMovieSize(pin):
                 tX2=tX2/10+1
             else:
                 tX2=tX2/10
-            if sizeMaxX<tX:
+            if sizeMaxX<tX2:
                 sizeMaxX=tX2
                 sizeMaxY=tY
     return [sizeMaxX,sizeMaxY]
+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
+                parx=tEX
+                pary=tEY
+    return [parx,pary]
 def getMovieDAR(pin):
     ffmpeg=configreader.getpath("ffmpeg")
     os.environ['LANG']="ja_JP.UTF-8"
@@ -399,4 +630,19 @@ def get16_9ffmpeg_s(pin,x,y):
             tn=tn*2#偶数にするための処理
             return tn
     except:
-        return 0
\ No newline at end of file
+        return 0
+def get_par_size(pin,y):
+    try:
+        tSize=getMovieBaseSize(pin)
+        tX=tSize[0]*10/tSize[1]*y
+        if tX>int(tX/10)*10:
+            tX=tX/10+1
+        else:
+            tX=tX/10
+        if tY>int(tY/10)*10:
+            tY=tY/10+1
+        else:
+            tY=tY/10
+        return [tX,tY]
+    except:
+        return [0,0]
\ No newline at end of file