OSDN Git Service

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