OSDN Git Service

eab7e9ba713cb089627516c15db514ef84b666bd
[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     dualpass = 0
15     isvfr=0
16     size="Tall"
17     if re.search("2", opts):
18         dualpass = 1
19     if re.search("H", opts):
20         size = "HD"
21     if re.search("S", opts):
22         size = "SD"
23     if re.search("F", opts):
24         size = "FHD"
25     if re.search("L", opts):
26         size = "Short"
27     if re.search("G", opts):
28         size = "LowHD"
29     if re.search("v", opts):
30         isvfr=1
31     encode(pin, pout,size,isvfr,dualpass)
32 def encode(pin,pout,size,isvfr,is2pass):
33     encvf=""
34     txt=""
35     encvf="-vf yadif=0,pp=ac"
36     harddup=",harddup"
37     ofps="-ofps 30000/1001"
38     bm=1
39     scale=""
40     x264enc1=""
41     x264enc2=""
42     crf=""
43     if isvfr==1:
44         ofps="-ofps 120000/1001"
45         encvf="-vf pullup,softskip,pp=ac"
46         #encvf="-vf filmdint=io=2997:119880,pp=mb"
47         bm=4
48         x264enc1=":me=hex:bframes=3"
49         x264enc2=":frameref=3:subq=7:partitions=all:8x8dct:direct=auto:me=umh:bframes=3:weight_b"
50         harddup=""
51     if size == "HD":
52         encvf = encvf + ",scale=1280:720::0:4"+harddup
53         bitrate = "bitrate=" + str(tv2avi.Bitrate_HD*bm)
54         crf="crf=20"
55     elif size == "SD":
56         encvf = encvf + ",scale=720:480::0:4"+harddup
57         bitrate = "bitrate=" + str(tv2avi.Bitrate_SD*bm)
58         crf="crf=20"
59     elif size == "FHD":
60         encvf = encvf + ",scale=1920:1080::0:4"+harddup
61         bitrate = "bitrate=" + str(tv2avi.Bitrate_FHD*bm)
62         crf="crf=22"
63     elif size == "Short":
64         encvf = encvf +harddup
65         bitrate = "bitrate=" + str(tv2avi.Bitrate_Short*bm)
66         crf="crf=20"
67     elif size == "LowHD":
68         encvf = encvf + ",scale=1280:720::0:4"+harddup
69         bitrate = "bitrate=" + str(tv2avi.Bitrate_LowHD*bm)
70         crf="crf=22"
71     else:
72         encvf = encvf + ",scale=1280:720::0:4"+harddup
73         bitrate = "bitrate=" + str(tv2avi.Bitrate_HD*bm)
74         crf="crf=22"
75     if is2pass == 1:
76         pas1exe = "mencoder \'" + pin + "\' -quiet -nosound -sws 9 -ovc x264 " + encvf + " "+ofps+" -x264encopts " + bitrate +x264enc1+ ":threads=auto:level_idc=41:pass=1:turbo=2 -passlogfile \'" + pin + ".log\' " + " -o /dev/null"
77         pas2exe = "mencoder \'" + pin + "\' -quiet -sws 9 -ovc x264 " + encvf + " "+ofps+" -x264encopts " + bitrate +x264enc2+ ":level_idc=42:threads=auto:pass=2 -passlogfile \'" + pin + ".log\' -oac faac -o \'" + pout + "\'"
78         pas1exe = "nice -n 19 " + pas1exe
79         pas2exe = "nice -n 19 " + pas2exe
80         recdblist.printutf8(pas1exe)
81         txt=txt+unicode(commands.getoutput(pas1exe.encode('utf-8')),'utf-8')
82         recdblist.printutf8(pas2exe)
83         time.sleep(5)
84         txt=txt+unicode(commands.getoutput(pas2exe.encode('utf-8')),'utf-8')
85     else:
86         pas1exe = "mencoder \'" + pin + "\' -quiet -sws 9 -ovc x264 " + encvf + " "+ofps+" -x264encopts " + bitrate +x264enc2+":level_idc=42:threads=auto -oac faac -o \'" + pout + "\'"
87         pas1exe = "nice -n 19 " + pas1exe
88         recdblist.printutf8(pas1exe)
89         txt=txt+unicode(commands.getoutput(pas1exe.encode('utf-8')),'utf-8')
90     logo=pin
91     logo=logo.replace(".sa.avi",".ts")
92     logo=logo.replace(".ts",".log")
93     f=open(logo,'a')
94     s=len(txt)
95     stxt=""
96     if s>100:
97         stxt=txt[s-300:]
98     else:
99         stxt=txt
100     s=stxt.find("\n")
101     if s>0:
102         s=s+len("\n")
103         stxt=stxt[s:]
104     txt="\n####mencoder-log_"+size+"####\n"+stxt
105     f.write(txt.encode('utf-8'))
106     f.close()