#!/usr/bin/python
# coding: UTF-8
# Rec10 TS Recording Tools
# Copyright (C) 2009 Yukikaze
import commands
import os
import re
import time
import tv2avi
def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
    pout = pout.encode('utf-8')
    pin = pin.encode('utf-8')
    """
    pinで指定されたファイルをpoutにx264でエンコードして書き出す
    """
    isAnime = 0
    if re.search("a", opts):
        isAnime = 1
    dualpass = 0
    resize=0
    size="Tall"
    if re.search("2", opts):
        dualpass = 1
    if re.search("H", opts):
        size = "HD"
    if re.search("S", opts):
        size = "SD"
    if re.search("F", opts):
        size = "FHD"
    if re.search("L", opts):
        size = "Short"
    if re.search("G", opts):
        size = "LowHD"
    encode(pin, pout,size,isAnime,dualpass)
def encode(pin,pout,size,isAnime,is2pass):
    encvf=""
    txt=""
    if isAnime == 1:
        encvf = "-vf yadif=3,pp=l5,framestep=2,"
    else:
        encvf = "-vf yadif=3,pp=l5,framestep=2,"
    if size == "HD":
        encvf = encvf + "scale=1280:720,harddup"
        bitrate = "bitrate=" + tv2avi.Bitrate_HD
    elif size == "SD":
        encvf = encvf + "scale=720:480,harddup"
        bitrate = "bitrate=" + tv2avi.Bitrate_SD
    elif size == "FHD":
        encvf = encvf + "scale=1920:1080,harddup"
        bitrate = "bitrate=" + tv2avi.Bitrate_FHD
    elif size == "Short":
        encvf = encvf + "harddup"
        bitrate = "bitrate=" + tv2avi.Bitrate_Short
    elif size == "LowHD":
        encvf = encvf + "scale=1280:720,harddup"
        bitrate = "bitrate=" + tv2avi.Bitrate_LowHD
    else:
        encvf = encvf + "scale=1280:720,harddup"
        bitrate = "bitrate=" + tv2avi.Bitrate_HD
    if is2pass == 1:
        pas1exe = "mencoder \'" + pin + "\' -quiet -ovc x264 " + encvf + " -ofps 30000/1001 -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 + " -ofps 30000/1001 -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)
        print pas2exe
        time.sleep(5)
        txt=txt+commands.getoutput(pas2exe)
    else:
        pas1exe = "mencoder \'" + pin + "\' -quiet -ovc x264 " + encvf + " -ofps 30000/1001 -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:]
    else:
        stxt=txt
    s=stxt.find("\n")
    if s>0:
        s=s+len("\n")
        stxt=stxt[s:]
    txt="\n####mencoder-log_"+size+"####\n"+stxt
    f.write(txt)
    f.close()