OSDN Git Service

41959a53e7b9b81378876af743ff9eafddd541e9
[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 tv2avi
7 import os
8 import re
9 import os.path
10 import time
11 import recdblist
12 import configreader
13
14 def ts2mp4(pin, pout, opt):
15     dir=os.path.split(pout)[0]
16     title=os.path.split(pout)[1]
17     title=os.path.splitext(title)[0]
18     tpavi=os.path.join(dir, title+".avi")
19     tv2avi.ts2avi(pin, tpavi, opt)
20     time.sleep(10)
21     if not os.path.exists(tpavi):
22         if os.path.exists(pin):
23             tpavi=pin
24     avi2mp4(tpavi,pout,opt)
25 def avi2mp4(pin,pout,opt):
26     isvfr=0
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.970030 -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         if os.path.exists(audiopath):
92             if os.path.exists(os.path.join(dir,title+u"_audio.mp3")):
93                 audiopath=os.path.join(dir,title+u"_audio.mp3")
94             elif os.path.exists(os.path.join(dir,title+u"_audio.aac")):
95                 audiopath=os.path.join(dir,title+u"_audio.aac")
96         exes.append(exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+pout+u"\'")
97         for e in exes:
98             recdblist.printutf8(e)
99             txt=txt+e+"\n"
100             try:
101                 txt=txt+unicode(commands.getoutput(e.encode('utf-8')),'utf-8')
102             except:
103                 ""
104     logo=pin.replace(u".avi",u".log")
105     txt = "\n####MP4Box-log####\n"+txt
106     f=open(logo,'a')
107     f.write(txt.encode('utf-8'))
108     f.close()
109     time.sleep(10)
110     delpath=[]
111     delpath.append(pin.replace(u".avi",u".120.avi"))
112     delpath.append(pin.replace(u".avi",u".sa.avi"))
113     delpath.append(pin.replace(u".avi",u".ts"))
114     delpath.append(pin.replace(u".avi",u".aac"))
115     delpath.append(pin.replace(u".avi",u".m2v"))
116     delpath.append(pin.replace(u".avi",u".120_video.h264"))
117     delpath.append(pin.replace(u".avi",u".ts.b25"))
118     if os.path.exists(pout):
119         if os.path.exists(audiopath):
120             os.remove(audiopath)
121         if os.path.exists(videopath):
122             os.remove(videopath)
123         """
124         for dp in delpath:
125             try:
126                 os.remove(dp)
127             except:
128                 ""
129         """
130 def mkv2mp4(pin,pout):
131     exeb = configreader.getpath(u"mkvextract")
132     exe = configreader.getpath(u"MP4Box")
133     dtsedit=configreader.getpath("DtsEdit")
134     wineexe=configreader.getpath("wine")
135     dir=os.path.split(pin)[0]
136     title=os.path.split(pin)[1]
137     title=os.path.splitext(title)[0]
138     audiopath=os.path.join(dir,title+u"_audio.raw")
139     videopath=os.path.join(dir,title+u"_video.h264")
140     timecodepath=os.path.join(dir,title+u"_1_timecode.txt")
141     tmpmp4=os.path.join(dir,title+u".tmp.mp4")
142     exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'"
143     exe1=exeb+u" timecodes_v2 \'"+pin+"\' 1:\'"+timecodepath+"\'"
144     exe2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+tmpmp4+u"\'"
145     exe3=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+timecodepath+u"\' \'Z:\\"+tmpmp4+u"\' -o \'Z:\\"+pout+u"\'"
146     logo=pin.replace(".avi",".log")
147     os.environ['LANG']="ja_JP.UTF-8"
148     txt= exe0+"\n"+exe1+"\n"+exe2+"\n"+exe3+"\n"
149     try:
150         txt = txt+unicode(commands.getoutput(exe0.encode('utf-8')),'utf-8')
151         txt = txt+unicode(commands.getoutput(exe1.encode('utf-8')),'utf-8')
152         txt = txt+unicode(commands.getoutput(exe2.encode('utf-8')),'utf-8')
153         txt = txt+unicode(commands.getoutput(exe3.encode('utf-8')),'utf-8')
154     except:
155         ""
156     os.remove(audiopath)
157     os.remove(videopath)
158     txt = "\n####MKV2MP4-log####\n"+txt
159     f=open(logo,'a')
160     f.write(txt.encode('utf-8'))
161     f.close()
162     time.sleep(10)
163     if os.path.exists(pout):
164         if os.path.exists(audiopath):
165             os.remove(audiopath)
166         if os.path.exists(videopath):
167             os.remove(videopath)