OSDN Git Service

77ae4c4032695be003140d9c71112423d2fb38e8
[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-2010 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     dualpass = 0
16     isvfr=0
17     is24fps=0
18     size="Tall"
19     if re.search("2", opts):
20         dualpass = 1
21     if re.search("H", opts):
22         size = "HD"
23     if re.search("S", opts):
24         size = "SD"
25     if re.search("F", opts):
26         size = "FHD"
27     if re.search("L", opts):
28         size = "Short"
29     if re.search("G", opts):
30         size = "LowHD"
31     if re.search("v", opts):
32         isvfr=1
33     if re.search("a", opts):
34         is24fps=1
35     if re.search("d",opts):
36         encode_ffmpeg(pin, pout, size, dualpass)
37     else:
38         encode(pin, pout,size,isvfr,dualpass,is24fps)
39 def encode(pin,pout,size,isvfr,is2pass,is24fps):
40     encvf=""
41     txt=""
42     encvf="-vf yadif=0,pp=ac,pp=mb"
43     harddup=",pp=ac,hqdn3d=2:1:2,harddup"
44     ofps="-ofps 30000/1001"
45     fps="-fps 30000/1001"
46     bm=1
47     scale=""
48     x264enc1=":me=hex:bframes=3"
49     x264enc2=":direct=auto:me=umh:bframes=3"
50     crf=""
51     if is24fps==1:
52         ofps="-ofps 24000/1001"
53         fps="-fps 30000/1001"
54         encvf="-vf pullup,softskip"
55         harddup=",pp=ac,pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
56         bm=1
57         x264enc1=":me=hex:bframes=3"
58         x264enc2=":frameref=6:subme=8:subq=7:trellis=2:aq-strength=0.6:deblock=1,1:psy-rd=0.4:partitions=all:8x8dct:direct_pred=auto:me=umh:bframes=3"
59     if isvfr==1:
60         ofps="-ofps 60000/1001"
61         fps="-fps 60000/1001"
62         encvf="-vf tfields=1,hqdn3d=2:1:2"
63         #encvf="-vf yadif=1,pp=ac,pp=mb"
64         harddup=",pp=ac,pp=l5,unsharp=l3x3:0.75:c3x3:0.75,harddup"
65         #encvf="-vf pullup,softskip,pp=ac"
66         #encvf="-vf filmdint=io=2997:119880,pp=md"
67         bm=1
68         x264enc1=":me=hex:bframes=3:cqm=jvt"
69         x264enc2=":frameref=3:subme=8:subq=7:8x8dct:direct_pred=auto:me=umh:bframes=3:weight_b:cqm=jvt"
70     if size == "HD":
71         encvf = encvf + ",scale=1280:720::0:4"+harddup
72         bitrate = "bitrate=" + str(tv2avi.Bitrate_HD*bm)
73         crf="crf=20"
74     elif size == "SD":
75         encvf = encvf + ",scale=720:480::0:4"+harddup
76         bitrate = "bitrate=" + str(tv2avi.Bitrate_SD*bm)
77         crf="crf=20"
78     elif size == "FHD":
79         encvf = encvf + ",scale=1920:1080::0:4"+harddup
80         bitrate = "bitrate=" + str(tv2avi.Bitrate_FHD*bm)
81         crf="crf=22"
82     elif size == "Short":
83         encvf = encvf +harddup
84         bitrate = "bitrate=" + str(tv2avi.Bitrate_Short*bm)
85         crf="crf=20"
86     elif size == "LowHD":
87         encvf = encvf + ",scale=1280:720::0:4"+harddup
88         bitrate = "bitrate=" + str(tv2avi.Bitrate_LowHD*bm)
89         crf="crf=22"
90     else:
91         encvf = encvf + ",scale=1280:720::0:4"+harddup
92         bitrate = "bitrate=" + str(tv2avi.Bitrate_HD*bm)
93         crf="crf=22"
94     if is2pass == 1:
95         pas1exe = "mencoder \'" + pin + "\' -vfm ffmpeg -quiet -nosound -sws 9 -ovc x264 " + encvf +" "+fps+" "+ofps+" -x264encopts " + bitrate +x264enc1+ ":threads=auto:level_idc=42:pass=1:turbo=2 -passlogfile \'" + pin + ".log\' " + " -o /dev/null"
96         pas2exe = "mencoder \'" + pin + "\' -vfm ffmpeg -quiet -sws 9 -ovc x264 " + encvf +" "+fps+ " "+ofps+" -x264encopts " + bitrate +x264enc2+ ":level_idc=42:threads=auto:pass=2 -passlogfile \'" + pin + ".log\' -oac faac -o \'" + pout + "\'"
97         pas1exe = "nice -n 19 " + pas1exe
98         pas2exe = "nice -n 19 " + pas2exe
99         recdblist.printutf8(pas1exe)
100         txt=txt+unicode(commands.getoutput(pas1exe.encode('utf-8')),'utf-8')
101         recdblist.printutf8(pas2exe)
102         time.sleep(5)
103         txt=txt+unicode(commands.getoutput(pas2exe.encode('utf-8')),'utf-8')
104     else:
105         pas1exe = "mencoder \'" + pin + "\' -vfm ffmpeg -quiet -sws 9 -ovc x264 " + encvf+" "+fps + " "+ofps+" -x264encopts " + bitrate +x264enc2+":level_idc=42:threads=auto -oac faac -o \'" + pout + "\'"
106         pas1exe = "nice -n 19 " + pas1exe
107         recdblist.printutf8(pas1exe)
108         txt=txt+unicode(commands.getoutput(pas1exe.encode('utf-8')),'utf-8')
109     logo=pin
110     logo=logo.replace(".sa.avi",".ts")
111     logo=logo.replace(".ts",".log")
112     f=open(logo,'a')
113     s=len(txt)
114     stxt=""
115     if s>100:
116         stxt=txt[s-300:]
117     else:
118         stxt=txt
119     s=stxt.find("\n")
120     if s>0:
121         s=s+len("\n")
122         stxt=stxt[s:]
123     txt="\n####mencoder-log_"+size+"####\n"+stxt
124     f.write(txt.encode('utf-8'))
125     f.close()
126
127 def encode_ffmpeg(pin,pout,size,is2pass):
128     #ffmpeg -i input.m2v -vsync 400 -vcodec h264 -b bitrate k -s sizexsize -deinterlace -
129     #x264enc1=":me=hex:bframes=3"
130     #:frameref=3:subq=7:partitions=all:8x8dct:direct=auto:me=umh:bframes=3:weight_b
131     #directpred 3
132     ffmpeg="ffmpeg"
133     vf1="-me hex -bframes 3 "
134     vf1="-ref 1 -subme 1 -me dia "
135     #vf2="-ref 3 -subq 7 -partitions all -direct auto -me umh -level 42 -profile high -bframes 3 -flags2 8x8dct+bpyramid+wpred+mixed_refs+brdo "
136     #vf2="-vpre hq -refs 3 -subq 7 -partitions all -level 42 -me_method umh -bf 3 -flags2 dct8x8+bpyramid+wpred+mixed_refs "
137     vf2="-vpre hq -level 42 -me_method umh -bf 3 "
138     if size == "HD":
139         s = "-s 1280x720 "
140         bitrate = str(tv2avi.Bitrate_HD)
141         crf="crf=20"
142     elif size == "SD":
143         s ="-s 720x480 "
144         bitrate = str(tv2avi.Bitrate_SD)
145         crf="crf=20"
146     elif size == "FHD":
147         s ="-s 1920x1080 "
148         bitrate = str(tv2avi.Bitrate_FHD)
149         crf="crf=22"
150     elif size == "Short":
151         s= ""
152         bitrate = str(tv2avi.Bitrate_Short)
153         crf="crf=20"
154     elif size == "LowHD":
155         s = "-s 1280x720 "
156         bitrate = str(tv2avi.Bitrate_LowHD)
157         crf="crf=22"
158     else:
159         s = "-s 1280x720 "
160         bitrate = str(tv2avi.Bitrate_LowHD)
161         crf="crf=22"
162     txt=""
163     if is2pass==1:
164         exe1=ffmpeg+" -y -i \""+pin+"\" -pass 1 -vsync 400 -vcodec libx264 -b "+bitrate+"k "+vf1+s+"-deinterlace -an -passlogfile \'" + pin + ".log\' " + " /dev/null"
165         exe2=ffmpeg+" -y -i \""+pin+"\" -pass 2 -vsync 400 -vcodec libx264 -b "+bitrate+"k "+vf2+s+"-deinterlace -an -passlogfile \'" + pin + ".log\' \"" +pout+"\"" #aviにすべきかどうか
166         txt=txt+exe1+"\n"+exe2+"\m"
167         recdblist.printutf8(exe1)
168         os.system(exe1)
169         os.system(exe2)
170     else:
171         exe=ffmpeg+" -y -i \""+pin+"\" -vsync 400 -vcodec libx264 -b "+bitrate+"k "+vf2+s+"-deinterlace -an \"" +pout+"\""
172         txt=txt+exe+"\n"
173         os.system(exe)
174     logo=pin
175     logo=logo.replace(".m2v",".ts")
176     logo=logo.replace(".ts",".log")
177     f=open(logo,'a')
178     s=len(txt)
179     stxt=""
180     if s>100:
181         stxt=txt[s-300:]
182     else:
183         stxt=txt
184     s=stxt.find("\n")
185     if s>0:
186         s=s+len("\n")
187         stxt=stxt[s:]
188     txt="\n####dualaudio ffmpeg-log_"+size+"####\n"+stxt
189     f.write(txt.encode('utf-8'))
190     f.close()