OSDN Git Service

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