OSDN Git Service

change 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 base64
11 import recdblist
12 def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
13     """
14     pinで指定されたファイルをpoutにx264でエンコードして書き出す
15     """
16     dualpass = 0
17     is24fps=0
18     size="HD"
19     crf=18
20     quality=4
21     quality=int(configreader.getenv("x264_preset"))
22     crf=int(configreader.getenv("crf"))
23     if re.search("2", opts):
24         dualpass = 1
25     if re.search("H", opts):
26         size = "HD"
27     if re.search("S", opts):
28         size = "SD"
29     if re.search("F", opts):
30         size = "FullHD"
31     if re.search("W",opts):
32         size = "WVGA"
33     if re.search("v", opts):
34         is24fps=1
35         crf=int(configreader.getenv("animation_crf"))
36     if re.search("a", opts):
37         is24fps=1
38         crf=int(configreader.getenv("animation_crf"))
39     if re.search("q",opts):
40         quality=quality-2
41     if re.search("w",opts):
42         quality=quality-1
43     if re.search("e",opts):
44         quality=quality+1
45     if re.search("r",opts):
46         quality=quality+2
47     if re.search("u",opts):
48         crf=crf+2
49     if re.search("i",opts):
50         crf=crf+1
51     if re.search("o",opts):
52         crf=crf-1
53     if re.search("p",opts):
54         crf=crf-2
55     if re.search("d",opts):
56         encode_ffmpeg(pin,pout,size,is24fps,quality,crf)
57     elif re.search("5",opts):
58         encode_ffmpeg(pin,pout,size,is24fps,quality,crf)
59     else:
60         encode(pin, pout,size,dualpass,is24fps,quality)
61 def encode(pin,pout,size,is24fps,quality,crf):
62     encvf=""
63     txt=""
64     encvf="-vf yadif=0,pp=l5"
65     harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
66     ofps="-ofps 30000/1001"
67     fps="-fps 30000/1001"
68     x264fps="--fps 30000/1001"
69     x264streamsize=""
70     x264preset=""
71     x264tune=""
72     if size == "HD":
73         encvf = encvf + ",scale=1280:720::0:3"+harddup
74         x264streamsize=u"1280x720"
75     elif size == "WVGA":
76         encvf = encvf + ",scale=854:480::0:3"+harddup
77         x264streamsize=u"854x480"
78     elif size == "FullHD":
79         encvf = encvf + ",scale=1920:1080::0:3"+harddup
80         x264streamsize=u"1920x1080"
81     elif size == "SD":
82         encvf = encvf + ",scale=720:480::0:3"+harddup
83         x264streamsize=u"720x480"
84     else:
85         encvf = encvf + ",scale=1280:720::0:3"+harddup
86         x264streamsize=u"1280x720"
87     if quality==1:
88         x264preset=u"--perset ultrafast"
89     elif quality==2:
90         x264preset=u"--perset veryfast"
91     elif quality==3:
92         x264preset=u"--perset fast"
93     elif quality==4:
94         x264preset=u"--perset medium"
95     elif quality==5:
96         x264preset=u"--perset slow"
97     elif quality==6:
98         x264preset=u"--perset slower"
99     x264crf=u"--crf "+str(crf)
100     if is24fps==1:
101         ofps="-ofps 24000/1001"
102         fps="-fps 30000/1001"
103         x264fps="--fps 24000/1001"
104         encvf="-vf pullup,softskip"
105         harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
106     os.environ['LANG']="ja_JP.UTF-8"
107     streampath=os.path.join(os.path.dirname(pin),base64.b32encode(os.path.split(pin)[1])[0:10])
108     os.system(u"mkfifo "+streampath)
109     encexe=u"mencoder \""+pin+u"\" -vfm ffmpeg -quiet -sws 9 "+encvf+u" "+fps+" "+ofps+" -ovc raw -of rawvideo -o \""+streampath+"\" &"
110     encexe=encexe+u" x264 "+x264crf+" --level 41 --profile high"+x264preset+" "+x264tune+" "+x264fps+" "+x264crf+" -o \""+pout+"\" "+streampath+" "+x264streamsize
111     encexe=u"nice -n 19 " +encexe
112     recdblist.printutf8(encexe)
113     txt=txt+"Cmd : "+encexe+"\n"
114     txtt=""
115     try:
116         txtt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
117     except:
118         ""
119     os.system("rm "+streampath)
120     txt=txt+txtt
121     recdblist.addlog( pin, txt, "mencoder-log")
122
123 def encode_ffmpeg(pin,pout,size,is24fps,quality,crf):
124     """
125     
126     """
127     ffmpeg="ffmpeg"
128     fps=u"-r 29.970030 "
129     x264fps="--fps 30000/1001"
130     x264streamsize=""
131     x264preset=""
132     x264tune=""
133     if size == "HD":
134         s = "-s 1280x720 "
135         x264streamsize=u"1280x720"
136     elif size == "WVGA":
137         s = "-s 854x480 "
138         x264streamsize=u"854x480"
139     elif size == "FullHD":
140         s = "-s 1920x1080 "
141         x264streamsize=u"1920x1080"
142     elif size == "SD":
143         s = "-s 720x480 "
144         x264streamsize=u"720x480"
145     else:
146         s = "-s 1280x720 "
147         x264streamsize=u"1280x720"
148     if quality==1:
149         x264preset=u"--perset ultrafast"
150     elif quality==2:
151         x264preset=u"--perset veryfast"
152     elif quality==3:
153         x264preset=u"--perset fast"
154     elif quality==4:
155         x264preset=u"--perset medium"
156     elif quality==5:
157         x264preset=u"--perset slow"
158     elif quality==6:
159         x264preset=u"--perset slower"
160     x264crf=u"--crf "+str(crf)
161     txt=""
162     os.environ['LANG']="ja_JP.UTF-8"
163     exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+r+"-deinterlace -an -f rawvideo - |"
164     exe=exe+u" x264 "+x264crf+" --level 41 --profile high"+x264preset+" "+x264tune+" "+x264fps+" "+x264crf+" -o \""+pout+"\" - "+x264streamsize
165     txt=txt+"Cmd : "+exe+"\n"
166     exe = "nice -n 19 " + exe
167     txt=txt+"Cmd : "+exe+"\n"
168     txtt=""
169     try:
170         txtt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
171     except:
172         ""
173     txt=txt+txtt
174     recdblist.addlog( pin, txt, "dual audio ffmpeg-log")
175