OSDN Git Service

implement single audio using bontsdemux option(b).
[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 configreader
7 import os
8 import os.path
9 import re
10 import random
11
12 import recdblist
13 def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
14     """
15     pinで指定されたファイルをpoutにx264でエンコードして書き出す
16     """
17     dualpass = 0
18     is24fps=0
19     size="HD"
20     crf=18
21     quality=4
22     quality=int(configreader.getenv("x264_preset"))
23     crf=int(configreader.getenv("crf"))
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 = "FullHD"
30     if re.search("W",opts):
31         size = "WVGA"
32     if re.search("MW8", opts):
33         size = "QVGA_BASE"
34         crf=crf+4
35     if re.search("MW9", opts):
36         size = "WVGA_BASE"
37         crf=crf+2
38     if re.search("v", opts):
39         is24fps=1
40         crf=int(configreader.getenv("animation_crf"))
41     if re.search("a", opts):
42         is24fps=1
43         crf=int(configreader.getenv("animation_crf"))
44     if re.search("q",opts):
45         quality=quality-2
46     if re.search("w",opts):
47         quality=quality-1
48     if re.search("e",opts):
49         quality=quality+1
50     if re.search("r",opts):
51         quality=quality+2
52     if re.search("u",opts):
53         crf=crf+2
54     if re.search("i",opts):
55         crf=crf+1
56     if re.search("o",opts):
57         crf=crf-1
58     if re.search("p",opts):
59         crf=crf-2
60     if re.search("d",opts):
61         encode_ffmpeg(pin,pout,size,is24fps,quality,crf)
62     elif re.search("5",opts):
63         encode_ffmpeg(pin,pout,size,is24fps,quality,crf)
64     elif re.search("b",opts):
65         try:
66             tm2v=pin.replace(".ts",".m2v")
67             encode(tm2v, pout,size,is24fps,quality,crf)
68         except Exception, inst:
69             recdblist.printutf8("error occures in ts2x264.py encode")
70             recdblist.printutf8(str(type(inst)))
71             recdblist.printutf8(str(inst))
72     else:
73         try:
74             encode(pin, pout,size,is24fps,quality,crf)
75         except Exception, inst:
76             recdblist.printutf8("error occures in ts2x264.py encode")
77             recdblist.printutf8(str(type(inst)))
78             recdblist.printutf8(str(inst))
79 def encode(pin,pout,size,is24fps,quality,crf):
80     mencoder=configreader.getpath("mencoder")
81     x264=configreader.getpath("x264")
82     encvf=""
83     txt=""
84     encvf="-vf yadif=0,pp=l5"
85     harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
86     ofps="-ofps 30000/1001"
87     fps="-fps 30000/1001"
88     x264fps="--fps 30000/1001"
89     x264streamsize=""
90     x264preset=""
91     x264tune=""
92     x264_bitrate="5000"
93     x264_thread="auto"
94     try:
95         xtt=configreader.getenv("x264_thread")
96         xtt=int(xtt)
97         if xtt>0:
98             x264_thread=str(xtt)
99     except:
100         x264_thread="auto"
101     x264_addline=configreader.getenv("x264_addline")
102     if is24fps==1:
103         ofps="-ofps 24000/1001"
104         fps="-fps 30000/1001"
105         x264fps="--fps 24000/1001"
106         x264tune="--tune animation"
107         encvf="-vf pullup,softskip"
108         harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
109     if size == "HD":
110         encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
111         x264streamsize=u"1280x720"
112         x264_bitrate="2500"
113     elif size == "WVGA":
114         encvf = encvf + ",scale=-2:480::0:3,expand=854:480"+harddup
115         x264streamsize=u"854x480"
116         x264_bitrate="1500"
117     elif size == "FullHD":
118         encvf = encvf + ",scale=-2:1080::0:3,expand=1920:1080"+harddup
119         x264streamsize=u"1920x1080"
120         x264_bitrate="5000"
121     elif size == "SD":
122         encvf = encvf + ",scale=720:-2::0:3,expand=720:480"+harddup
123         x264streamsize=u"720x480"
124         x264_bitrate="1250"
125     elif size == "QVGA_BASE":
126         encvf = encvf + ",scale=320:-2::0:3,expand=320:240"+harddup
127         x264streamsize=u"320x240"
128         x264_bitrate="300"
129     elif size == "WVGA_BASE":
130         encvf = encvf + ",scale=-2:480::0:3,expand=854:480"+harddup
131         x264streamsize=u"854x480"
132         x264_bitrate="1500"
133     else:
134         encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
135         x264streamsize=u"1280x720"
136         x264_bitrate="2500"
137     if quality==1:
138         x264preset=u"--preset ultrafast"
139     elif quality==2:
140         x264preset=u"--preset veryfast"
141     elif quality==3:
142         x264preset=u"--preset fast"
143     elif quality==4:
144         x264preset=u"--preset medium"
145     elif quality==5:
146         x264preset=u"--preset slow"
147     elif quality==6:
148         x264preset=u"--preset slower"
149     if size == "WVGA_BASE" or size == "QVGA_BASE":
150         x264profile=" --level 32 --profile baseline "
151     else:
152         x264profile=" --level 41 --profile high "
153     x264crf=u"--crf "+str(crf)
154     os.environ['LANG']="ja_JP.UTF-8"
155     random.seed(pin)
156     random.jumpahead(10)
157     streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 999999)))
158     os.system(u"mkfifo "+streampath)
159     encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet -sws 9 "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" &"
160     encexe=encexe+u" nice -n 19 "+x264+" "+x264crf+u" "+x264_addline+u"  --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" "+streampath+" "+x264streamsize
161     encexe=u"nice -n 19 " +encexe
162     recdblist.printutf8(encexe)
163     txt=txt+"Cmd : "+encexe+"\n"
164     txtt=""
165     try:
166         txtt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
167     except:
168         ""
169     os.system("rm "+streampath)
170     txt=txt+txtt
171     recdblist.addlog( pin, txt, "mencoder-log")
172
173 def encode_ffmpeg(pin,pout,size,is24fps,quality,crf):
174     """
175     
176     """
177     ffmpeg=configreader.getpath("ffmpeg")
178     x264=configreader.getpath("x264")
179     fps=u"-r 29.970030 "
180     x264fps="--fps 30000/1001"
181     x264streamsize=""
182     x264preset=""
183     x264tune=""
184     x264_bitrate="2500"
185     x264_thread="auto"
186     try:
187         xtt=configreader.getenv("x264_thread")
188         xtt=int(xtt)
189         if xtt>0:
190             x264_thread=str(xtt)
191     except:
192         x264_thread="auto"
193     x264_addline=configreader.getenv("x264_addline")
194     if size == "HD":
195         s = "-s 1280x720 "
196         x264streamsize=u"1280x720"
197         x264_bitrate="2500"
198     elif size == "WVGA":
199         s = "-s 854x480 "
200         x264streamsize=u"854x480"
201         x264_bitrate="1500"
202     elif size == "FullHD":
203         s = "-s 1920x1080 "
204         x264streamsize=u"1920x1080"
205         x264_bitrate="5000"
206     elif size == "SD":
207         s = "-s 720x480 "
208         x264streamsize=u"720x480"
209         x264_bitrate="1250"
210     elif size == "QVGA_BASE":
211         s = "-s 320x240 "
212         x264streamsize=u"320x240"
213         x264_bitrate="300"
214     elif size == "WVGA_BASE":
215         s = "-s 854x480 "
216         x264streamsize=u"854x480"
217         x264_bitrate="1500"
218     else:
219         s = "-s 1280x720 "
220         x264streamsize=u"1280x720"
221         x264_bitrate="2500"
222     if quality==1:
223         x264preset=u"--preset ultrafast"
224     elif quality==2:
225         x264preset=u"--preset veryfast"
226     elif quality==3:
227         x264preset=u"--preset fast"
228     elif quality==4:
229         x264preset=u"--preset medium"
230     elif quality==5:
231         x264preset=u"--preset slow"
232     elif quality==6:
233         x264preset=u"--preset slower"
234     if size == "WVGA_BASE" or size == "QVGA_BASE":
235         x264profile=" --level 32 --profile baseline "
236     else:
237         x264profile=" --level 41 --profile high "
238     x264crf=u"--crf "+str(crf)
239     txt=""
240     os.environ['LANG']="ja_JP.UTF-8"
241     exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - |"
242     exe=exe+u" nice -n 19 "+x264+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
243     txt=txt+"Cmd : "+exe+"\n"
244     exe = "nice -n 19 " + exe
245     txt=txt+"Cmd : "+exe+"\n"
246     txtt=""
247     recdblist.printutf8(exe)
248     try:
249         txtt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
250     except:
251         ""
252     txt=txt+txtt
253     recdblist.addlog( pin, txt, "dual audio ffmpeg-log")
254