OSDN Git Service

add remove ts option.
[rec10/rec10-git.git] / rec10 / trunk / src / tv2mp4.py
1 #!/usr/bin/python\r
2 # coding: UTF-8\r
3 # Rec10 TS Recording Tools\r
4 # Copyright (C) 2009-2010 Yukikaze\r
5 import commands\r
6 import shutil\r
7 import tv2avi\r
8 import os\r
9 import re\r
10 import os.path\r
11 import string\r
12 import base64\r
13 import time\r
14 import recdblist\r
15 import configreader\r
16 import status\r
17 \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
25     time.sleep(10)\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
33     isdualaac=0\r
34     ispentaaudio=0\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
40         isdualaac=1\r
41         duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置\r
42     elif re.search("5",opt):\r
43         ispentaaudio=1\r
44         duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置\r
45     exe = configreader.getpath("mp4box")\r
46     txt=""\r
47     os.environ['LANG']="ja_JP.UTF-8"\r
48     if isdualaac==1:\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
96     else:\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
120     isvfr=0\r
121     if re.search(u"v", opt):\r
122         isvfr=1\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
130     exes=[]\r
131     os.environ['LANG']="ja_JP.UTF-8"\r
132     txt=""\r
133     if isvfr==1:\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
150         try:\r
151             recdblist.printutf8(evfr0)\r
152             txt=txt+evfr0+"\n"\r
153             txt=txt+unicode(commands.getoutput(evfr0.encode('utf-8')),'utf-8')\r
154         except:\r
155             ""\r
156         try:\r
157             recdblist.printutf8(evfr1)\r
158             txt=txt+evfr1+"\n"\r
159             txt=txt+unicode(commands.getoutput(evfr1.encode('utf-8')),'utf-8')\r
160         except:\r
161             ""\r
162         try:\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
168             txt=txt+evfr2+"\n"\r
169             txt=txt+unicode(commands.getoutput(evfr2.encode('utf-8')),'utf-8')\r
170         except:\r
171             ""\r
172         try:\r
173             recdblist.printutf8(evfr3)\r
174             txt=txt+evfr3+"\n"\r
175             txt=txt+unicode(commands.getoutput(evfr3.encode('utf-8')),'utf-8')\r
176         except:\r
177             ""\r
178         try:\r
179             recdblist.printutf8(evfr4)\r
180             txt=txt+evfr4+"\n"\r
181             txt=txt+unicode(commands.getoutput(evfr4.encode('utf-8')),'utf-8')\r
182         except:\r
183             ""\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
188             ex="nice -n 17 "+e\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
198             txt=txt+e+"\n"\r
199             try:\r
200                 cmdt=unicode(commands.getoutput(ex.encode('utf-8')),'utf-8')\r
201             except:\r
202                 ""\r
203             try:\r
204                 cmdt=unicode(commands.getoutput(ex2.encode('utf-8')),'utf-8')\r
205             except:\r
206                 ""\r
207             try:\r
208                 if os.path.exists(pin120):\r
209                     cmdt=unicode(commands.getoutput(ex3.encode('utf-8')),'utf-8')\r
210             except:\r
211                 ""\r
212             try:\r
213                 cmdt=unicode(commands.getoutput(ex4.encode('utf-8')),'utf-8')\r
214             except:\r
215                 ""\r
216             try:\r
217                 cmdt=unicode(commands.getoutput(ex5.encode('utf-8')),'utf-8')\r
218             except:\r
219                 ""\r
220             try:\r
221                 os.remove(ptmp)\r
222             except:\r
223                 ""\r
224             txt=txt+cmdt+"\n"\r
225     else:\r
226         try:\r
227             shutil.move(pin,os.path.join(dir,etitle+".avi"))\r
228         except:\r
229             ""\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
233             os.system(e0)\r
234             os.system(e1)\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
240         else:\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
245         os.system(e2)\r
246         shutil.move(ptmp,pout)\r
247     recdblist.addlog(pin,txt,"MP4Box-log")\r
248     time.sleep(10)\r
249     delpath=[]\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
267         """\r
268         for dp in delpath:\r
269             try:\r
270                 os.remove(dp)\r
271             except:\r
272                 ""\r
273         """\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
292     txt=""\r
293     try:\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
298     except:\r
299         ""\r
300     recdblist.addlog(pin, txt, "MKV2MP4-log")\r
301     txt = "\n####MKV2MP4-log####\n"+txt\r
302     time.sleep(10)\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
311             os.remove(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
317     time.sleep(10)\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
322     txt=""\r
323     try:\r
324         txt=execcomd(cmdn)\r
325     except Exception, inst:\r
326         txt= "error occures in execmp4box\n"\r
327         txt=txt+ str(type(inst))+"\n"\r
328         txt=txt+str(inst)\r
329     recdblist.addlog(pin, txt, "MP4Box-log")\r
330     time.sleep(5)\r
331     shutil.move(ptin,pin)\r
332     shutil.move(ptout,pout)\r
333     time.sleep(5)\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
343     time.sleep(5)\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
349     txt=""\r
350     try:\r
351         txt=execcomd(cmdn)\r
352     except Exception, inst:\r
353         txt= "error occures in addmp4\n"\r
354         txt=txt+ str(type(inst))+"\n"\r
355         txt=txt+str(inst)\r
356     recdblist.addlog(pout, txt, "MP4Box-log-add")\r
357     time.sleep(5)\r
358     os.remove(ptoutb)\r
359     shutil.move(ptadd,padd)\r
360     shutil.move(ptout,pout)\r
361     time.sleep(5)\r
362 def execcomd(cmd):\r
363     txt=""\r
364     try:\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
368     except:\r
369         ""\r
370     return txt\r
371 \r
372 def deltitle(path,title):\r
373     dp=[]\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 configreader.getenv("remove_ts")==1:\r
386         dp.append(os.path.join(path,title+".ts"))\r
387     if os.path.exists(os.path.join(path,title+".ts"))and os.path.exists(os.path.join(path,title+".ts.b25")):\r
388         if os.path.getsize(os.path.join(path,title+".ts"))*10>os.path.getsize(os.path.join(path,title+".ts.b25")):\r
389             if os.path.getsize(os.path.join(path,title+".ts"))>1000*1000*100:\r
390                 dp.append(os.path.join(path,title+".ts.b25"))\r
391     if os.path.exists(os.path.join(path,title+".mp4")):\r
392         if os.path.getsize(os.path.join(path,title+".mp4"))>1000*1000*10:\r
393             for ip in dp:\r
394                 try:\r
395                     os.remove(ip)\r
396                 except:\r
397                     ""\r