3 # Rec10 TS Recording Tools
\r
4 # Copyright (C) 2009-2010 Yukikaze
\r
18 def ts2mp4(pin, pout, opt):
\r
19 dir=os.path.split(pout)[0]
\r
20 title=os.path.split(pout)[1]
\r
21 title=os.path.splitext(title)[0]
\r
22 tpraw=os.path.join(dir, title+".264")
\r
23 tpmp4=os.path.join(dir, title+".mp4")
\r
24 tv2avi.ts2raw(pin, tpraw, opt)
\r
26 raw2mp4(tpraw, tpmp4, opt)
\r
27 def raw2mp4(pin,pout,opt):
\r
28 dir=os.path.split(pout)[0]
\r
29 title=os.path.split(pout)[1]
\r
30 title=os.path.splitext(title)[0]
\r
31 duration="-fps 29.970030 "
\r
32 ffmpeg=configreader.getpath("ffmpeg")
\r
35 if re.search("a",opt):
\r
36 duration="-fps 23.976023 "
\r
37 elif re.search("v",opt):
\r
38 duration="-fps 23.976023 "
\r
39 if re.search("d",opt):
\r
41 duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置
\r
42 elif re.search("5",opt):
\r
44 duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置
\r
45 exe = configreader.getpath("mp4box")
\r
47 os.environ['LANG']="ja_JP.UTF-8"
\r
49 wineexe=configreader.getpath("wine")
\r
50 pincap=pin.replace(".264",".srt")
\r
51 cap2ass=configreader.getpath("Caption2Ass")
\r
52 pints=pin.replace(".264",".ts")
\r
53 paac1=pin.replace(".264","_1.aac")
\r
54 paac2=pin.replace(".264","_2.aac")
\r
55 recdblist.printutf8(paac1)
\r
56 if not os.path.exists(paac1):
\r
57 paac1=pin.replace(".264","_1.mp3")
\r
58 if not os.path.exists(paac2):
\r
59 paac2=pin.replace(".264","_2.mp3")
\r
60 e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
\r
61 e1=exe +u" "+duration+" -add \""+pin+"\" -new \""+pout+"\""
\r
62 e1a1=exe +u" -add \""+paac1+"\" \""+pout+"\""
\r
63 e1a2=exe +u" -add \""+paac2+"\" \""+pout+"\""
\r
64 e1s=exe +u" -add \""+pincap+"\" \""+pout+"\""
\r
65 if os.path.isfile(cap2ass):
\r
66 txt=txt+execcomd(e0)
\r
67 execmp4box(pin, pout, e1)
\r
68 addmp4(paac1, pout, e1a1)
\r
69 addmp4(paac2, pout, e1a2)
\r
70 if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
\r
71 addmp4(pincap, pout, e1s)
\r
72 elif ispentaaudio==1:
\r
73 wineexe=configreader.getpath("wine")
\r
74 pincap=pin.replace(".264",".srt")
\r
75 cap2ass=configreader.getpath("Caption2Ass")
\r
76 pints=pin.replace(".264",".ts")
\r
77 paac1=pin.replace(".264","_1.aac")
\r
78 paac2=pin.replace(".264","_2.aac")
\r
79 recdblist.printutf8(paac1)
\r
80 if not os.path.exists(paac1):
\r
81 paac1=pin.replace(".264","_1.mp3")
\r
82 if not os.path.exists(paac2):
\r
83 paac2=pin.replace(".264","_2.mp3")
\r
84 e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
\r
85 e1=exe +u" "+duration+" -add \""+pin+"\" -new \""+pout+"\""
\r
86 e1a1=exe +u" -add \""+paac1+"\" \""+pout+"\""
\r
87 e1a2=exe +u" -add \""+paac2+"\" \""+pout+"\""
\r
88 e1s=exe +u" -add \""+pincap+"\" \""+pout+"\""
\r
89 if os.path.isfile(cap2ass):
\r
90 txt=txt+execcomd(e0)
\r
91 execmp4box(pin, pout, e1)
\r
92 addmp4(paac1, pout, e1a1)
\r
93 addmp4(paac2, pout, e1a2)
\r
94 if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
\r
95 addmp4(pincap, pout, e1s)
\r
97 wineexe=configreader.getpath("wine")
\r
98 pincap=pin.replace(".264",".srt")
\r
99 cap2ass=configreader.getpath("Caption2Ass")
\r
100 pints=pin.replace(".264",".ts")
\r
101 pinaac=pin.replace(".264",".aac")
\r
102 e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
\r
103 e1=ffmpeg+" -i \""+pints+"\" -vn -f aac -acodec copy \""+pinaac+"\""
\r
104 e2=exe +u" "+duration+" -add \""+pin+"\" -new \""+pout+"\""
\r
105 e2a=exe +u" -add \""+pinaac+"\" \""+pout+"\""
\r
106 e2s=exe +u" -add \""+pincap+"\" \""+pout+"\""
\r
107 if os.path.isfile(cap2ass):
\r
108 txt=txt+execcomd(e0)
\r
109 txt=txt+execcomd(e1)
\r
110 execmp4box(pin, pout, e2)
\r
111 addmp4(pinaac, pout, e2a)
\r
112 if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
\r
113 addmp4(pincap, pout, e2s)
\r
114 recdblist.addlog(pout, txt, "mp4box-log")
\r
115 if status.getSettings_auto_del_tmp()==1:
\r
116 if os.path.exists(pout):
\r
117 if os.path.getsize(pout)>100*1000*1000:
\r
118 deltitle(dir,title)
\r
119 def avi2mp4(pin,pout,opt):
\r
121 if re.search(u"v", opt):
\r
123 exe = configreader.getpath(u"MP4Box")
\r
124 dir=os.path.split(pin)[0]
\r
125 title=os.path.split(pin)[1]
\r
126 title=os.path.splitext(title)[0]
\r
127 etitle=base64.b16encode(title.encode('utf-8'))
\r
128 audiopath=os.path.join(dir,etitle+u"_audio.raw")
\r
129 videopath=os.path.join(dir,etitle+u"_video.264")
\r
131 os.environ['LANG']="ja_JP.UTF-8"
\r
134 wineexe=configreader.getpath("wine")
\r
135 cfr2tc=configreader.getpath("cfr2tc")
\r
136 dtsedit=configreader.getpath("DtsEdit")
\r
137 caption2ass=configreader.getpath("Caption2Ass")
\r
138 pin120=os.path.join(dir,etitle+u".120.avi")
\r
139 pints=os.path.join(dir,title+u".ts")
\r
140 pmp4=os.path.join(dir,etitle+u".tmp.mp4")
\r
141 pintimecode=os.path.join(dir,etitle+u".timecode.txt")
\r
142 pinaac=os.path.join(dir,etitle+u".aac")
\r
143 pincat=os.path.join(dir,etitle+u".srt")
\r
144 evfr0=ffmpeg+u" -i '"+pints+"' -vn -f aac -acodec copy '"+pinaac+"'"
\r
145 evfr1=wineexe+u" "+cfr2tc+u" '"+"Z:\\"+pin+"' '"+"Z:\\"+pin120+u"' 'Z:\\"+pintimecode+u"' 2"
\r
146 evfr2=exe+u" -aviraw video \'"+pin120+u"\'"
\r
147 videotpath=os.path.join(dir,etitle+u".120_video.h264")
\r
148 evfr3=exe+u" -fps 29.970030 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+pmp4+"\'"
\r
149 evfr4=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+pintimecode+u"\' \'Z:\\"+pmp4+"\' -o \'Z:\\"+pout+"\'"
\r
151 recdblist.printutf8(evfr0)
\r
153 txt=txt+unicode(commands.getoutput(evfr0.encode('utf-8')),'utf-8')
\r
157 recdblist.printutf8(evfr1)
\r
159 txt=txt+unicode(commands.getoutput(evfr1.encode('utf-8')),'utf-8')
\r
163 if not os.path.exists(pin120):
\r
164 evfr2=exe+u" -aviraw video \'"+pin+u"\'"
\r
165 videotpath=os.path.join(dir,etitle+u"_video.h264")
\r
166 evfr3=exe+u" -fps 29.970030 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+pmp4+"\'"
\r
167 recdblist.printutf8(evfr2)
\r
169 txt=txt+unicode(commands.getoutput(evfr2.encode('utf-8')),'utf-8')
\r
173 recdblist.printutf8(evfr3)
\r
175 txt=txt+unicode(commands.getoutput(evfr3.encode('utf-8')),'utf-8')
\r
179 recdblist.printutf8(evfr4)
\r
181 txt=txt+unicode(commands.getoutput(evfr4.encode('utf-8')),'utf-8')
\r
184 if (not os.path.exists(pin.replace(".avi",".120.avi"))and (not os.path.exists(pout))):
\r
185 txt=txt+"CFR2TC error. maybe avi file is over 1GB.(ODML error would have happened.)"
\r
186 ptmp=pin.replace(".avi",".noodml.avi")
\r
187 e="mencoder -noodml -forceidx -oac copy -ovc copy -o \'"+ptmp+"\'"+" \'"+pin+"\'"
\r
189 e2=wineexe+u" "+cfr2tc+u" '"+"Z:\\"+ptmp+"' '"+"Z:\\"+pin120+u"' '"+"Z:\\"+pintimecode+u"' 2"
\r
190 ex2="nice -n 17 "+e2
\r
191 e3=exe+u" -aviraw video \'"+pin120+u"\'"
\r
192 ex3="nice -n 17 "+e3
\r
193 e4=exe+u" -fps 29.97 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+pmp4+"\'"
\r
194 ex4="nice -n 17 "+e4
\r
195 e5=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+pintimecode+u"\' \'Z:\\"+pmp4+"\' -o \'Z:\\"+pout+"\'"
\r
196 ex5="nice -n 17 "+e5
\r
197 recdblist.printutf8(e)
\r
200 cmdt=unicode(commands.getoutput(ex.encode('utf-8')),'utf-8')
\r
204 cmdt=unicode(commands.getoutput(ex2.encode('utf-8')),'utf-8')
\r
208 if os.path.exists(pin120):
\r
209 cmdt=unicode(commands.getoutput(ex3.encode('utf-8')),'utf-8')
\r
213 cmdt=unicode(commands.getoutput(ex4.encode('utf-8')),'utf-8')
\r
217 cmdt=unicode(commands.getoutput(ex5.encode('utf-8')),'utf-8')
\r
227 shutil.move(pin,os.path.join(dir,etitle+".avi"))
\r
230 if os.path.exists(os.path.join(dir,etitle+".avi")):
\r
231 e0=exe+u" -aviraw audio \'"+os.path.join(dir,etitle+".avi")+u"\'"
\r
232 e1=exe+u" -aviraw video \'"+os.path.join(dir,etitle+".avi")+u"\'"
\r
235 if not os.path.exists(audiopath):
\r
236 if os.path.exists(os.path.join(dir,etitle+u"_audio.mp3")):
\r
237 audiopath=os.path.join(dir,etitle+u"_audio.mp3")
\r
238 elif os.path.exists(os.path.join(dir,etitle+u"_audio.aac")):
\r
239 audiopath=os.path.join(dir,etitle+u"_audio.aac")
\r
241 shutil.move(audiopath,os.path.join(dir,etitle+u"_audio.aac"))
\r
242 audiopath=os.path.join(dir,etitle+u"_audio.aac")
\r
243 ptmp=os.path.exists(os.path.join(dir,etitle+u".mp4"))
\r
244 e2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+ptmp+u"\'"
\r
246 shutil.move(ptmp,pout)
\r
247 recdblist.addlog(pin,txt,"MP4Box-log")
\r
250 delpath.append(pin.replace(u".avi",u".120.avi"))
\r
251 delpath.append(pin.replace(u".avi",u".sa.avi"))
\r
252 delpath.append(pin.replace(u".avi",u".m2v"))
\r
253 delpath.append(pin.replace(u".avi",u"_1.aac"))
\r
254 delpath.append(pin.replace(u".avi",u"_2.aac"))
\r
255 delpath.append(pin.replace(u".avi",u"_1.mp3"))
\r
256 delpath.append(pin.replace(u".avi",u"_2.mp3"))
\r
257 delpath.append(pin.replace(u".avi",u".ts"))
\r
258 delpath.append(pin.replace(u".avi",u".aac"))
\r
259 delpath.append(pin.replace(u".avi",u".m2v"))
\r
260 delpath.append(pin.replace(u".avi",u".120_video.264"))
\r
261 delpath.append(pin.replace(u".avi",u".ts.b25"))
\r
262 if os.path.exists(pout):
\r
263 if os.path.exists(audiopath):
\r
264 os.remove(audiopath)
\r
265 if os.path.exists(videopath):
\r
266 os.remove(videopath)
\r
274 def mkv2mp4(pin,pout):
\r
275 exeb = configreader.getpath(u"mkvextract")
\r
276 exe = configreader.getpath(u"MP4Box")
\r
277 dtsedit=configreader.getpath("DtsEdit")
\r
278 wineexe=configreader.getpath("wine")
\r
279 dir=os.path.split(pin)[0]
\r
280 title=os.path.split(pin)[1]
\r
281 title=os.path.splitext(title)[0]
\r
282 etitle=base64.b16encode(title.encode('utf-8'))
\r
283 audiopath=os.path.join(dir,etitle+u"_audio.aac")
\r
284 videopath=os.path.join(dir,etitle+u"_video.264")
\r
285 timecodepath=os.path.join(dir,etitle+u"_1_timecode.txt")
\r
286 tmpmp4=os.path.join(dir,etitle+u".tmp.mp4")
\r
287 exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'"
\r
288 exe1=exeb+u" timecodes_v2 \'"+pin+"\' 1:\'"+timecodepath+"\'"
\r
289 exe2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+tmpmp4+u"\'"
\r
290 exe3=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+timecodepath+u"\' \'Z:\\"+tmpmp4+u"\' -o \'Z:\\"+pout+u"\'"
\r
291 os.environ['LANG']="ja_JP.UTF-8"
\r
294 txt=txt+execcomd(exe0)+"\n"
\r
295 txt=txt+execcomd(exe1)+"\n"
\r
296 txt=txt+execcomd(exe2)+"\n"
\r
297 txt=txt+execcomd(exe3)+"\n"
\r
300 recdblist.addlog(pin, txt, "MKV2MP4-log")
\r
301 txt = "\n####MKV2MP4-log####\n"+txt
\r
303 if os.path.exists(pout):
\r
304 if os.path.exists(audiopath):
\r
305 os.remove(audiopath)
\r
306 if os.path.exists(videopath):
\r
307 os.remove(videopath)
\r
308 if os.path.exists(timecodepath):
\r
309 os.remove(timecodepath)
\r
310 if os.path.exists(tmpmp4):
\r
312 def execmp4box(pin,pout,cmd):
\r
313 title=os.path.splitext(os.path.split(pin)[1])[0]
\r
314 ptin=os.path.join(os.path.dirname(pin),base64.b16encode(title.encode('utf-8'))+".264")
\r
315 recdblist.printutf8(ptin)
\r
316 shutil.move(pin,ptin)
\r
318 ptout=os.path.join(os.path.dirname(pout),base64.b16encode(title.encode('utf-8'))+".mp4")
\r
319 cmdn=string.replace(cmd,pin,ptin)
\r
320 cmdn=string.replace(cmdn,pout,ptout)
\r
321 recdblist.printutf8(cmdn)
\r
325 except Exception, inst:
\r
326 txt= "error occures in execmp4box\n"
\r
327 txt=txt+ str(type(inst))+"\n"
\r
329 recdblist.addlog(pin, txt, "MP4Box-log")
\r
331 shutil.move(ptin,pin)
\r
332 shutil.move(ptout,pout)
\r
334 def addmp4(padd,pout,cmd):#without video
\r
335 title=os.path.splitext(os.path.split(padd)[1])[0]
\r
336 ext=os.path.splitext(os.path.split(padd)[1])[1]
\r
337 ptadd=os.path.join(os.path.dirname(padd),base64.b16encode(title.encode('utf-8'))+ext)
\r
338 ptoutb=os.path.join(os.path.dirname(pout),base64.b16encode(title.encode('utf-8'))+"_b.mp4")
\r
339 ptout=os.path.join(os.path.dirname(pout),base64.b16encode(title.encode('utf-8'))+".mp4")
\r
340 shutil.move(padd,ptadd)
\r
341 if os.path.isfile(pout):
\r
342 shutil.move(pout,ptoutb)
\r
344 cmdn=string.replace(cmd,padd,ptadd)
\r
345 cmdn=string.replace(cmdn,u"-out \""+pout,u"-add \""+ptoutb+"\" -new \""+ptout)
\r
346 cmdn=string.replace(cmdn,u"\""+pout,u"-add \""+ptoutb+"\" -new \""+ptout)
\r
347 cmdn=string.replace(cmdn,pout,ptout)
\r
348 recdblist.printutf8(cmdn)
\r
352 except Exception, inst:
\r
353 txt= "error occures in addmp4\n"
\r
354 txt=txt+ str(type(inst))+"\n"
\r
356 recdblist.addlog(pout, txt, "MP4Box-log-add")
\r
359 shutil.move(ptadd,padd)
\r
360 shutil.move(ptout,pout)
\r
365 txt=u"Cmd : "+cmd+"\n"
\r
366 txt2=commands.getoutput(cmd.encode('utf-8'))
\r
367 txt=txt+unicode(txt2,"utf-8")+"\n"
\r
372 def deltitle(path,title):
\r
374 dp.append(os.path.join(path,title+".avi"))
\r
375 dp.append(os.path.join(path,title+".264"))
\r
376 dp.append(os.path.join(path,title+".120.avi"))
\r
377 dp.append(os.path.join(path,title+".noodml.avi"))
\r
378 dp.append(os.path.join(path,title+".aac"))
\r
379 dp.append(os.path.join(path,title+".m2v"))
\r
380 dp.append(os.path.join(path,title+"_1.aac"))
\r
381 dp.append(os.path.join(path,title+"_2.aac"))
\r
382 dp.append(os.path.join(path,title+"_1.mp3"))
\r
383 dp.append(os.path.join(path,title+"_2.mp3"))
\r
384 dp.append(os.path.join(path,title+".srt"))
\r
385 if os.path.exists(os.path.join(path,title+".ts"))and os.path.exists(os.path.join(path,title+".ts.b25")):
\r
386 if os.path.getsize(os.path.join(path,title+".ts"))*10>os.path.getsize(os.path.join(path,title+".ts.b25")):
\r
387 if os.path.getsize(os.path.join(path,title+".ts"))>1000*1000*100:
\r
388 dp.append(os.path.join(path,title+".ts.b25"))
\r
389 if os.path.exists(os.path.join(path,title+".mp4")):
\r
390 if os.path.getsize(os.path.join(path,title+".mp4"))>1000*1000*10:
\r