OSDN Git Service

implement vfr mp4 making function.
[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 sys
6 import commands
7 import tv2avi
8 import os
9 import re
10 import os.path
11 import time
12 import recdblist
13 import configreader
14
15 def ts2mp4(pin, pout, opt):
16     dir=os.path.split(pout)[0]
17     title=os.path.split(pout)[1]
18     title=os.path.splitext(title)[0]
19     tpavi=os.path.join(dir, title+".avi")
20     tv2avi.ts2avi(pin, tpavi, opt)
21     time.sleep(10)
22     if not os.path.exists(tpavi):
23         if os.path.exists(pin):
24             tpavi=pin
25     avi2mp4(tpavi,pout,opt)
26 def avi2mp4(pin,pout,opt):
27     if re.search(u"v", opt):
28         isvfr=1
29     exe = configreader.getpath(u"MP4Box")
30     dir=os.path.split(pin)[0]
31     title=os.path.split(pin)[1]
32     title=os.path.splitext(title)[0]
33     audiopath=os.path.join(dir,title+u"_audio.raw")
34     videopath=os.path.join(dir,title+u"_video.h264")
35     exes=[]
36     os.environ['LANG']="ja_JP.UTF-8"
37     txt=""
38     if isvfr==1:
39         wineexe=configreader.getpath("wine")
40         cfr2tc=configreader.getpath("cfr2tc")
41         dtsedit=configreader.getpath("DtsEdit")
42         pin120=pin.replace(".avi",".120.avi")
43         pints=pin.replace(".avi",".ts")
44         pmp4=pin.replace(".avi",".tmp.mp4")
45         pintimecode=pin.replace(".avi",".timecode.txt")
46         pinaac=pin.replace(".avi",".aac")
47         evfr=[]
48         evfr.append("ffmpeg -i '"+pints+"' -vn -f aac -acodec copy '"+pinaac+"'")
49         evfr.append(wineexe+u" "+cfr2tc+u" '"+"Z:\\"+pin+"' '"+"Z:\\"+pin120+u"' 'Z:\\"+pintimecode+u"' 2")
50         evfr.append(exe+u" -aviraw video \'"+pin120+u"\'")
51         videotpath=os.path.join(dir,title+u".120_video.h264")
52         evfr.append(exe+u" -fps 29.97 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+pmp4+"\'")
53         #evfr.append(exe+u" -add \'"+pinaac+"\' \'"+pmp4+"\'")
54         evfr.append(wineexe+u" "+dtsedit+u" -tc \'Z:\\"+pintimecode+u"\' \'Z:\\"+pmp4+"\' -o \'Z:\\"+pout+"\'")
55         exes=evfr
56         for e in exes:
57             recdblist.printutf8(e)
58             txt=txt+e+"\n"
59             try:
60                 txt=txt+unicode(commands.getoutput(e.encode('utf-8')),'utf-8')
61             except:
62                 ""
63         if (not os.path.exists(pin.replace(".avi",".120.avi"))and (not os.path.exists(pout))):
64             txt=txt+"CFR2TC error. maybe avi file is over 1GB.(ODML error would have happend.)"
65             ptmp=pin.replace(".avi",".noodml.avi")
66             e="mencoder -noodml -forceidx -oac copy -ovc copy -o \'"+ptmp+"\'"+" \'"+pin+"\'"
67             ex="nice -n 17 "+e
68             e2=wineexe+u" "+cfr2tc+u" '"+"Z:\\"+ptmp+"' '"+"Z:\\"+pin120+u"' '"+"Z:\\"+pintimecode+u"' 2"
69             ex2="nice -n 17 "+e2
70             e3=exe+u" -aviraw video \'"+pin120+u"\'"
71             ex3="nice -n 17 "+e3
72             e4=exe+u" -fps 29.97 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+pmp4+"\'"
73             ex4="nice -n 17 "+e4
74             e5=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+pintimecode+u"\' \'Z:\\"+pmp4+"\' -o \'Z:\\"+pout+"\'"
75             ex5="nice -n 17 "+e5
76             recdblist.printutf8(e)
77             txt=txt+e+"\n"
78             try:
79                 cmdt=unicode(commands.getoutput(ex.encode('utf-8')),'utf-8')
80                 cmdt=cmdt+unicode(commands.getoutput(ex2.encode('utf-8')),'utf-8')
81                 cmdt=cmdt+unicode(commands.getoutput(ex3.encode('utf-8')),'utf-8')
82                 cmdt=cmdt+unicode(commands.getoutput(ex4.encode('utf-8')),'utf-8')
83                 cmdt=cmdt+unicode(commands.getoutput(ex5.encode('utf-8')),'utf-8')
84                 os.remove(ptmp)
85             except:
86                 ""
87             txt=txt+cmdt+"\n"
88     else:
89         exes.append(exe+u" -aviraw audio \'"+pin+u"\'")
90         exes.append(exe+u" -aviraw video \'"+pin+u"\'")
91         exes.append(exe+u" -new -fps 29.97 -add \'"+videopath+u"\'#video -add \'"+audiopath+u"\'#audio \'"+pout+u"\'")
92         for e in exes:
93             recdblist.printutf8(e)
94             txt=txt+e+"\n"
95             try:
96                 txt=txt+unicode(commands.getoutput(e.encode('utf-8')),'utf-8')
97             except:
98                 ""
99     logo=pin.replace(u".avi",u".log")
100     txt = "\n####MP4Box-log####\n"+txt
101     f=open(logo,'a')
102     f.write(txt.encode('utf-8'))
103     f.close()
104     time.sleep(10)
105     delpath=[]
106     delpath.append(pin.replace(u".avi",u".120.avi"))
107     delpath.append(pin.replace(u".avi",u".sa.avi"))
108     delpath.append(pin.replace(u".avi",u".ts"))
109     delpath.append(pin.replace(u".avi",u".aac"))
110     delpath.append(pin.replace(u".avi",u".m2v"))
111     delpath.append(pin.replace(u".avi",u".120_video.264"))
112     delpath.append(pin.replace(u".avi",u".ts.b25"))
113     if os.path.exists(pout):
114         if os.path.exists(audiopath):
115             os.remove(audiopath)
116         if os.path.exists(videopath):
117             os.remove(videopath)
118         """
119         for dp in delpath:
120             try:
121                 os.remove(dp)
122             except:
123                 ""
124         """
125 def mkv2mp4(pin,pout):
126     exeb = configreader.getpath(u"mkvextract")
127     exe = configreader.getpath(u"MP4Box")
128     dir=os.path.split(pin)[0]
129     title=os.path.split(pin)[1]
130     title=os.path.splitext(title)[0]
131     audiopath=os.path.join(dir,title+u"_audio.mp3")
132     if not os.path.exists(audiopath):
133         if os.path.exists(os.path.join(dir,title+u"_audio.raw")):
134             audiopath=os.path.join(dir,title+u"_audio.raw")
135         if os.path.exists(os.path.join(dir,title+u"_audio.aac")):
136             audiopath=os.path.join(dir,title+u"_audio.aac")
137     videopath=os.path.join(dir,title+u"_video.h264")
138     #doexe = exe.encode('utf-8') + u' \"' + pin.encode('utf-8') + u'\" \"' + pout.encode('utf-8') + u'\"'
139     exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'"
140     exe1=exe+u" -aviraw audio \'"+pin+u"\'"
141     exe2=exe+u" -aviraw video \'"+pin+u"\'"
142     exefin=exe+u" -new -fps 29.970030 -add \'"+videopath+u"\'#video -add \'"+audiopath+u"\'#audio \'"+pout+u"\'"
143     logo=pin.replace(".avi",".log")
144     os.environ['LANG']="ja_JP.UTF-8"
145     txt= exe0+"\n"+exe1+"\n"+exe2+"\n"+exefin+"\n"
146     txt = txt+unicode(commands.getoutput(exe0.encode('utf-8')),'utf-8')
147     txt = txt+unicode(commands.getoutput(exe1.encode('utf-8')),'utf-8')
148     txt = txt+unicode(commands.getoutput(exe2.encode('utf-8')),'utf-8')
149     txt = txt+unicode(commands.getoutput(exefin.encode('utf-8')),'utf-8')
150     os.remove(audiopath)
151     os.remove(videopath)
152     txt = "\n####MKV2MP4-log####\n"+txt
153     f=open(logo,'a')
154     f.write(txt.encode('utf-8'))
155     f.close()
156     time.sleep(10)
157     if os.path.exists(pout):
158         if os.path.exists(audiopath):
159             os.remove(audiopath)
160         if os.path.exists(videopath):
161             os.remove(videopath)