OSDN Git Service

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