OSDN Git Service

fix some bug.
[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 string\r
12 import base64\r
13 import time\r
14 import recdblist\r
15 import configreader\r
16 import status\r
17 \r
18 def ts2mp4(pin, pout, opt):\r
19     dir=os.path.split(pout)[0]\r
20     title=os.path.split(pout)[1]\r
21     title=os.path.splitext(title)[0]\r
22     tpraw=os.path.join(dir, title+".264")\r
23     tpmp4=os.path.join(dir, title+".mp4")\r
24     if os.path.isfile(pin) and os.path.getsize(pin)>10*1000:\r
25         tv2avi.ts2raw(pin, tpraw, opt)\r
26         time.sleep(10)\r
27         if os.path.isfile(tpraw) and os.path.getsize(tpraw)>10*1000:\r
28             raw2mp4(tpraw, tpmp4, opt)\r
29 def raw2mp4(pin,pout,opt):\r
30     dir=os.path.split(pout)[0]\r
31     title=os.path.split(pout)[1]\r
32     title=os.path.splitext(title)[0]\r
33     duration="-fps 29.970030 "\r
34     ffmpeg=configreader.getpath("ffmpeg")\r
35     isdualaac=0\r
36     ispentaaudio=0\r
37     if re.search("a",opt):\r
38         duration="-fps 23.976023 "\r
39     elif re.search("v",opt):\r
40         duration="-fps 23.976023 "\r
41     if re.search("d",opt):\r
42         isdualaac=1\r
43         duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置\r
44     elif re.search("5",opt):\r
45         ispentaaudio=1\r
46         duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置\r
47     exe = configreader.getpath("mp4box")\r
48     txt=""\r
49     os.environ['LANG']="ja_JP.UTF-8"\r
50     if isdualaac==1:\r
51         wineexe=configreader.getpath("wine")\r
52         pincap=pin.replace(".264",".srt")\r
53         cap2ass=configreader.getpath("caption2ass")\r
54         pints=pin.replace(".264",".ts")\r
55         paac1=pin.replace(".264","_1.aac")\r
56         paac2=pin.replace(".264","_2.aac")\r
57         recdblist.printutf8(paac1)\r
58         if not os.path.exists(paac1):\r
59             paac1=pin.replace(".264","_1.mp3")\r
60         if not os.path.exists(paac2):\r
61             paac2=pin.replace(".264","_2.mp3")\r
62         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""\r
63         e1=exe +u" "+duration+" -add \""+pin+"\" -new \""+pout+"\""\r
64         e1a1=exe +u" -add \""+paac1+"\" \""+pout+"\""\r
65         e1a2=exe +u" -add \""+paac2+"\" \""+pout+"\""\r
66         e1s=exe +u" -add \""+pincap+"\" \""+pout+"\""\r
67         if os.path.isfile(cap2ass):\r
68             txt=txt+execcomd(e0)\r
69         execmp4box(pin, pout, e1)\r
70         addmp4(paac1, pout, e1a1)\r
71         addmp4(paac2, pout, e1a2)\r
72         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):\r
73             addmp4(pincap, pout, e1s)\r
74     elif ispentaaudio==1:\r
75         wineexe=configreader.getpath("wine")\r
76         pincap=pin.replace(".264",".srt")\r
77         cap2ass=configreader.getpath("caption2ass")\r
78         pints=pin.replace(".264",".ts")\r
79         paac1=pin.replace(".264","_1.aac")\r
80         paac2=pin.replace(".264","_2.aac")\r
81         recdblist.printutf8(paac1)\r
82         if not os.path.exists(paac1):\r
83             paac1=pin.replace(".264","_1.mp3")\r
84         if not os.path.exists(paac2):\r
85             paac2=pin.replace(".264","_2.mp3")\r
86         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""\r
87         e1=exe +u" "+duration+" -add \""+pin+"\" -new \""+pout+"\""\r
88         e1a1=exe +u" -add \""+paac1+"\" \""+pout+"\""\r
89         e1a2=exe +u" -add \""+paac2+"\" \""+pout+"\""\r
90         e1s=exe +u" -add \""+pincap+"\" \""+pout+"\""\r
91         if os.path.isfile(cap2ass):\r
92             txt=txt+execcomd(e0)\r
93         execmp4box(pin, pout, e1)\r
94         addmp4(paac1, pout, e1a1)\r
95         addmp4(paac2, pout, e1a2)\r
96         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):\r
97             addmp4(pincap, pout, e1s)\r
98     else:\r
99         wineexe=configreader.getpath("wine")\r
100         pincap=pin.replace(".264",".srt")\r
101         cap2ass=configreader.getpath("caption2ass")\r
102         pints=pin.replace(".264",".ts")\r
103         pinaac=pin.replace(".264",".aac")\r
104         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""\r
105         e1=ffmpeg+" -i \""+pints+"\" -vn -f aac -acodec copy \""+pinaac+"\""\r
106         e2=exe +u" "+duration+" -add \""+pin+"\" -new \""+pout+"\""\r
107         e2a=exe +u" -add \""+pinaac+"\" \""+pout+"\""\r
108         e2s=exe +u" -add \""+pincap+"\" \""+pout+"\""\r
109         if os.path.isfile(cap2ass):\r
110             txt=txt+execcomd(e0)\r
111         txt=txt+execcomd(e1)\r
112         execmp4box(pin, pout, e2)\r
113         addmp4(pinaac, pout, e2a)\r
114         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):\r
115             addmp4(pincap, pout, e2s)\r
116     recdblist.addlog(pout, txt, "mp4box-log")\r
117     if status.getSettings_auto_del_tmp()==1:\r
118         if os.path.exists(pout):\r
119             if os.path.getsize(pout)>100*1000*1000:\r
120                 deltitle(dir,title)\r
121 def mkv2mp4(pin,pout):\r
122     exeb = configreader.getpath(u"mkvextract")\r
123     exe = configreader.getpath(u"mp4Box")\r
124     #dtsedit=configreader.getpath("DtsEdit")\r
125     wineexe=configreader.getpath("wine")\r
126     dir=os.path.split(pin)[0]\r
127     title=os.path.split(pin)[1]\r
128     title=os.path.splitext(title)[0]\r
129     etitle=base64.b16encode(title.encode('utf-8'))\r
130     audiopath=os.path.join(dir,etitle+u"_audio.aac")\r
131     videopath=os.path.join(dir,etitle+u"_video.264")\r
132     timecodepath=os.path.join(dir,etitle+u"_1_timecode.txt")\r
133     tmpmp4=os.path.join(dir,etitle+u".tmp.mp4")\r
134     exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'"\r
135     exe1=exeb+u" timecodes_v2 \'"+pin+"\' 1:\'"+timecodepath+"\'"\r
136     exe2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+tmpmp4+u"\'"\r
137     exe3=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+timecodepath+u"\' \'Z:\\"+tmpmp4+u"\' -o \'Z:\\"+pout+u"\'"\r
138     os.environ['LANG']="ja_JP.UTF-8"\r
139     txt=""\r
140     try:\r
141         txt=txt+execcomd(exe0)+"\n"\r
142         txt=txt+execcomd(exe1)+"\n"\r
143         txt=txt+execcomd(exe2)+"\n"\r
144         txt=txt+execcomd(exe3)+"\n"\r
145     except:\r
146         ""\r
147     recdblist.addlog(pin, txt, "MKV2MP4-log")\r
148     txt = "\n####MKV2MP4-log####\n"+txt\r
149     time.sleep(10)\r
150     if status.getSettings_auto_del_tmp()==1:\r
151         if os.path.exists(pout):\r
152             if os.path.getsize(pout)>100*1000*1000:\r
153                 deltitle(dir,title)\r
154 def execmp4box(pin,pout,cmd):\r
155     title=os.path.splitext(os.path.split(pin)[1])[0]\r
156     ptin=os.path.join(os.path.dirname(pin),base64.b16encode(title.encode('utf-8'))+".264")\r
157     recdblist.printutf8(ptin)\r
158     shutil.move(pin,ptin)\r
159     time.sleep(10)\r
160     ptout=os.path.join(os.path.dirname(pout),base64.b16encode(title.encode('utf-8'))+".mp4")\r
161     cmdn=string.replace(cmd,pin,ptin)\r
162     cmdn=string.replace(cmdn,pout,ptout)\r
163     recdblist.printutf8(cmdn)\r
164     txt=""\r
165     try:\r
166         txt=execcomd(cmdn)\r
167     except Exception, inst:\r
168         txt= "error occures in execmp4box\n"\r
169         txt=txt+ str(type(inst))+"\n"\r
170         txt=txt+str(inst)\r
171     recdblist.addlog(pin, txt, "MP4Box-log")\r
172     time.sleep(5)\r
173     shutil.move(ptin,pin)\r
174     shutil.move(ptout,pout)\r
175     time.sleep(5)\r
176 def addmp4(padd,pout,cmd):#without video\r
177     title=os.path.splitext(os.path.split(padd)[1])[0]\r
178     ext=os.path.splitext(os.path.split(padd)[1])[1]\r
179     ptadd=os.path.join(os.path.dirname(padd),base64.b16encode(title.encode('utf-8'))+ext)\r
180     ptoutb=os.path.join(os.path.dirname(pout),base64.b16encode(title.encode('utf-8'))+"_b.mp4")\r
181     ptout=os.path.join(os.path.dirname(pout),base64.b16encode(title.encode('utf-8'))+".mp4")\r
182     shutil.move(padd,ptadd)\r
183     if os.path.isfile(pout):\r
184         shutil.move(pout,ptoutb)\r
185     time.sleep(5)\r
186     cmdn=string.replace(cmd,padd,ptadd)\r
187     cmdn=string.replace(cmdn,u"-out \""+pout,u"-add \""+ptoutb+"\" -new \""+ptout)\r
188     cmdn=string.replace(cmdn,u"\""+pout,u"-add \""+ptoutb+"\" -new \""+ptout)\r
189     cmdn=string.replace(cmdn,pout,ptout)\r
190     recdblist.printutf8(cmdn)\r
191     txt=""\r
192     try:\r
193         txt=execcomd(cmdn)\r
194     except Exception, inst:\r
195         txt= "error occures in addmp4\n"\r
196         txt=txt+ str(type(inst))+"\n"\r
197         txt=txt+str(inst)\r
198     recdblist.addlog(pout, txt, "MP4Box-log-add")\r
199     time.sleep(5)\r
200     os.remove(ptoutb)\r
201     shutil.move(ptadd,padd)\r
202     shutil.move(ptout,pout)\r
203     time.sleep(5)\r
204 def execcomd(cmd):\r
205     txt=""\r
206     try:\r
207         txt=u"Cmd : "+cmd+"\n"\r
208         txt2=commands.getoutput(cmd.encode('utf-8'))\r
209         txt=txt+unicode(txt2,"utf-8")+"\n"\r
210     except:\r
211         ""\r
212     return txt\r
213 \r
214 def deltitle(path,title):\r
215     dp=[]\r
216     dp.append(os.path.join(path,title+".avi"))\r
217     dp.append(os.path.join(path,title+".264"))\r
218     dp.append(os.path.join(path,title+".120.avi"))\r
219     dp.append(os.path.join(path,title+".noodml.avi"))\r
220     dp.append(os.path.join(path,title+".aac"))\r
221     dp.append(os.path.join(path,title+".m2v"))\r
222     dp.append(os.path.join(path,title+"_1.aac"))\r
223     dp.append(os.path.join(path,title+"_2.aac"))\r
224     dp.append(os.path.join(path,title+"_1.mp3"))\r
225     dp.append(os.path.join(path,title+"_2.mp3"))\r
226     dp.append(os.path.join(path,title+".srt"))\r
227     if configreader.getenv("remove_ts")=="1":\r
228         dp.append(os.path.join(path,title+".ts"))\r
229     if os.path.exists(os.path.join(path,title+".ts"))and os.path.exists(os.path.join(path,title+".ts.b25")):\r
230         if os.path.getsize(os.path.join(path,title+".ts"))*10>os.path.getsize(os.path.join(path,title+".ts.b25")):\r
231             if os.path.getsize(os.path.join(path,title+".ts"))>1000*1000*100:\r
232                 dp.append(os.path.join(path,title+".ts.b25"))\r
233     if os.path.exists(os.path.join(path,title+".mp4")):\r
234         if os.path.getsize(os.path.join(path,title+".mp4"))>1000*1000*10:\r
235             for ip in dp:\r
236                 try:\r
237                     os.remove(ip)\r
238                 except:\r
239                     ""\r