OSDN Git Service

fix vfr.
[rec10/rec10-git.git] / rec10 / trunk / src / ts2x264.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
5 import commands
6 import re
7 import time
8 import tv2avi
9 import recdblist
10 def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
11     """
12     pinで指定されたファイルをpoutにx264でエンコードして書き出す
13     """
14     isAnime = 0
15     if re.search("a", opts):
16         isAnime = 1
17     dualpass = 0
18     resize=0
19     isvfr=0
20     size="Tall"
21     if re.search("2", opts):
22         dualpass = 1
23     if re.search("H", opts):
24         size = "HD"
25     if re.search("S", opts):
26         size = "SD"
27     if re.search("F", opts):
28         size = "FHD"
29     if re.search("L", opts):
30         size = "Short"
31     if re.search("G", opts):
32         size = "LowHD"
33     if re.search("v", opts):
34         isvfr=1
35     encode(pin, pout,size,isvfr,dualpass)
36 def encode(pin,pout,size,isvfr,is2pass):
37     encvf=""
38     txt=""
39     encvf="-vf yadif=0,pp=mb"
40     harddup=",harddup"
41     ofps="-ofps 30000/1001"
42     bm=1
43     scale=""
44     x264enc1=""
45     x264enc2=""
46     crf=""
47     if isvfr==1:
48         ofps="-ofps 120000/1001"
49         encvf="-vf pullup,softskip,pp=mb"
50         bm=4
51         x264enc1=":me=hex:bframes=3"
52         x264enc2=":frameref=3:subq=7:partitions=all:8x8dct:direct=auto:me=umh:bframes=3:weight_b"
53         harddup=""
54     if size == "HD":
55         encvf = encvf + ",scale=1280:720::0:4"+harddup
56         bitrate = "bitrate=" + str(tv2avi.Bitrate_HD*bm)
57         crf="crf=20"
58     elif size == "SD":
59         encvf = encvf + ",scale=720:480::0:4"+harddup
60         bitrate = "bitrate=" + str(tv2avi.Bitrate_SD*bm)
61         crf="crf=20"
62     elif size == "FHD":
63         encvf = encvf + ",scale=1920:1080::0:4"+harddup
64         bitrate = "bitrate=" + str(tv2avi.Bitrate_FHD*bm)
65         crf="crf=22"
66     elif size == "Short":
67         encvf = encvf +harddup
68         bitrate = "bitrate=" + str(tv2avi.Bitrate_Short*bm)
69         crf="crf=20"
70     elif size == "LowHD":
71         encvf = encvf + ",scale=1280:720::0:4"+harddup
72         bitrate = "bitrate=" + str(tv2avi.Bitrate_LowHD*bm)
73         crf="crf=22"
74     else:
75         encvf = encvf + ",scale=1280:720::0:4"+harddup
76         bitrate = "bitrate=" + str(tv2avi.Bitrate_HD*bm)
77         crf="crf=22"
78     if is2pass == 1:
79         pas1exe = "mencoder \'" + pin + "\' -noskip -quiet -nosound -sws 9 -ovc x264 " + encvf + " "+ofps+" -x264encopts " + bitrate +x264enc1+ ":threads=auto:pass=1:turbo=2 -passlogfile \'" + pin + ".log\' " + " -o /dev/null"
80         pas2exe = "mencoder \'" + pin + "\' -noskip -noodml -quiet -nosound -sws 9 -ovc x264 " + encvf + " "+ofps+" -x264encopts " + bitrate +x264enc2+ ":threads=auto:pass=2 -passlogfile \'" + pin + ".log\' " + " -o \'" + pout + "\'"
81         pas1exe = "nice -n 19 " + pas1exe
82         pas2exe = "nice -n 19 " + pas2exe
83         recdblist.printutf8(pas1exe)
84         txt=txt+unicode(commands.getoutput(pas1exe.encode('utf-8')),'utf-8')
85         recdblist.printutf8(pas2exe)
86         time.sleep(5)
87         txt=txt+unicode(commands.getoutput(pas2exe.encode('utf-8')),'utf-8')
88     else:
89         pas1exe = "mencoder \'" + pin + "\' -noskip -noodml -nosound -quiet -sws 9 -ovc x264 " + encvf + " "+ofps+" -x264encopts " + bitrate +x264enc2+":threads=auto -o \'" + pout + "\'"
90         pas1exe = "nice -n 19 " + pas1exe
91         recdblist.printutf8(pas1exe)
92         txt=txt+unicode(commands.getoutput(pas1exe.encode('utf-8')),'utf-8')
93     logo=pin
94     logo=logo.replace(".sa.avi",".ts")
95     logo=logo.replace(".ts",".log")
96     f=open(logo,'a')
97     s=len(txt)
98     stxt=""
99     if s>100:
100         stxt=txt[s-300:]
101     else:
102         stxt=txt
103     s=stxt.find("\n")
104     if s>0:
105         s=s+len("\n")
106         stxt=stxt[s:]
107     txt="\n####mencoder-log_"+size+"####\n"+stxt
108     f.write(txt.encode('utf-8'))
109     f.close()