OSDN Git Service

implement neroaac encoding.
[rec10/rec10-git.git] / rec10 / trunk / src / tv2mp4.py
1 #!/usr/bin/python\r
2 # coding: UTF-8\r
3 # Rec10 TS Recording Tools\r
4 # Copyright (C) 2009-2010 Yukikaze\r
5 import commands\r
6 import shutil\r
7 import tv2avi\r
8 import os\r
9 import re\r
10 import os.path\r
11 import base64\r
12 import time\r
13 import recdblist\r
14 import configreader\r
15 \r
16 def ts2mp4(pin, pout, opt):\r
17     dir=os.path.split(pout)[0]\r
18     title=os.path.split(pout)[1]\r
19     title=os.path.splitext(title)[0]\r
20     tpavi=os.path.join(dir, title+".avi")\r
21     tv2avi.ts2avi(pin, tpavi, opt)\r
22     time.sleep(10)\r
23     if not os.path.exists(tpavi):\r
24         if os.path.exists(pin):\r
25             tpavi=pin\r
26     avi2mp4(tpavi,pout,opt)\r
27 def avi2mp4(pin,pout,opt):\r
28     isvfr=0\r
29     if re.search(u"v", opt):\r
30         isvfr=1\r
31     exe = configreader.getpath(u"MP4Box")\r
32     dir=os.path.split(pin)[0]\r
33     title=os.path.split(pin)[1]\r
34     title=os.path.splitext(title)[0]\r
35     etitle=base64.b16encode(title.encode('utf-8'))\r
36     audiopath=os.path.join(dir,etitle+u"_audio.raw")\r
37     videopath=os.path.join(dir,etitle+u"_video.264")\r
38     exes=[]\r
39     os.environ['LANG']="ja_JP.UTF-8"\r
40     txt=""\r
41     if isvfr==1:\r
42         wineexe=configreader.getpath("wine")\r
43         cfr2tc=configreader.getpath("cfr2tc")\r
44         dtsedit=configreader.getpath("DtsEdit")\r
45         caption2ass=configreader.getpath("Caption2Ass")\r
46         pin120=os.path.join(dir,etitle+u".120.avi")\r
47         pints=os.path.join(dir,title+u".ts")\r
48         pmp4=os.path.join(dir,etitle+u".tmp.mp4")\r
49         pintimecode=os.path.join(dir,etitle+u".timecode.txt")\r
50         pinaac=os.path.join(dir,etitle+u".aac")\r
51         pincat=os.path.join(dir,etitle+u".srt")\r
52         evfr0="ffmpeg -i '"+pints+"' -vn -f aac -acodec copy '"+pinaac+"'"\r
53         evfr1=wineexe+u" "+cfr2tc+u" '"+"Z:\\"+pin+"' '"+"Z:\\"+pin120+u"' 'Z:\\"+pintimecode+u"' 2"\r
54         evfr2=exe+u" -aviraw video \'"+pin120+u"\'"\r
55         videotpath=os.path.join(dir,etitle+u".120_video.h264")\r
56         evfr3=exe+u" -fps 29.970030 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+pmp4+"\'"\r
57         evfr4=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+pintimecode+u"\' \'Z:\\"+pmp4+"\' -o \'Z:\\"+pout+"\'"\r
58         try:\r
59             recdblist.printutf8(evfr0)\r
60             txt=txt+evfr0+"\n"\r
61             txt=txt+unicode(commands.getoutput(evfr0.encode('utf-8')),'utf-8')\r
62         except:\r
63             ""\r
64         try:\r
65             recdblist.printutf8(evfr1)\r
66             txt=txt+evfr1+"\n"\r
67             txt=txt+unicode(commands.getoutput(evfr1.encode('utf-8')),'utf-8')\r
68         except:\r
69             ""\r
70         try:\r
71             if not os.path.exists(pin120):\r
72                 evfr2=exe+u" -aviraw video \'"+pin+u"\'"\r
73                 videotpath=os.path.join(dir,etitle+u"_video.h264")\r
74                 evfr3=exe+u" -fps 29.970030 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+pmp4+"\'"\r
75             recdblist.printutf8(evfr2)\r
76             txt=txt+evfr2+"\n"\r
77             txt=txt+unicode(commands.getoutput(evfr2.encode('utf-8')),'utf-8')\r
78         except:\r
79             ""\r
80         try:\r
81             recdblist.printutf8(evfr3)\r
82             txt=txt+evfr3+"\n"\r
83             txt=txt+unicode(commands.getoutput(evfr3.encode('utf-8')),'utf-8')\r
84         except:\r
85             ""\r
86         try:\r
87             recdblist.printutf8(evfr4)\r
88             txt=txt+evfr4+"\n"\r
89             txt=txt+unicode(commands.getoutput(evfr4.encode('utf-8')),'utf-8')\r
90         except:\r
91             ""\r
92         if (not os.path.exists(pin.replace(".avi",".120.avi"))and (not os.path.exists(pout))):\r
93             txt=txt+"CFR2TC error. maybe avi file is over 1GB.(ODML error would have happend.)"\r
94             ptmp=pin.replace(".avi",".noodml.avi")\r
95             e="mencoder -noodml -forceidx -oac copy -ovc copy -o \'"+ptmp+"\'"+" \'"+pin+"\'"\r
96             ex="nice -n 17 "+e\r
97             e2=wineexe+u" "+cfr2tc+u" '"+"Z:\\"+ptmp+"' '"+"Z:\\"+pin120+u"' '"+"Z:\\"+pintimecode+u"' 2"\r
98             ex2="nice -n 17 "+e2\r
99             e3=exe+u" -aviraw video \'"+pin120+u"\'"\r
100             ex3="nice -n 17 "+e3\r
101             e4=exe+u" -fps 29.97 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+pmp4+"\'"\r
102             ex4="nice -n 17 "+e4\r
103             e5=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+pintimecode+u"\' \'Z:\\"+pmp4+"\' -o \'Z:\\"+pout+"\'"\r
104             ex5="nice -n 17 "+e5\r
105             recdblist.printutf8(e)\r
106             txt=txt+e+"\n"\r
107             try:\r
108                 cmdt=unicode(commands.getoutput(ex.encode('utf-8')),'utf-8')\r
109             except:\r
110                 ""\r
111             try:\r
112                 cmdt=unicode(commands.getoutput(ex2.encode('utf-8')),'utf-8')\r
113             except:\r
114                 ""\r
115             try:\r
116                 if os.path.exists(pin120):\r
117                     cmdt=unicode(commands.getoutput(ex3.encode('utf-8')),'utf-8')\r
118             except:\r
119                 ""\r
120             try:\r
121                 cmdt=unicode(commands.getoutput(ex4.encode('utf-8')),'utf-8')\r
122             except:\r
123                 ""\r
124             try:\r
125                 cmdt=unicode(commands.getoutput(ex5.encode('utf-8')),'utf-8')\r
126             except:\r
127                 ""\r
128             try:\r
129                 os.remove(ptmp)\r
130             except:\r
131                 ""\r
132             txt=txt+cmdt+"\n"\r
133     else:\r
134         try:\r
135             shutil.move(pin,os.path.join(dir,etitle+".avi"))\r
136         except:\r
137             ""\r
138         if os.path.exists(os.path.join(dir,etitle+".avi")):\r
139             e0=exe+u" -aviraw audio \'"+os.path.join(dir,etitle+".avi")+u"\'"\r
140             e1=exe+u" -aviraw video \'"+os.path.join(dir,etitle+".avi")+u"\'"\r
141             os.system(e0)\r
142             os.system(e1)\r
143         if not os.path.exists(audiopath):\r
144             if os.path.exists(os.path.join(dir,etitle+u"_audio.mp3")):\r
145                 audiopath=os.path.join(dir,etitle+u"_audio.mp3")\r
146             elif os.path.exists(os.path.join(dir,etitle+u"_audio.aac")):\r
147                 audiopath=os.path.join(dir,etitle+u"_audio.aac")\r
148         else:\r
149             shutil.move(audiopath,os.path.join(dir,etitle+u"_audio.aac"))\r
150             audiopath=os.path.join(dir,etitle+u"_audio.aac")\r
151         ptmp=os.path.exists(os.path.join(dir,etitle+u".mp4"))\r
152         e2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+ptmp+u"\'"\r
153         os.system(e2)\r
154         shutil.move(ptmp,pout)\r
155     logo=pin.replace(u".avi",u".log")\r
156     txt = "\n####MP4Box-log####\n"+txt\r
157     f=open(logo,'a')\r
158     f.write(txt.encode('utf-8'))\r
159     f.close()\r
160     time.sleep(10)\r
161     delpath=[]\r
162     delpath.append(pin.replace(u".avi",u".120.avi"))\r
163     delpath.append(pin.replace(u".avi",u".sa.avi"))\r
164     delpath.append(pin.replace(u".avi",u".m2v"))\r
165     delpath.append(pin.replace(u".avi",u"_1.aac"))\r
166     delpath.append(pin.replace(u".avi",u"_2.aac"))\r
167     delpath.append(pin.replace(u".avi",u"_1.mp3"))\r
168     delpath.append(pin.replace(u".avi",u"_2.mp3"))\r
169     delpath.append(pin.replace(u".avi",u".ts"))\r
170     delpath.append(pin.replace(u".avi",u".aac"))\r
171     delpath.append(pin.replace(u".avi",u".m2v"))\r
172     delpath.append(pin.replace(u".avi",u".120_video.264"))\r
173     delpath.append(pin.replace(u".avi",u".ts.b25"))\r
174     if os.path.exists(pout):\r
175         if os.path.exists(audiopath):\r
176             os.remove(audiopath)\r
177         if os.path.exists(videopath):\r
178             os.remove(videopath)\r
179         """\r
180         for dp in delpath:\r
181             try:\r
182                 os.remove(dp)\r
183             except:\r
184                 ""\r
185         """\r
186 def mkv2mp4(pin,pout):\r
187     exeb = configreader.getpath(u"mkvextract")\r
188     exe = configreader.getpath(u"MP4Box")\r
189     dtsedit=configreader.getpath("DtsEdit")\r
190     wineexe=configreader.getpath("wine")\r
191     dir=os.path.split(pin)[0]\r
192     title=os.path.split(pin)[1]\r
193     title=os.path.splitext(title)[0]\r
194     etitle=base64.b16encode(title.encode('utf-8'))\r
195     audiopath=os.path.join(dir,etitle+u"_audio.aac")\r
196     videopath=os.path.join(dir,etitle+u"_video.264")\r
197     timecodepath=os.path.join(dir,etitle+u"_1_timecode.txt")\r
198     tmpmp4=os.path.join(dir,etitle+u".tmp.mp4")\r
199     exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'"\r
200     exe1=exeb+u" timecodes_v2 \'"+pin+"\' 1:\'"+timecodepath+"\'"\r
201     exe2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+tmpmp4+u"\'"\r
202     exe3=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+timecodepath+u"\' \'Z:\\"+tmpmp4+u"\' -o \'Z:\\"+pout+u"\'"\r
203     logo=pin.replace(".avi",".log")\r
204     os.environ['LANG']="ja_JP.UTF-8"\r
205     txt=""\r
206     execcomd(exe0)\r
207     execcomd(exe1)\r
208     execcomd(exe2)\r
209     execcomd(exe3)\r
210     txt = "\n####MKV2MP4-log####\n"+txt\r
211     f=open(logo,'a')\r
212     f.write(txt.encode('utf-8'))\r
213     f.close()\r
214     time.sleep(10)\r
215     if os.path.exists(pout):\r
216         if os.path.exists(audiopath):\r
217             os.remove(audiopath)\r
218         if os.path.exists(videopath):\r
219             os.remove(videopath)\r
220         if os.path.exists(timecodepath):\r
221             os.remove(timecodepath)\r
222         if os.path.exists(tmpmp4):\r
223             os.remove(tmpmp4)\r
224 def execcomd(cmd):\r
225     txt=""\r
226     try:\r
227         txt=u"Cmd : "+cmd+"\n"\r
228         txt2=commands.getoutput(cmd.encode('utf-8'))\r
229         txt=txt+unicode(txt2,"utf-8")+"\n"\r
230     except:\r
231         ""\r
232     return txt\r