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 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         ptmp=os.path.exists(os.path.join(dir,etitle+u".mp4"))
136         e2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+ptmp+u"\'"
137         os.system(e2)
138         shutil.move(ptmp,pout)
139     logo=pin.replace(u".avi",u".log")
140     txt = "\n####MP4Box-log####\n"+txt
141     f=open(logo,'a')
142     f.write(txt.encode('utf-8'))
143     f.close()
144     time.sleep(10)
145     delpath=[]
146     delpath.append(pin.replace(u".avi",u".120.avi"))
147     delpath.append(pin.replace(u".avi",u".sa.avi"))
148     delpath.append(pin.replace(u".avi",u".ts"))
149     delpath.append(pin.replace(u".avi",u".aac"))
150     delpath.append(pin.replace(u".avi",u".m2v"))
151     delpath.append(pin.replace(u".avi",u".120_video.264"))
152     delpath.append(pin.replace(u".avi",u".ts.b25"))
153     if os.path.exists(pout):
154         if os.path.exists(audiopath):
155             os.remove(audiopath)
156         if os.path.exists(videopath):
157             os.remove(videopath)
158         """
159         for dp in delpath:
160             try:
161                 os.remove(dp)
162             except:
163                 ""
164         """
165 def mkv2mp4(pin,pout):
166     exeb = configreader.getpath(u"mkvextract")
167     exe = configreader.getpath(u"MP4Box")
168     dtsedit=configreader.getpath("DtsEdit")
169     wineexe=configreader.getpath("wine")
170     dir=os.path.split(pin)[0]
171     title=os.path.split(pin)[1]
172     title=os.path.splitext(title)[0]
173     etitle=base64.b16encode(title.encode('utf-8'))
174     audiopath=os.path.join(dir,etitle+u"_audio.raw")
175     videopath=os.path.join(dir,etitle+u"_video.264")
176     timecodepath=os.path.join(dir,etitle+u"_1_timecode.txt")
177     tmpmp4=os.path.join(dir,etitle+u".tmp.mp4")
178     tmpmp42=os.path.join(dir,etitle+u".mp4")
179     exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'"
180     exe1=exeb+u" timecodes_v2 \'"+pin+"\' 1:\'"+timecodepath+"\'"
181     exe2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -new \'"+tmpmp4+u"\'"
182     exe3=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+timecodepath+u"\' \'Z:\\"+tmpmp4+u"\' -o \'Z:\\"+tmpmp42+u"\'"
183     exe4=exe+u" -add \'"+audiopath+u"\' \'"+tmpmp42+u"\'"
184     logo=pin.replace(".avi",".log")
185     os.environ['LANG']="ja_JP.UTF-8"
186     txt= exe0+"\n"+exe1+"\n"+exe2+"\n"+exe3+"\n"
187     try:
188         txt = txt+unicode(commands.getoutput(exe0.encode('utf-8')),'utf-8')
189     except:
190         ""
191     try:
192         txt = txt+unicode(commands.getoutput(exe1.encode('utf-8')),'utf-8')
193     except:
194         ""
195     try:
196         txt = txt+unicode(commands.getoutput(exe2.encode('utf-8')),'utf-8')
197     except:
198         ""
199     try:
200         commands.getoutput(exe3.encode('utf-8'))
201     except:
202         ""
203     try:
204         txt = txt+unicode(commands.getoutput(exe4.encode('utf-8')),'utf-8')
205     except:
206         ""
207     shutil.move(tmpmp42,pout)
208     txt = "\n####MKV2MP4-log####\n"+txt
209     f=open(logo,'a')
210     f.write(txt.encode('utf-8'))
211     f.close()
212     time.sleep(10)
213     if os.path.exists(pout):
214         if os.path.exists(audiopath):
215             os.remove(audiopath)
216         if os.path.exists(videopath):
217             os.remove(videopath)
218         if os.path.exists(timecodepath):
219             os.remove(timecodepath)
220         if os.path.exists(tmpmp4):
221             os.remove(tmpmp4)
222         if os.path.exists(tmpmp42):
223             os.remove(tmpmp42)