3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
13 def ts2x264(pin, pout, opts):#sizeは"HD"か"SD"
15 pinで指定されたファイルをpoutにx264でエンコードして書き出す
22 quality=int(configreader.getenv("x264_preset"))
23 crf=int(configreader.getenv("crf"))
24 if re.search("H", opts):
26 if re.search("S", opts):
28 if re.search("F", opts):
30 if re.search("W",opts):
32 if re.search("MW8", opts):
35 if re.search("MW9", opts):
38 if re.search("v", opts):
40 crf=int(configreader.getenv("animation_crf"))
41 if re.search("a", opts):
43 crf=int(configreader.getenv("animation_crf"))
44 if re.search("q",opts):
46 if re.search("w",opts):
48 if re.search("e",opts):
50 if re.search("r",opts):
52 if re.search("u",opts):
54 if re.search("i",opts):
56 if re.search("o",opts):
58 if re.search("p",opts):
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):
66 tm2v=pin.replace(".ts",".m2v")
67 encode(tm2v, pout,size,is24fps,quality,crf)
68 except Exception, inst:
69 recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst))
72 encode(pin, pout,size,is24fps,quality,crf)
73 except Exception, inst:
74 recdblist.Commonlogex("Error", "ts2x264(ts2x264.py)", str(type(inst)), str(inst))
75 def encode(pin,pout,size,is24fps,quality,crf):
76 mencoder=configreader.getpath("mencoder")
77 x264=configreader.getpath("x264")
80 encvf="-vf yadif=0,pp=l5"
81 harddup=",hqdn3d=2:1:2,unsharp=l3x3:0.75:c3x3:0.75,harddup"
82 ofps="-ofps 30000/1001"
84 x264fps="--fps 30000/1001"
91 xtt=configreader.getenv("x264_thread")
97 x264_addline=configreader.getenv("x264_addline")
99 ofps="-ofps 24000/1001"
100 fps="-fps 30000/1001"
101 x264fps="--fps 24000/1001"
102 x264tune="--tune animation"
103 encvf="-vf pullup,softskip"
104 harddup=",pp=l5,unsharp=l3x3:0.75:c3x3:0.75,hqdn3d=2:1:2,harddup"
106 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
107 x264streamsize=u"1280x720"
110 encvf = encvf + ",scale=-2:480::0:3,expand=854:480"+harddup
111 x264streamsize=u"854x480"
113 elif size == "FullHD":
114 encvf = encvf + ",scale=-2:1080::0:3,expand=1920:1080"+harddup
115 x264streamsize=u"1920x1080"
118 encvf = encvf + ",scale=720:-2::0:3,expand=720:480"+harddup
119 x264streamsize=u"720x480"
121 elif size == "QVGA_BASE":
122 encvf = encvf + ",scale=320:-2::0:3,expand=320:240"+harddup
123 x264streamsize=u"320x240"
125 elif size == "WVGA_BASE":
126 encvf = encvf + ",scale=-2:480::0:3,expand=854:480"+harddup
127 x264streamsize=u"854x480"
130 encvf = encvf + ",scale=-2:720::0:3,expand=1280:720"+harddup
131 x264streamsize=u"1280x720"
134 x264preset=u"--preset ultrafast"
136 x264preset=u"--preset veryfast"
138 x264preset=u"--preset fast"
140 x264preset=u"--preset medium"
142 x264preset=u"--preset slow"
144 x264preset=u"--preset slower"
145 if size == "WVGA_BASE" or size == "QVGA_BASE":
146 x264profile=" --level 32 --profile baseline "
148 x264profile=" --level 41 --profile high "
149 x264crf=u"--crf "+str(crf)
150 os.environ['LANG']="ja_JP.UTF-8"
153 streampath=os.path.join(os.path.dirname(pin),str(random.randint(10000, 999999)))
154 os.system(u"mkfifo "+streampath)
155 encexe=mencoder+u" \""+pin+u"\" -vfm ffmpeg -quiet -sws 9 "+encvf+u",format=i420 "+fps+" "+ofps+" -oac mp3lame -ovc raw -of rawvideo -o \""+streampath+"\" &"
156 encexe=encexe+u" nice -n 19 "+x264+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" "+streampath+" "+x264streamsize
157 encexe=u"nice -n 19 " +encexe
158 recdblist.printutf8(encexe)
161 txt=unicode(commands.getoutput(encexe.encode('utf-8')),'utf-8')
164 os.system("rm "+streampath)
165 recdblist.addCommandLog(pin, u"Mencoder", encexe, txt)
167 def encode_ffmpeg(pin,pout,size,is24fps,quality,crf):
171 ffmpeg=configreader.getpath("ffmpeg")
172 x264=configreader.getpath("x264")
174 x264fps="--fps 30000/1001"
181 xtt=configreader.getenv("x264_thread")
187 x264_addline=configreader.getenv("x264_addline")
190 x264streamsize=u"1280x720"
194 x264streamsize=u"854x480"
196 elif size == "FullHD":
198 x264streamsize=u"1920x1080"
202 x264streamsize=u"720x480"
204 elif size == "QVGA_BASE":
206 x264streamsize=u"320x240"
208 elif size == "WVGA_BASE":
210 x264streamsize=u"854x480"
214 x264streamsize=u"1280x720"
217 x264preset=u"--preset ultrafast"
219 x264preset=u"--preset veryfast"
221 x264preset=u"--preset fast"
223 x264preset=u"--preset medium"
225 x264preset=u"--preset slow"
227 x264preset=u"--preset slower"
228 if size == "WVGA_BASE" or size == "QVGA_BASE":
229 x264profile=" --level 32 --profile baseline "
231 x264profile=" --level 41 --profile high "
232 x264crf=u"--crf "+str(crf)
234 os.environ['LANG']="ja_JP.UTF-8"
235 exe=ffmpeg+u" -y -i \""+pin+"\" -vsync 400 -vcodec rawvideo -pix_fmt yuv420p "+s+fps+"-deinterlace -an -f rawvideo - |"
236 exe=exe+u" nice -n 19 "+x264+" "+x264crf+u" "+x264_addline+u" --threads "+x264_thread+" "+x264profile+x264preset+" "+x264tune+" "+x264fps+" -o \""+pout+"\" - "+x264streamsize
237 exe = "nice -n 19 " + exe
239 recdblist.printutf8(exe)
241 txt=unicode(commands.getoutput(exe.encode('utf-8')),'utf-8')
244 recdblist.addCommandLog(pin, u"FFmpeg動画エンコード", exe, txt)