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     audiopath=os.path.join(dir,title+u"_audio.raw")
36     videopath=os.path.join(dir,title+u"_video.264")
37     exes=[]
38     os.environ['LANG']="ja_JP.UTF-8"
39     txt=""
40     if isvfr==1:
41         wineexe=configreader.getpath("wine")
42         cfr2tc=configreader.getpath("cfr2tc")
43         dtsedit=configreader.getpath("DtsEdit")
44         pin120=pin.replace(".avi",".120.avi")
45         pints=pin.replace(".avi",".ts")
46         pmp4=pin.replace(".avi",".tmp.mp4")
47         pintimecode=pin.replace(".avi",".timecode.txt")
48         pinaac=pin.replace(".avi",".aac")
49         evfr=[]
50         evfr.append("ffmpeg -i '"+pints+"' -vn -f aac -acodec copy '"+pinaac+"'")
51         evfr.append(wineexe+u" "+cfr2tc+u" '"+"Z:\\"+pin+"' '"+"Z:\\"+pin120+u"' 'Z:\\"+pintimecode+u"' 2")
52         evfr.append(exe+u" -aviraw video \'"+pin120+u"\'")
53         videotpath=os.path.join(dir,title+u".120_video.264")
54         evfr.append(exe+u" -fps 29.970030 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+pmp4+"\'")
55         #evfr.append(exe+u" -add \'"+pinaac+"\' \'"+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                 cmdt=cmdt+unicode(commands.getoutput(ex2.encode('utf-8')),'utf-8')
83                 cmdt=cmdt+unicode(commands.getoutput(ex3.encode('utf-8')),'utf-8')
84                 cmdt=cmdt+unicode(commands.getoutput(ex4.encode('utf-8')),'utf-8')
85                 cmdt=cmdt+unicode(commands.getoutput(ex5.encode('utf-8')),'utf-8')
86                 os.remove(ptmp)
87             except:
88                 ""
89             txt=txt+cmdt+"\n"
90     else:
91         exes.append(exe+u" -aviraw audio \'"+pin+u"\'")
92         exes.append(exe+u" -aviraw video \'"+pin+u"\'")
93         if os.path.exists(audiopath):
94             if os.path.exists(os.path.join(dir,title+u"_audio.mp3")):
95                 audiopath=os.path.join(dir,title+u"_audio.mp3")
96             elif os.path.exists(os.path.join(dir,title+u"_audio.aac")):
97                 audiopath=os.path.join(dir,title+u"_audio.aac")
98         exes.append(exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+pout+u"\'")
99         for e in exes:
100             recdblist.printutf8(e)
101             txt=txt+e+"\n"
102             try:
103                 txt=txt+unicode(commands.getoutput(e.encode('utf-8')),'utf-8')
104             except:
105                 ""
106     logo=pin.replace(u".avi",u".log")
107     txt = "\n####MP4Box-log####\n"+txt
108     f=open(logo,'a')
109     f.write(txt.encode('utf-8'))
110     f.close()
111     time.sleep(10)
112     delpath=[]
113     delpath.append(pin.replace(u".avi",u".120.avi"))
114     delpath.append(pin.replace(u".avi",u".sa.avi"))
115     delpath.append(pin.replace(u".avi",u".ts"))
116     delpath.append(pin.replace(u".avi",u".aac"))
117     delpath.append(pin.replace(u".avi",u".m2v"))
118     delpath.append(pin.replace(u".avi",u".120_video.264"))
119     delpath.append(pin.replace(u".avi",u".ts.b25"))
120     if os.path.exists(pout):
121         if os.path.exists(audiopath):
122             os.remove(audiopath)
123         if os.path.exists(videopath):
124             os.remove(videopath)
125         """
126         for dp in delpath:
127             try:
128                 os.remove(dp)
129             except:
130                 ""
131         """
132 def mkv2mp4(pin,pout):
133     exeb = configreader.getpath(u"mkvextract")
134     exe = configreader.getpath(u"MP4Box")
135     dtsedit=configreader.getpath("DtsEdit")
136     wineexe=configreader.getpath("wine")
137     dir=os.path.split(pin)[0]
138     title=os.path.split(pin)[1]
139     title=os.path.splitext(title)[0]
140     etitle=base64.b16encode(title.encode('utf-8'))
141     audiopath=os.path.join(dir,etitle+u"_audio.raw")
142     videopath=os.path.join(dir,etitle+u"_video.264")
143     timecodepath=os.path.join(dir,etitle+u"_1_timecode.txt")
144     tmpmp4=os.path.join(dir,etitle+u".tmp.mp4")
145     tmpmp42=os.path.join(dir,etitle+u".mp4")
146     exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'"
147     exe1=exeb+u" timecodes_v2 \'"+pin+"\' 1:\'"+timecodepath+"\'"
148     exe2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -new \'"+tmpmp4+u"\'"
149     exe3=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+timecodepath+u"\' \'Z:\\"+tmpmp4+u"\' -o \'Z:\\"+tmpmp42+u"\'"
150     exe4=exe+u" -add \'"+audiopath+u"\' \'"+tmpmp42+u"\'"
151     logo=pin.replace(".avi",".log")
152     os.environ['LANG']="ja_JP.UTF-8"
153     txt= exe0+"\n"+exe1+"\n"+exe2+"\n"+exe3+"\n"
154     try:
155         txt = txt+unicode(commands.getoutput(exe0.encode('utf-8')),'utf-8')
156     except:
157         ""
158     try:
159         txt = txt+unicode(commands.getoutput(exe1.encode('utf-8')),'utf-8')
160     except:
161         ""
162     try:
163         txt = txt+unicode(commands.getoutput(exe2.encode('utf-8')),'utf-8')
164     except:
165         ""
166     try:
167         commands.getoutput(exe3.encode('utf-8'))
168     except:
169         ""
170     try:
171         txt = txt+unicode(commands.getoutput(exe4.encode('utf-8')),'utf-8')
172     except:
173         ""
174     shutil.move(tmpmp42,pout)
175     txt = "\n####MKV2MP4-log####\n"+txt
176     f=open(logo,'a')
177     f.write(txt.encode('utf-8'))
178     f.close()
179     time.sleep(10)
180     if os.path.exists(pout):
181         if os.path.exists(audiopath):
182             os.remove(audiopath)
183         if os.path.exists(videopath):
184             os.remove(videopath)
185         if os.path.exists(timecodepath):
186             os.remove(timecodepath)
187         if os.path.exists(tmpmp4):
188             os.remove(tmpmp4)
189         if os.path.exists(tmpmp42):
190             os.remove(tmpmp42)