OSDN Git Service

fix mkv2mp4.
[rec10/rec10-git.git] / rec10 / trunk / src / tv2mp4.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
5 import commands
6 import shutil
7 import tv2avi
8 import os
9 import re
10 import os.path
11 import base64
12 import time
13 import recdblist
14 import configreader
15
16 def ts2mp4(pin, pout, opt):
17     dir=os.path.split(pout)[0]
18     title=os.path.split(pout)[1]
19     title=os.path.splitext(title)[0]
20     tpavi=os.path.join(dir, title+".avi")
21     tv2avi.ts2avi(pin, tpavi, opt)
22     time.sleep(10)
23     if not os.path.exists(tpavi):
24         if os.path.exists(pin):
25             tpavi=pin
26     avi2mp4(tpavi,pout,opt)
27 def avi2mp4(pin,pout,opt):
28     isvfr=0
29     if re.search(u"v", opt):
30         isvfr=1
31     exe = configreader.getpath(u"MP4Box")
32     dir=os.path.split(pin)[0]
33     title=os.path.split(pin)[1]
34     title=os.path.splitext(title)[0]
35     etitle=base64.b16encode(title)
36     audiopath=os.path.join(dir,etitle+u"_audio.raw")
37     videopath=os.path.join(dir,etitle+u"_video.264")
38     exes=[]
39     os.environ['LANG']="ja_JP.UTF-8"
40     txt=""
41     if isvfr==1:
42         wineexe=configreader.getpath("wine")
43         cfr2tc=configreader.getpath("cfr2tc")
44         dtsedit=configreader.getpath("DtsEdit")
45         pin120=os.path.join(dir,etitle+u".120.avi")
46         pints=os.path.join(dir,title+u".ts")
47         pmp4=os.path.join(dir,etitle+u".tmp.mp4")
48         pmp42=os.path.join(dir,etitle+u".tmp2.mp4")
49         pintimecode=os.path.join(dir,etitle+u".timecode.txt")
50         pinaac=os.path.join(dir,etitle+u".aac")
51         evfr=[]
52         evfr.append("ffmpeg -i '"+pints+"' -vn -f aac -acodec copy '"+pinaac+"'")
53         evfr.append(wineexe+u" "+cfr2tc+u" '"+"Z:\\"+pin+"' '"+"Z:\\"+pin120+u"' 'Z:\\"+pintimecode+u"' 2")
54         evfr.append(exe+u" -aviraw video \'"+pin120+u"\'")
55         videotpath=os.path.join(dir,etitle+u".120_video.264")
56         evfr.append(exe+u" -fps 29.970030 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+pmp4+"\'")
57         evfr.append(wineexe+u" "+dtsedit+u" -tc \'Z:\\"+pintimecode+u"\' \'Z:\\"+pmp4+"\' -o \'Z:\\"+pmp42+"\'")
58         evfr.append(exe+u" -add \'"+pinaac+"\' \'"+pmp42+"\'")
59         exes=evfr
60         for e in exes:
61             recdblist.printutf8(e)
62             txt=txt+e+"\n"
63             try:
64                 txt=txt+unicode(commands.getoutput(e.encode('utf-8')),'utf-8')
65             except:
66                 ""
67         try:
68             shutil.move(pmp42,pout)
69         except:
70             ""
71         if (not os.path.exists(pin.replace(".avi",".120.avi"))and (not os.path.exists(pout))):
72             txt=txt+"CFR2TC error. maybe avi file is over 1GB.(ODML error would have happend.)"
73             ptmp=pin.replace(".avi",".noodml.avi")
74             e="mencoder -noodml -forceidx -oac copy -ovc copy -o \'"+ptmp+"\'"+" \'"+pin+"\'"
75             ex="nice -n 17 "+e
76             e2=wineexe+u" "+cfr2tc+u" '"+"Z:\\"+ptmp+"' '"+"Z:\\"+pin120+u"' '"+"Z:\\"+pintimecode+u"' 2"
77             ex2="nice -n 17 "+e2
78             e3=exe+u" -aviraw video \'"+pin120+u"\'"
79             ex3="nice -n 17 "+e3
80             e4=exe+u" -fps 29.97 -add \'"+videotpath+"\' -new \'"+pmp4+"\'"
81             ex4="nice -n 17 "+e4
82             e5=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+pintimecode+u"\' \'Z:\\"+pmp4+"\' -o \'Z:\\"+pmp42+"\'"
83             ex5="nice -n 17 "+e5
84             e6=exe+u" -add \'"+pinaac+"\' \'"+pmp42+"\'"
85             ex6="nice -n 17 "+e6
86             recdblist.printutf8(e)
87             txt=txt+e+"\n"
88             try:
89                 cmdt=unicode(commands.getoutput(ex.encode('utf-8')),'utf-8')
90             except:
91                 ""
92             try:
93                 cmdt=unicode(commands.getoutput(ex2.encode('utf-8')),'utf-8')
94             except:
95                 ""
96             try:
97                 cmdt=unicode(commands.getoutput(ex3.encode('utf-8')),'utf-8')
98             except:
99                 ""
100             try:
101                 cmdt=unicode(commands.getoutput(ex4.encode('utf-8')),'utf-8')
102             except:
103                 ""
104             try:
105                 cmdt=unicode(commands.getoutput(ex5.encode('utf-8')),'utf-8')
106             except:
107                 ""
108             try:
109                 cmdt=unicode(commands.getoutput(ex6.encode('utf-8')),'utf-8')
110             except:
111                 ""
112             try:
113                 os.remove(ptmp)
114             except:
115                 ""
116             try:
117                 shutil.move(pmp42,pout)
118             except:
119                 ""
120             txt=txt+cmdt+"\n"
121     else:
122         try:
123             shutil.move(pin,os.path.join(dir,etitle+".avi"))
124         except:
125             ""
126         e0=exe+u" -aviraw audio \'"+os.path.join(dir,etitle+".avi")+u"\'"
127         e1=exe+u" -aviraw video \'"+os.path.join(dir,etitle+".avi")+u"\'"
128         os.system(e0)
129         os.system(e1)
130         if not os.path.exists(audiopath):
131             if os.path.exists(os.path.join(dir,etitle+u"_audio.mp3")):
132                 audiopath=os.path.join(dir,etitle+u"_audio.mp3")
133             elif os.path.exists(os.path.join(dir,etitle+u"_audio.aac")):
134                 audiopath=os.path.join(dir,etitle+u"_audio.aac")
135         else:
136             shutil.move(audiopath,os.path.join(dir,etitle+u"_audio.aac"))
137             audiopath=os.path.join(dir,etitle+u"_audio.aac")
138         ptmp=os.path.exists(os.path.join(dir,etitle+u".mp4"))
139         e2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+ptmp+u"\'"
140         os.system(e2)
141         shutil.move(ptmp,pout)
142     logo=pin.replace(u".avi",u".log")
143     txt = "\n####MP4Box-log####\n"+txt
144     f=open(logo,'a')
145     f.write(txt.encode('utf-8'))
146     f.close()
147     time.sleep(10)
148     delpath=[]
149     delpath.append(pin.replace(u".avi",u".120.avi"))
150     delpath.append(pin.replace(u".avi",u".sa.avi"))
151     delpath.append(pin.replace(u".avi",u".ts"))
152     delpath.append(pin.replace(u".avi",u".aac"))
153     delpath.append(pin.replace(u".avi",u".m2v"))
154     delpath.append(pin.replace(u".avi",u".120_video.264"))
155     delpath.append(pin.replace(u".avi",u".ts.b25"))
156     if os.path.exists(pout):
157         if os.path.exists(audiopath):
158             os.remove(audiopath)
159         if os.path.exists(videopath):
160             os.remove(videopath)
161         """
162         for dp in delpath:
163             try:
164                 os.remove(dp)
165             except:
166                 ""
167         """
168 def mkv2mp4(pin,pout):
169     exeb = configreader.getpath(u"mkvextract")
170     exe = configreader.getpath(u"MP4Box")
171     dtsedit=configreader.getpath("DtsEdit")
172     wineexe=configreader.getpath("wine")
173     dir=os.path.split(pin)[0]
174     title=os.path.split(pin)[1]
175     title=os.path.splitext(title)[0]
176     etitle=base64.b16encode(title.encode('utf-8'))
177     audiopath=os.path.join(dir,etitle+u"_audio.aac")
178     videopath=os.path.join(dir,etitle+u"_video.264")
179     timecodepath=os.path.join(dir,etitle+u"_1_timecode.txt")
180     tmpmp4=os.path.join(dir,etitle+u".tmp.mp4")
181     tmpmp42=os.path.join(dir,etitle+u".mp4")
182     exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'"
183     exe1=exeb+u" timecodes_v2 \'"+pin+"\' 1:\'"+timecodepath+"\'"
184     exe2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -new \'"+tmpmp4+u"\'"
185     exe3=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+timecodepath+u"\' \'Z:\\"+tmpmp4+u"\' -o \'Z:\\"+tmpmp42+u"\'"
186     exe4=exe+u" -add \'"+audiopath+u"\' \'"+tmpmp42+u"\'"
187     logo=pin.replace(".avi",".log")
188     os.environ['LANG']="ja_JP.UTF-8"
189     txt= exe0+"\n"+exe1+"\n"+exe2+"\n"+exe3+"\n"
190     try:
191         txt = txt+unicode(commands.getoutput(exe0.encode('utf-8')),'utf-8')
192     except:
193         ""
194     try:
195         txt = txt+unicode(commands.getoutput(exe1.encode('utf-8')),'utf-8')
196     except:
197         ""
198     try:
199         txt = txt+unicode(commands.getoutput(exe2.encode('utf-8')),'utf-8')
200     except:
201         ""
202     try:
203         os.system(exe3.encode('utf-8'))
204     except:
205         ""
206     try:
207         txt = txt+unicode(commands.getoutput(exe4.encode('utf-8')),'utf-8')
208     except:
209         ""
210     shutil.move(tmpmp42,pout)
211     txt = "\n####MKV2MP4-log####\n"+txt
212     f=open(logo,'a')
213     f.write(txt.encode('utf-8'))
214     f.close()
215     time.sleep(10)
216     if os.path.exists(pout):
217         if os.path.exists(audiopath):
218             os.remove(audiopath)
219         if os.path.exists(videopath):
220             os.remove(videopath)
221         if os.path.exists(timecodepath):
222             os.remove(timecodepath)
223         if os.path.exists(tmpmp4):
224             os.remove(tmpmp4)
225         if os.path.exists(tmpmp42):
226             os.remove(tmpmp42)