OSDN Git Service

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