OSDN Git Service

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