OSDN Git Service

fix mp4 vfr.
[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.encode('utf-8'))
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         pintimecode=os.path.join(dir,etitle+u".timecode.txt")
49         pinaac=os.path.join(dir,etitle+u".aac")
50         evfr=[]
51         evfr.append("ffmpeg -i '"+pints+"' -vn -f aac -acodec copy '"+pinaac+"'")
52         evfr.append(wineexe+u" "+cfr2tc+u" '"+"Z:\\"+pin+"' '"+"Z:\\"+pin120+u"' 'Z:\\"+pintimecode+u"' 2")
53         evfr.append(exe+u" -aviraw video \'"+pin120+u"\'")
54         videotpath=os.path.join(dir,etitle+u".120_video.h264")
55         evfr.append(exe+u" -fps 29.970030 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+pmp4+"\'")
56         evfr.append(wineexe+u" "+dtsedit+u" -tc \'Z:\\"+pintimecode+u"\' \'Z:\\"+pmp4+"\' -o \'Z:\\"+pout+"\'")
57         exes=evfr
58         for e in exes:
59             recdblist.printutf8(e)
60             txt=txt+e+"\n"
61             try:
62                 txt=txt+unicode(commands.getoutput(e.encode('utf-8')),'utf-8')
63             except:
64                 ""
65         if (not os.path.exists(pin.replace(".avi",".120.avi"))and (not os.path.exists(pout))):
66             txt=txt+"CFR2TC error. maybe avi file is over 1GB.(ODML error would have happend.)"
67             ptmp=pin.replace(".avi",".noodml.avi")
68             e="mencoder -noodml -forceidx -oac copy -ovc copy -o \'"+ptmp+"\'"+" \'"+pin+"\'"
69             ex="nice -n 17 "+e
70             e2=wineexe+u" "+cfr2tc+u" '"+"Z:\\"+ptmp+"' '"+"Z:\\"+pin120+u"' '"+"Z:\\"+pintimecode+u"' 2"
71             ex2="nice -n 17 "+e2
72             e3=exe+u" -aviraw video \'"+pin120+u"\'"
73             ex3="nice -n 17 "+e3
74             e4=exe+u" -fps 29.97 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+pmp4+"\'"
75             ex4="nice -n 17 "+e4
76             e5=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+pintimecode+u"\' \'Z:\\"+pmp4+"\' -o \'Z:\\"+pout+"\'"
77             ex5="nice -n 17 "+e5
78             recdblist.printutf8(e)
79             txt=txt+e+"\n"
80             try:
81                 cmdt=unicode(commands.getoutput(ex.encode('utf-8')),'utf-8')
82             except:
83                 ""
84             try:
85                 cmdt=unicode(commands.getoutput(ex2.encode('utf-8')),'utf-8')
86             except:
87                 ""
88             try:
89                 cmdt=unicode(commands.getoutput(ex3.encode('utf-8')),'utf-8')
90             except:
91                 ""
92             try:
93                 cmdt=unicode(commands.getoutput(ex4.encode('utf-8')),'utf-8')
94             except:
95                 ""
96             try:
97                 cmdt=unicode(commands.getoutput(ex5.encode('utf-8')),'utf-8')
98             except:
99                 ""
100             try:
101                 os.remove(ptmp)
102             except:
103                 ""
104             txt=txt+cmdt+"\n"
105     else:
106         try:
107             shutil.move(pin,os.path.join(dir,etitle+".avi"))
108         except:
109             ""
110         e0=exe+u" -aviraw audio \'"+os.path.join(dir,etitle+".avi")+u"\'"
111         e1=exe+u" -aviraw video \'"+os.path.join(dir,etitle+".avi")+u"\'"
112         os.system(e0)
113         os.system(e1)
114         if not os.path.exists(audiopath):
115             if os.path.exists(os.path.join(dir,etitle+u"_audio.mp3")):
116                 audiopath=os.path.join(dir,etitle+u"_audio.mp3")
117             elif os.path.exists(os.path.join(dir,etitle+u"_audio.aac")):
118                 audiopath=os.path.join(dir,etitle+u"_audio.aac")
119         else:
120             shutil.move(audiopath,os.path.join(dir,etitle+u"_audio.aac"))
121             audiopath=os.path.join(dir,etitle+u"_audio.aac")
122         ptmp=os.path.exists(os.path.join(dir,etitle+u".mp4"))
123         e2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+ptmp+u"\'"
124         os.system(e2)
125         shutil.move(ptmp,pout)
126     logo=pin.replace(u".avi",u".log")
127     txt = "\n####MP4Box-log####\n"+txt
128     f=open(logo,'a')
129     f.write(txt.encode('utf-8'))
130     f.close()
131     time.sleep(10)
132     delpath=[]
133     delpath.append(pin.replace(u".avi",u".120.avi"))
134     delpath.append(pin.replace(u".avi",u".sa.avi"))
135     delpath.append(pin.replace(u".avi",u".ts"))
136     delpath.append(pin.replace(u".avi",u".aac"))
137     delpath.append(pin.replace(u".avi",u".m2v"))
138     delpath.append(pin.replace(u".avi",u".120_video.264"))
139     delpath.append(pin.replace(u".avi",u".ts.b25"))
140     if os.path.exists(pout):
141         if os.path.exists(audiopath):
142             os.remove(audiopath)
143         if os.path.exists(videopath):
144             os.remove(videopath)
145         """
146         for dp in delpath:
147             try:
148                 os.remove(dp)
149             except:
150                 ""
151         """
152 def mkv2mp4(pin,pout):
153     exeb = configreader.getpath(u"mkvextract")
154     exe = configreader.getpath(u"MP4Box")
155     dtsedit=configreader.getpath("DtsEdit")
156     wineexe=configreader.getpath("wine")
157     dir=os.path.split(pin)[0]
158     title=os.path.split(pin)[1]
159     title=os.path.splitext(title)[0]
160     etitle=base64.b16encode(title.encode('utf-8'))
161     audiopath=os.path.join(dir,etitle+u"_audio.aac")
162     videopath=os.path.join(dir,etitle+u"_video.264")
163     timecodepath=os.path.join(dir,etitle+u"_1_timecode.txt")
164     tmpmp4=os.path.join(dir,etitle+u".tmp.mp4")
165     exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'"
166     exe1=exeb+u" timecodes_v2 \'"+pin+"\' 1:\'"+timecodepath+"\'"
167     exe2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+tmpmp4+u"\'"
168     exe3=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+timecodepath+u"\' \'Z:\\"+tmpmp4+u"\' -o \'Z:\\"+pout+u"\'"
169     logo=pin.replace(".avi",".log")
170     os.environ['LANG']="ja_JP.UTF-8"
171     txt= exe0+"\n"+exe1+"\n"+exe2+"\n"+exe3+"\n"
172     try:
173         txt = txt+unicode(commands.getoutput(exe0.encode('utf-8')),'utf-8')
174     except:
175         ""
176     try:
177         txt = txt+unicode(commands.getoutput(exe1.encode('utf-8')),'utf-8')
178     except:
179         ""
180     try:
181         txt = txt+unicode(commands.getoutput(exe2.encode('utf-8')),'utf-8')
182     except:
183         ""
184     try:
185         os.system(exe3.encode('utf-8'))
186     except:
187         ""
188     txt = "\n####MKV2MP4-log####\n"+txt
189     f=open(logo,'a')
190     f.write(txt.encode('utf-8'))
191     f.close()
192     time.sleep(10)
193     if os.path.exists(pout):
194         if os.path.exists(audiopath):
195             os.remove(audiopath)
196         if os.path.exists(videopath):
197             os.remove(videopath)
198         if os.path.exists(timecodepath):
199             os.remove(timecodepath)
200         if os.path.exists(tmpmp4):
201             os.remove(tmpmp4)