OSDN Git Service

fix scale option of mencoder when blu-ray and interlaced option.
[rec10/rec10-git.git] / rec10 / trunk / src / ts2x264.py
index 35b6460..4a2b37f 100644 (file)
-#!/usr/bin/python
-# coding: UTF-8
-# Rec10 TS Recording Tools
-# Copyright (C) 2009-2010 Yukikaze
-import commands
-import configreader
-import os
-import os.path
-import re
-import random
-
-import recdblist
-def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
-    """
-    pinで指定されたファイルをpoutにx264でエンコードして書き出す
-    """
-    dualpass = 0
-    is24fps=0
-    size="HD"
-    crf=18
-    quality=4
-    quality=int(configreader.getenv("x264_preset"))
-    crf=int(configreader.getenv("crf"))
-    if re.search("H", opts):
-        size = "HD"
-    if re.search("S", opts):
-        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("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_kai(pin,pout,size,is24fps,quality,crf)
-    elif re.search("5",opts):
-        encode_ffmpeg_kai(pin,pout,size,is24fps,quality,crf)
-    elif re.search("Y",opts):
-        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")
-            encode(tm2v, pout,size,is24fps,quality,crf)
-        except Exception, inst:
-            recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst))
-    else:
-        try:
-            encode(pin, pout,size,is24fps,quality,crf)
-        except Exception, inst:
-            recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst))
-def encode(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"
-    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":
-        encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
-        x264streamsize=u"1280x720"
-        x264_bitrate="2500"
-    elif size == "WVGA":
-        encvf = encvf + ",scale=-2:480::0:3,expand=854:480"+harddup
-        x264streamsize=u"854x480"
-        x264_bitrate="1500"
-    elif size == "FullHD":
-        encvf = encvf + ",scale=-2:1080::0:3,expand=1920:1080"+harddup
-        x264streamsize=u"1920x1080"
-        x264_bitrate="5000"
-    elif size == "SD":
-        encvf = encvf + ",scale=720:-2::0:3,expand=720:480"+harddup
-        x264streamsize=u"720x480"
-        x264_bitrate="1250"
-    elif size == "QVGA_BASE":
-        encvf = encvf + ",scale=320:-2::0:3,expand=320:240"+harddup
-        x264streamsize=u"320x240"
-        x264_bitrate="300"
-    elif size == "WVGA_BASE":
-        encvf = encvf + ",scale=-2:480::0:3,expand=854:480"+harddup
-        x264streamsize=u"854x480"
-        x264_bitrate="1500"
-    else:
-        encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
-        x264streamsize=u"1280x720"
-        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+" "+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_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):
-    """
-    
-    """
-    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"
-    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":
-        s = "-s 1280x720 "
-        x264streamsize=u"1280x720"
-        x264_bitrate="2500"
-    elif size == "WVGA":
-        s = "-s 854x480 "
-        x264streamsize=u"854x480"
-        x264_bitrate="1500"
-    elif size == "FullHD":
-        s = "-s 1920x1080 "
-        x264streamsize=u"1920x1080"
-        x264_bitrate="5000"
-    elif size == "SD":
-        s = "-s 720x480 "
-        x264streamsize=u"720x480"
-        x264_bitrate="1250"
-    elif size == "QVGA_BASE":
-        s = "-s 320x240 "
-        x264streamsize=u"320x240"
-        x264_bitrate="300"
-    elif size == "WVGA_BASE":
-        s = "-s 854x480 "
-        x264streamsize=u"854x480"
-        x264_bitrate="1500"
-    else:
-        s = "-s 1280x720 "
-        x264streamsize=u"1280x720"
-        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+" "+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_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):
-    """
-
-    """
-    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"
-    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":
-        padx=get16_9ffmpeg_s(pin,1280,720)
-        s = "-s "+str(1280-2*padx)+"x720 "
-        if padx>0:
-            s=s+"-padleft "+str(padx)+" -padright "+str(padx)+" "
-        x264streamsize=u"1280x720"
-        x264_bitrate="2500"
-    elif size == "WVGA":
-        padx=get16_9ffmpeg_s(pin,854,480)
-        s = "-s "+str(854-2*padx)+"x480 "
-        if padx>0:
-            s=s+"-padleft "+str(padx)+" -padright "+str(padx)+" "
-        x264streamsize=u"854x480"
-        x264_bitrate="1500"
-    elif size == "FullHD":
-        padx=get16_9ffmpeg_s(pin,1920,1080)
-        s = "-s "+str(1920-2*padx)+"x1080 "
-        if padx>0:
-            s=s+"-padleft "+str(padx)+" -padright "+str(padx)+" "
-        x264streamsize=u"1920x1080"
-        x264_bitrate="5000"
-    elif size == "SD":
-        s = "-s 720x480 "
-        x264streamsize=u"720x480"
-        x264_bitrate="1250"
-    elif size == "QVGA_BASE":
-        s = "-s 320x240 "
-        x264streamsize=u"320x240"
-        x264_bitrate="300"
-    elif size == "WVGA_BASE":
-        s = "-s 854x480 "
-        x264streamsize=u"854x480"
-        x264_bitrate="1500"
-    else:
-        padx=get16_9ffmpeg_s(pin,1280,720)
-        s = "-s "+str(1280-2*padx)+"x720 "
-        if padx>0:
-            s=s+"-padleft "+str(padx)+" -padright "+str(padx)+" "
-        x264streamsize=u"1280x720"
-        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+" "+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 getMovieSize(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])
-            tX2=tX*tEX*10/tEY
-            if tX2>10*int(tX2/10):
-                tX2=tX2/10+1
-            else:
-                tX2=tX2/10
-            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"
-    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
-                darx=tEX
-                dary=tEY
-    return [darx,dary]
-def get16_9ffmpeg_s(pin,x,y):
-    try:
-        tDAR=getMovieDAR(pin)
-        if (tDAR[0]==16 and tDAR[1]==9):
-            return 0
-        elif tDAR==[0,0]:
-            return 0
-        else:
-            tSize=getMovieSize(pin)
-            tn=x-tSize[1]*x/tSize[0]
-            tn=tn/4
-            tn=tn*2#偶数にするための処理
-            return tn
-    except:
-        return 0
-def get_par_size(pin,y):
-    tSize=getMovieBaseSize(pin)
-    tX=tSize[0]*10*y/tSize[1]
-    tY=y
-    if tX>int(tX/10)*10:
-        tX=tX/10+1
-    else:
-        tX=tX/10
-    return [tX,tY]
\ No newline at end of file
+#!/usr/bin/python\r
+# coding: UTF-8\r
+# Rec10 TS Recording Tools\r
+# Copyright (C) 2009-2011 Yukikaze\r
+import commands\r
+import configreader\r
+import os\r
+import os.path\r
+import re\r
+import random\r
+import time\r
+import traceback\r
+import zip\r
+\r
+import recdblist\r
+def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"\r
+    """\r
+    pinで指定されたファイルをpoutにx264でエンコードして書き出す\r
+    """\r
+    dualpass = 0\r
+    is24fps=0\r
+    size="HD"\r
+    crf=18\r
+    quality=4\r
+    quality=int(configreader.getConfEnv("x264_preset"))\r
+    crf=int(configreader.getConfEnv("crf"))\r
+    deinterlace=1\r
+    bluray=0\r
+    sar=1\r
+    if re.search("H", opts):\r
+        size = "HD"\r
+    if re.search("S", opts):\r
+        size = "WVGA"\r
+    if re.search("F", opts):\r
+        size = "FullHD"\r
+    if re.search("W",opts):\r
+        size = "WVGA"\r
+    if re.search("MW1",opts):\r
+        size = "QVGA_BASE"\r
+        crf=crf+4\r
+        sar=0\r
+    if re.search("MW2", opts):\r
+        size = "WVGA_BASE"\r
+        crf=crf+2\r
+        sar=0\r
+    if re.search("v", opts):\r
+        is24fps=1\r
+        crf=int(configreader.getConfEnv("animation_crf"))\r
+    if re.search("a", opts):\r
+        is24fps=1\r
+        crf=int(configreader.getConfEnv("animation_crf"))\r
+    if re.search("I", opts):\r
+        deinterlace=0\r
+    if re.search("B", opts):\r
+        bluray=1\r
+        sar=0\r
+    if re.search("q",opts):\r
+        quality=quality-2\r
+    if re.search("w",opts):\r
+        quality=quality-1\r
+    if re.search("e",opts):\r
+        quality=quality+1\r
+    if re.search("r",opts):\r
+        quality=quality+2\r
+    if re.search("u",opts):\r
+        crf=crf+2\r
+    if re.search("i",opts):\r
+        crf=crf+1\r
+    if re.search("o",opts):\r
+        crf=crf-1\r
+    if re.search("p",opts):\r
+        crf=crf-2\r
+    if re.search("d",opts):#二カ国語放送の場合\r
+        tm2v=pin.replace(".ts",".m2v")\r
+        encodeFfmpegSar(tm2v,pout,size,is24fps,quality,crf,deinterlace=deinterlace,usebluray=bluray,usesar=sar)\r
+    elif re.search("5",opts):#5.1chの場合\r
+        encodeFfmpegSar(pin,pout,size,is24fps,quality,crf,deinterlace=deinterlace,usebluray=bluray,usesar=sar)\r
+    elif re.search("b",opts):#BonTsDemuxを使いたい場合\r
+        tm2v=pin.replace(".ts",".m2v")\r
+        encodeFfmpegSar(tm2v,pout,size,is24fps,quality,crf,deinterlace=deinterlace,usebluray=bluray,usesar=sar)\r
+    else:\r
+        try:\r
+            encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=1,usebluray=bluray,usesar=sar)\r
+            if os.path.exists(pout):\r
+                if not os.path.getsize(pout)>1*1000:\r
+                    recdblist.addCommonlogEX("Warning", "Old mencoder option used.","","",log_level=200)\r
+                    try:\r
+                        encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=0,usebluray=bluray,usesar=sar)\r
+                    except Exception, inst:\r
+                        recdblist.addCommonlogEX("Error", "ts2x264_mencoder_old(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)\r
+            else:\r
+                recdblist.addCommonlogEX("Warning", "Old mencoder option used.","","",log_level=200)\r
+                try:\r
+                    encodeMencoderSar(pin, pout,size,is24fps,quality,crf,deinterlace=deinterlace,uselavf=0,usebluray=bluray,usesar=sar)\r
+                except Exception, inst:\r
+                    recdblist.addCommonlogEX("Error", "ts2x264_mencoder_old(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)\r
+        except Exception, inst:\r
+            recdblist.addCommonlogEX("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst)+traceback.format_exc(),log_level=200)\r
+def encodeMencoderSar(pin,pout,size,is24fps,quality,crf,deinterlace=1,uselavf=0,usebluray=0,usesar=1):\r
+    mencoder=configreader.getConfPath("mencoder")\r
+    tee=configreader.getConfPath("tee")\r
+    encvf=""\r
+    txt=""\r
+    encvf="-sws 9 -vf yadif=0,pp=l5"\r
+    harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"\r
+    ofps="-ofps 30000/1001"\r
+    fps="-fps 30000/1001"\r
+    x264fps="30000/1001 --keyint 30"\r
+    x264streamsize=""\r
+    x264preset=""\r
+    x264tune=""\r
+    x264_bitrate="5000"\r
+    x264interlaced=0\r
+    x264sar="1:1"\r
+    tsar=getMoviePAR2(pin)\r
+    if usesar==1:\r
+        x264sar=str(tsar[0])+u":"+str(tsar[1])\r
+    lavft=""\r
+    if is24fps==1:\r
+        ofps="-ofps 24000/1001"\r
+        fps="-fps 30000/1001"\r
+        x264fps="24000/1001 --keyint 24"\r
+        x264tune="--tune animation"\r
+        encvf="-sws 9 -vf pullup,softskip"\r
+        harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"\r
+    if size == "HD":\r
+        tsize=getParSize(pin,720)\r
+        if usesar==1:\r
+            encvf = encvf + ",scale=-3:720::0:3"+harddup\r
+            x264streamsize=str(tsize[0])+u"x720"\r
+        else:\r
+            encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup\r
+            x264streamsize=u"1280x720"\r
+        x264_bitrate="2500"\r
+    elif size == "WVGA":\r
+        tsize=getParSize(pin,480)\r
+        if usesar==1:\r
+            encvf = encvf + ",scale=-3:480::0:3"+harddup\r
+            x264streamsize=str(tsize[0])+u"x480"\r
+        else:\r
+            encvf = encvf + ",scale=-2:480::0:3,expand=854:480"+harddup\r
+            x264streamsize=u"854x480"\r
+        x264_bitrate="1500"\r
+    elif size == "FullHD":\r
+        tsize=getParSize(pin,1080)\r
+        if usesar==1:\r
+            encvf = encvf + ",scale=-3:1080::0:3"+harddup\r
+            x264streamsize=str(tsize[0])+u"x1080"\r
+        else:\r
+            encvf = encvf + ",scale=-2:1080::0:3,expand=1920:1080"+harddup\r
+            x264streamsize=u"1920x1080"\r
+        x264_bitrate="5000"\r
+    elif size == "QVGA_BASE":\r
+        tsize=getParSize(pin,240)\r
+        if usesar==1:\r
+            encvf = encvf + ",scale=-3:240::0:3"+harddup\r
+            x264streamsize=str(tsize[0])+u"x240"\r
+        else:\r
+            encvf = encvf + ",scale=320:-2::0:3,expand=320:240"+harddup\r
+            x264streamsize=u"320x240"\r
+        x264_bitrate="300"\r
+    elif size == "WVGA_BASE":\r
+        tsize=getParSize(pin,480)\r
+        if usesar==1:\r
+            encvf = encvf + ",scale=-3:480::0:3"+harddup\r
+            x264streamsize=str(tsize[0])+u"x480"\r
+        else:\r
+            encvf = encvf + ",scale=-2:480::0:3,expand=854:480"+harddup\r
+            x264streamsize=u"854x480"\r
+        x264_bitrate="1500"\r
+    else:\r
+        tsize=getParSize(pin,720)\r
+        if usesar==1:\r
+            encvf = encvf + ",scale=-3:720::0:3"+harddup\r
+            x264streamsize=str(tsize[0])+u"x720"\r
+        else:\r
+            encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup\r
+            x264streamsize=u"1280x720"\r
+        x264_bitrate="2500"\r
+    if deinterlace==0:\r
+        tsize=getMovieBaseSize2(pin)\r
+        ofps="-ofps 30000/1001"\r
+        #fps="-fps 30000/1001"\r
+        fps=""\r
+        x264fps="30000/1001 --keyint 30"\r
+        x264interlaced=1\r
+        encvf="-vf hqdn3d=2:1:2"\r
+        harddup=",harddup"\r
+        encvf=encvf+harddup\r
+        x264streamsize=str(tsize[0])+u"x"+str(tsize[1])\r
+        if usebluray==1:\r
+            encvf = u"-sws 9 "+encvf + ",scale=-2:1080::0:3,expand=1920:1080"+harddup\r
+            x264streamsize=u"1920x1080"\r
+    if tsize[0] <= 0 or tsize[1] <= 0:\r
+        encvf="-sws 9 -vf yadif=0,pp=l5"\r
+        harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"\r
+        ofps="-ofps 30000/1001"\r
+        fps="-fps 30000/1001"\r
+        x264fps="30000/1001 --keyint 30"\r
+        tsize=[1280,720]\r
+        encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup\r
+        x264streamsize=u"1280x720"\r
+        x264_bitrate="2500"\r
+    if quality==1:\r
+        x264preset=u"ultrafast"\r
+    elif quality==2:\r
+        x264preset=u"veryfast"\r
+    elif quality==3:\r
+        x264preset=u"fast"\r
+    elif quality==4:\r
+        x264preset=u"medium"\r
+    elif quality==5:\r
+        x264preset=u"slow"\r
+    elif quality==6:\r
+        x264preset=u"slower"\r
+    else:\r
+        x264preset=u"medium"\r
+    if size == "WVGA_BASE" or size == "QVGA_BASE":\r
+        x264profile=" --level 32 --profile baseline "\r
+    else:\r
+        x264profile=" --level 41 --profile high "\r
+    if uselavf==1:\r
+        lavft="-demuxer lavf "\r
+    x264crf=str(crf)\r
+    os.environ['LANG']="ja_JP.UTF-8"\r
+    random.seed(pin)\r
+    random.jumpahead(10)\r
+    temptime=int(time.time())\r
+    temptime=temptime % 9697\r
+    random.jumpahead(temptime)\r
+    streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 99999999)))\r
+    unicode(commands.getoutput(u"mkfifo "+streampath),'utf-8','ignore')\r
+    logmencoder=recdblist.getLogTitle(pin)+".mencoder.log"\r
+    logx264=recdblist.getLogTitle(pin)+".x264.log"\r
+    encexe=mencoder+u" \""+pin+u"\" -quiet -vfm ffmpeg "+lavft+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" 2>&1 | "+tee+" \""+logmencoder+"\" & "\r
+    encexe=encexe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout, streampath, x264streamsize, crf=x264crf,interlaced=x264interlaced,bluray=usebluray)\r
+    #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\r
+    encexe=u"nice -n 19 " +encexe+" 2>&1 | "+tee+" \""+logx264+"\""\r
+    try:\r
+        recdblist.addCommandSelfLog(pin, encexe)\r
+    except Exception, inst:\r
+        print type(inst)\r
+        print str(inst)\r
+        print traceback.print_exc(file=sys.stdout)\r
+    recdblist.printutf8(encexe)\r
+    txt=""\r
+    try:\r
+        txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8','ignore')\r
+    except:\r
+        ""\r
+    os.remove(streampath)\r
+    zip.addFile2FileZip(logmencoder, recdblist.getLogTitle(pin)+".log.zip")\r
+    zip.addFile2FileZip(logx264, recdblist.getLogTitle(pin)+".log.zip")\r
+    os.remove(logmencoder)\r
+    os.remove(logx264)\r
+    recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)\r
+#    recdblist.addCommandLogTar(pin,u"Mencoder","mencoder", encexe,txt)\r
+def encodeFfmpegSar(pin,pout,size,is24fps,quality,crf,deinterlace=1,usebluray=0,usesar=1):\r
+    """\r
+\r
+    """\r
+    ffmpeg=configreader.getConfPath("ffmpeg")\r
+    tee=configreader.getConfPath("tee")\r
+    fps=u"-r 29.970030 "\r
+    x264fps="30000/1001 --keyint 30"\r
+    x264streamsize=""\r
+    x264preset=""\r
+    x264tune=""\r
+    x264_bitrate="2500"\r
+    x264_thread="auto"\r
+    x264interlaced=0\r
+    tsar=getMoviePAR2(pin)\r
+    filter="-deinterlace"\r
+    x264sar=str(tsar[0])+":"+str(tsar[1])\r
+    if is24fps==1:\r
+        fps=u"-r 23.976023 "\r
+        x264fps="24000/1001 --keyint 24"\r
+        x264tune="--tune animation"\r
+    if size == "HD":\r
+        tsize=getParSize(pin,720)\r
+        s = "-s "+str(tsize[0])+"x720 "\r
+        x264streamsize=str(tsize[0])+u"x720"\r
+        x264_bitrate="2500"\r
+    elif size == "WVGA":\r
+        tsize=getParSize(pin,480)\r
+        s = "-s "+str(tsize[0])+"x480 "\r
+        x264streamsize=str(tsize[0])+u"x480"\r
+        x264_bitrate="1500"\r
+    elif size == "FullHD":\r
+        tsize=getParSize(pin,1080)\r
+        s = "-s "+str(tsize[0])+"x1080 "\r
+        x264streamsize=str(tsize[0])+u"x1080"\r
+        x264_bitrate="5000"\r
+    elif size == "SD":\r
+        tsize=getParSize(pin,480)\r
+        s = "-s "+str(tsize[0])+"x480 "\r
+        x264streamsize=str(tsize[0])+u"x480"\r
+        x264_bitrate="1250"\r
+    elif size == "QVGA_BASE":\r
+        tsize=getParSize(pin,240)\r
+        s = "-s "+str(tsize[0])+"x240 "\r
+        x264streamsize=str(tsize[0])+u"x240"\r
+        x264_bitrate="300"\r
+    elif size == "WVGA_BASE":\r
+        tsize=getParSize(pin,480)\r
+        s = "-s "+str(tsize[0])+"x480 "\r
+        x264streamsize=str(tsize[0])+u"x480"\r
+        x264_bitrate="1500"\r
+    else:\r
+        tsize=getParSize(pin,720)\r
+        s = "-s "+str(tsize[0])+"x720 "\r
+        x264streamsize=str(tsize[0])+u"x720"\r
+        x264_bitrate="2500"\r
+    if deinterlace==0:\r
+        tsize=getMovieBaseSize2(pin)\r
+        fps=""\r
+        s = "-s "+str(tsize[0])+"x"+str(tsize[1])+" "\r
+        x264fps="30000/1001 --keyint 30"\r
+        x264interlaced=1\r
+        filter=""\r
+        x264streamsize=str(tsize[0])+u"x"+str(tsize[1])\r
+    if quality==1:\r
+        x264preset=u"ultrafast"\r
+    elif quality==2:\r
+        x264preset=u"veryfast"\r
+    elif quality==3:\r
+        x264preset=u"fast"\r
+    elif quality==4:\r
+        x264preset=u"medium"\r
+    elif quality==5:\r
+        x264preset=u"slow"\r
+    elif quality==6:\r
+        x264preset=u"slower"\r
+    else:\r
+        x264preset=u"medium"\r
+    if size == "WVGA_BASE" or size == "QVGA_BASE":\r
+        x264profile=" --level 32 --profile baseline "\r
+    else:\r
+        x264profile=" --level 41 --profile high "\r
+    x264crf=str(crf)\r
+    txt=""\r
+    os.environ['LANG']="ja_JP.UTF-8"\r
+    log_ffmpeg=recdblist.getLogTitle(pin)+".ffmpeg.log"\r
+    log_x264=recdblist.getLogTitle(pin)+".x264.log"\r
+    exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+" "+filter+" -an -f rawvideo - 2> \""+log_ffmpeg+"\" | "\r
+    exe=exe+getX264Commandline(x264preset, x264sar, x264fps, x264profile, x264tune, pout,"-", x264streamsize, crf=x264crf,interlaced=x264interlaced,bluray=usebluray)\r
+    exe = "nice -n 19 " + exe+" 2>&1 | "+tee+" \""+log_x264+"\""\r
+    txt=""\r
+    #recdblist.addCommandSelfLog(log_ffmpeg,recdblist.getLogTitle(pin)+".log.zip")\r
+    #recdblist.addCommandSelfLog(log_x264,recdblist.getLogTitle(pin)+".log.zip")\r
+    recdblist.printutf8(exe)\r
+    recdblist.addCommandSelfLog(pin,exe)\r
+    try:\r
+        txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
+    except:\r
+        ""\r
+    recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)\r
+    zip.addFile2FileZip(log_ffmpeg, recdblist.getLogTitle(pin+".log.zip"))\r
+    zip.addFile2FileZip(log_x264, recdblist.getLogTitle(pin+".log.zip"))\r
+    os.remove(log_ffmpeg)\r
+    os.remove(log_x264)\r
+def getMovieBaseSize(pin):\r
+    ffmpeg=configreader.getConfPath("ffmpeg")\r
+    os.environ['LANG']="ja_JP.UTF-8"\r
+    exe=ffmpeg+u" -i \""+pin+"\" 2>&1"\r
+    txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
+    rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
+    sizeMaxX=0\r
+    sizeMaxY=0\r
+    txtls=txts.split("\n")\r
+    for t in txtls:\r
+        rM=rT.match(t)\r
+        if rM:\r
+            sizetxt=rM.group(1)\r
+            partxt=rM.group(2)\r
+            tX=int(sizetxt.split("x")[0])\r
+            tY=int(sizetxt.split("x")[1])\r
+            tEX=int(partxt.split(":")[0])\r
+            tEY=int(partxt.split(":")[1])\r
+            if sizeMaxX<tX:\r
+                sizeMaxX=tX\r
+                sizeMaxY=tY\r
+    return [sizeMaxX,sizeMaxY]\r
+def getMovieBaseSize2(pin):##動画開始後すぐに解像度が変更されたときに対処\r
+    ffmpeg=configreader.getConfPath("ffmpeg")\r
+    os.environ['LANG']="ja_JP.UTF-8"\r
+    exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"\r
+    try:\r
+        recdblist.addCommandSelfLog(pin, exe)\r
+    except Exception, inst:\r
+        print type(inst)\r
+        print str(inst)\r
+        print traceback.print_exc(file=sys.stdout)\r
+    txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
+    rT=re.compile(u"Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
+    sizeMaxX=0\r
+    sizeMaxY=0\r
+    txtls=txts.split("\n")\r
+    for t in txtls:\r
+        rM=rT.search(t)\r
+        if rM:\r
+            sizetxt=rM.group(1)\r
+            partxt=rM.group(2)\r
+            tX=int(sizetxt.split("x")[0])\r
+            tY=int(sizetxt.split("x")[1])\r
+            tEX=int(partxt.split(":")[0])\r
+            tEY=int(partxt.split(":")[1])\r
+            if sizeMaxX<tX:\r
+                sizeMaxX=tX\r
+                sizeMaxY=tY\r
+    os.remove(pin+".size.ts")\r
+    return [sizeMaxX,sizeMaxY]\r
+def getMoviePAR(pin):\r
+    ffmpeg=configreader.getConfPath("ffmpeg")\r
+    os.environ['LANG']="ja_JP.UTF-8"\r
+    exe=ffmpeg+u" -i \""+pin+"\" 2>&1"\r
+    txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
+    rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
+    sizeMaxX=0\r
+    parx=0\r
+    pary=0\r
+    txtls=txts.split("\n")\r
+    for t in txtls:\r
+        rM=rT.match(t)\r
+        if rM:\r
+            sizetxt=rM.group(1)\r
+            dartxt=rM.group(2)\r
+            tX=int(sizetxt.split("x")[0])\r
+            tY=int(sizetxt.split("x")[1])\r
+            tEX=int(dartxt.split(":")[0])\r
+            tEY=int(dartxt.split(":")[1])\r
+            if sizeMaxX<tX:\r
+                sizeMaxX=tX\r
+                if tX==1920 and tY==1080:\r
+                    parx=1\r
+                    pary=1\r
+                else:\r
+                    parx=tEX\r
+                    pary=tEY\r
+    return [parx,pary]\r
+def getMoviePAR2(pin):\r
+    ffmpeg=configreader.getConfPath("ffmpeg")\r
+    os.environ['LANG']="ja_JP.UTF-8"\r
+    exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"\r
+    try:\r
+        recdblist.addCommandSelfLog(pin, exe)\r
+    except Exception, inst:\r
+        print type(inst)\r
+        print str(inst)\r
+        print traceback.print_exc(file=sys.stdout)\r
+    txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
+    rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+PAR\D+(\d+:\d+)\D+.*\Z")\r
+    sizeMaxX=0\r
+    parx=0\r
+    pary=0\r
+    txtls=txts.split("\n")\r
+    for t in txtls:\r
+        rM=rT.match(t)\r
+        if rM:\r
+            sizetxt=rM.group(1)\r
+            dartxt=rM.group(2)\r
+            tX=int(sizetxt.split("x")[0])\r
+            tY=int(sizetxt.split("x")[1])\r
+            tEX=int(dartxt.split(":")[0])\r
+            tEY=int(dartxt.split(":")[1])\r
+            if sizeMaxX<tX:\r
+                sizeMaxX=tX\r
+                if tX==1920 and tY==1080:\r
+                    parx=1\r
+                    pary=1\r
+                else:\r
+                    parx=tEX\r
+                    pary=tEY\r
+    os.remove(pin+".size.ts")\r
+    return [parx,pary]\r
+def getMovieDAR(pin):\r
+    ffmpeg=configreader.getConfPath("ffmpeg")\r
+    os.environ['LANG']="ja_JP.UTF-8"\r
+    exe=ffmpeg+u" -i \""+pin+"\" 2>&1"\r
+    txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
+    rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")\r
+    sizeMaxX=0\r
+    darx=0\r
+    dary=0\r
+    txtls=txts.split("\n")\r
+    for t in txtls:\r
+        rM=rT.match(t)\r
+        if rM:\r
+            sizetxt=rM.group(1)\r
+            dartxt=rM.group(2)\r
+            tX=int(sizetxt.split("x")[0])\r
+            tY=int(sizetxt.split("x")[1])\r
+            tEX=int(dartxt.split(":")[0])\r
+            tEY=int(dartxt.split(":")[1])\r
+            if sizeMaxX<tX:\r
+                sizeMaxX=tX\r
+                if tX==1920 and tY==1080:\r
+                    darx=16\r
+                    dary=9\r
+                else:\r
+                    darx=tEX\r
+                    dary=tEY\r
+    return [darx,dary]\r
+def getMovieDAR2(pin):\r
+    ffmpeg=configreader.getConfPath("ffmpeg")\r
+    os.environ['LANG']="ja_JP.UTF-8"\r
+    exe=ffmpeg+u" -ss 5 -fs 1 -i \""+pin+"\" \""+pin+".size.ts\" 2>&1"\r
+    txts=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8','ignore')\r
+    rT=re.compile(u".*Stream.*#.*:.*\D+([\d]+x[\d]+)\D+.*DAR\D+(\d+:\d+)\D+.*\Z")\r
+    sizeMaxX=0\r
+    darx=0\r
+    dary=0\r
+    txtls=txts.split("\n")\r
+    for t in txtls:\r
+        rM=rT.match(t)\r
+        if rM:\r
+            sizetxt=rM.group(1)\r
+            dartxt=rM.group(2)\r
+            tX=int(sizetxt.split("x")[0])\r
+            tY=int(sizetxt.split("x")[1])\r
+            tEX=int(dartxt.split(":")[0])\r
+            tEY=int(dartxt.split(":")[1])\r
+            if sizeMaxX<tX:\r
+                sizeMaxX=tX\r
+                if tX==1920 and tY==1080:\r
+                    darx=16\r
+                    dary=9\r
+                else:\r
+                    darx=tEX\r
+                    dary=tEY\r
+    os.remove(pin+".size.ts")\r
+    return [darx,dary]\r
+def getParSize(pin,y):\r
+    tSize=getMovieBaseSize2(pin)\r
+    if tSize[1] != 0 :\r
+        tX=tSize[0]*10*y/tSize[1]\r
+        tY=y\r
+        if tX>int(tX/10)*10:\r
+            tX=tX/10+1\r
+        else:\r
+            tX=tX/10\r
+    else:\r
+        tX=-1\r
+        tY=-1\r
+    return [tX,tY]\r
+def getX264CoreVersion():\r
+    x264=configreader.getConfPath("x264")\r
+    #print x264\r
+    t1=commands.getoutput(x264+" --help|grep core")\r
+    #print t1\r
+    rT=re.compile(u"x264 core:(\d*)[\d]*.*\Z")\r
+    rM=rT.match(t1)\r
+    v=-1\r
+    if rM:\r
+        v=int(rM.group(1))\r
+    return v\r
+def getX264Commandline(preset,sar,fps,x264profile,x264tune,pout,pin,x264streamsize,crf=-1,bitrate=0,interlaced=0,bluray=0):\r
+    x264=configreader.getConfPath("x264")\r
+    os.environ['LANG']="ja_JP.UTF-8"\r
+    x264_sar="--sar "+sar\r
+    x264preset=u"--preset "+preset\r
+    x264fps="--fps "+fps\r
+    x264interlaced=""\r
+    x264bluray=""\r
+    x264_addline=configreader.getConfEnv("x264_addline")\r
+    if crf==-1:\r
+        x264bitrate=u"--bitrate "+str(bitrate)\r
+    else:\r
+        x264crf=u"--crf "+str(crf)\r
+    if interlaced==1:\r
+        x264interlaced="--tff --nal-hrd vbr"\r
+    if bluray==1:\r
+        if getX264CoreVersion()<115:\r
+            x264bluray=" --weightp 1 --nal-hrd vbr --bframes 3 --b-pyramid none --open-gop bluray --slices 4 --aud --colorprim bt709 --transfer bt709 "\r
+            x264_addline=""\r
+            if fps=="24000/1001 --keyint 24" or fps =="24000/1001":\r
+                if x264streamsize=="1280x720":\r
+                    x264bluray=u"--ref 6 "+x264bluray\r
+                if x264streamsize=="1920x1080":\r
+                    x264bluray=u"--ref 4 "+x264bluray\r
+            elif fps=="30000/1001 --keyint 30" or fps =="30000/1001":\r
+                if x264streamsize=="1280x720":\r
+                    x264bluray=u"--ref 6 --pulldown double "+x264bluray\r
+                if x264streamsize=="1920x1080":\r
+                    if interlaced==0:\r
+                        x264bluray=u"--ref 4 --fake-interlaced --pic-struct "+x264bluray\r
+                    else:\r
+                        x264bluray=u"--ref 4 "+x264bluray\r
+        elif getX264CoreVersion()>=115:\r
+            x264bluray=" --slices 4 --bluray-compat --nal-hrd vbr --bframes 3 --b-pyramid strict --aud --colorprim bt709 --transfer bt709 "\r
+            x264_addline=""\r
+            if fps=="24000/1001 --keyint 24" or fps =="24000/1001":\r
+                if x264streamsize=="1280x720":\r
+                    x264bluray=u"--ref 6 "+x264bluray\r
+                if x264streamsize=="1920x1080":\r
+                    x264bluray=u"--ref 4 "+x264bluray\r
+            elif fps=="30000/1001 --keyint 30" or fps =="30000/1001":\r
+                if x264streamsize=="1280x720":\r
+                    x264bluray=u"--ref 6 --pulldown double "+x264bluray\r
+                if x264streamsize=="1920x1080":\r
+                    if interlaced==0:\r
+                        x264bluray=u"--ref 4 --fake-interlaced --pic-struct "+x264bluray\r
+                    else:\r
+                        x264bluray=u"--ref 4 "+x264bluray\r
+    x264_thread="auto"\r
+    try:\r
+        xtt=configreader.getConfEnv("x264_thread")\r
+        xtt=int(xtt)\r
+        if xtt>0:\r
+            x264_thread=str(xtt)\r
+    except:\r
+        x264_thread="auto"\r
+    x264_addline=configreader.getConfEnv("x264_addline")\r
+    if getX264CoreVersion()>103:\r
+        x264res=u"--input-res "+x264streamsize\r
+        exe=u"nice -n 19 "+x264+" --demuxer raw --vbv-maxrate 30000 --vbv-bufsize 25000 "+x264_sar+" "+x264crf+u" "+x264bluray+u" "+x264interlaced+u" "+x264_addline+u" --colormatrix bt709 --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" "+x264res+" -o \""+pout+"\" "+pin\r
+    else:\r
+        exe=u"nice -n 19 "+x264+" --demuxer raw --vbv-maxrate 30000 --vbv-bufsize 25000 "+x264_sar+" "+x264crf+u" "+x264bluray+u" "+x264interlaced+u" "+x264_addline+u" --colormatrix bt709 --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" "+pin+" "+x264streamsize\r
+    return exe\r