OSDN Git Service

fix encode for mobile function.(option 8,9,0)
[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         txt=txt+execcomd(e1)\r
124         execmp4box(pin, pout, e2)\r
125         addmp4(pinaac, pout, e2a)\r
126         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):\r
127             addmp4(pincap, pout, e2s)\r
128     recdblist.addlog(pout, txt, "mp4box-log")\r
129     if status.getSettings_auto_del_tmp()==1:\r
130         if os.path.exists(pout):\r
131             if re.search(opts,"MW8") or re.search(opts,"MW9"):\r
132                 if os.path.getsize(pout)>50*1000*1000:\r
133                     deltitle(dir,title)\r
134             elif re.search(opts,"8") or re.search(opts,"9"):\r
135                 ""\r
136             else:\r
137                 if os.path.getsize(pout)>100*1000*1000:\r
138                     deltitle(dir,title)\r
139 def mkv2mp4(pin,pout):\r
140     exeb = configreader.getpath(u"mkvextract")\r
141     exe = configreader.getpath(u"mp4Box")\r
142     #dtsedit=configreader.getpath("DtsEdit")\r
143     wineexe=configreader.getpath("wine")\r
144     dir=os.path.split(pin)[0]\r
145     title=os.path.split(pin)[1]\r
146     title=os.path.splitext(title)[0]\r
147     etitle=base64.b16encode(title.encode('utf-8'))\r
148     audiopath=os.path.join(dir,etitle+u"_audio.aac")\r
149     videopath=os.path.join(dir,etitle+u"_video.264")\r
150     timecodepath=os.path.join(dir,etitle+u"_1_timecode.txt")\r
151     tmpmp4=os.path.join(dir,etitle+u".tmp.mp4")\r
152     exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'"\r
153     exe1=exeb+u" timecodes_v2 \'"+pin+"\' 1:\'"+timecodepath+"\'"\r
154     exe2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+tmpmp4+u"\'"\r
155     exe3=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+timecodepath+u"\' \'Z:\\"+tmpmp4+u"\' -o \'Z:\\"+pout+u"\'"\r
156     os.environ['LANG']="ja_JP.UTF-8"\r
157     txt=""\r
158     try:\r
159         txt=txt+execcomd(exe0)+"\n"\r
160         txt=txt+execcomd(exe1)+"\n"\r
161         txt=txt+execcomd(exe2)+"\n"\r
162         txt=txt+execcomd(exe3)+"\n"\r
163     except:\r
164         ""\r
165     recdblist.addlog(pin, txt, "MKV2MP4-log")\r
166     txt = "\n####MKV2MP4-log####\n"+txt\r
167     time.sleep(10)\r
168     if status.getSettings_auto_del_tmp()==1:\r
169         if os.path.exists(pout):\r
170             if os.path.getsize(pout)>100*1000*1000:\r
171                 deltitle(dir,title)\r
172 def execmp4box(pin,pout,cmd):\r
173     title=os.path.splitext(os.path.split(pin)[1])[0]\r
174     ptin=os.path.join(os.path.dirname(pin),base64.b16encode(title.encode('utf-8'))+".264")\r
175     recdblist.printutf8(ptin)\r
176     shutil.move(pin,ptin)\r
177     time.sleep(10)\r
178     ptout=os.path.join(os.path.dirname(pout),base64.b16encode(title.encode('utf-8'))+".mp4")\r
179     cmdn=string.replace(cmd,pin,ptin)\r
180     cmdn=string.replace(cmdn,pout,ptout)\r
181     recdblist.printutf8(cmdn)\r
182     txt=""\r
183     try:\r
184         txt=execcomd(cmdn)\r
185     except Exception, inst:\r
186         txt= "error occures in execmp4box\n"\r
187         txt=txt+ str(type(inst))+"\n"\r
188         txt=txt+str(inst)\r
189     recdblist.addlog(pin, txt, "MP4Box-log")\r
190     time.sleep(5)\r
191     shutil.move(ptin,pin)\r
192     shutil.move(ptout,pout)\r
193     time.sleep(5)\r
194 def addmp4(padd,pout,cmd):#without video\r
195     title=os.path.splitext(os.path.split(padd)[1])[0]\r
196     ext=os.path.splitext(os.path.split(padd)[1])[1]\r
197     ptadd=os.path.join(os.path.dirname(padd),base64.b16encode(title.encode('utf-8'))+ext)\r
198     ptoutb=os.path.join(os.path.dirname(pout),base64.b16encode(title.encode('utf-8'))+"_b.mp4")\r
199     ptout=os.path.join(os.path.dirname(pout),base64.b16encode(title.encode('utf-8'))+".mp4")\r
200     shutil.move(padd,ptadd)\r
201     if os.path.isfile(pout):\r
202         shutil.move(pout,ptoutb)\r
203     time.sleep(5)\r
204     cmdn=string.replace(cmd,padd,ptadd)\r
205     cmdn=string.replace(cmdn,u"-out \""+pout,u"-add \""+ptoutb+"\" -new \""+ptout)\r
206     cmdn=string.replace(cmdn,u"\""+pout,u"-add \""+ptoutb+"\" -new \""+ptout)\r
207     cmdn=string.replace(cmdn,pout,ptout)\r
208     recdblist.printutf8(cmdn)\r
209     txt=""\r
210     try:\r
211         txt=execcomd(cmdn)\r
212     except Exception, inst:\r
213         txt= "error occures in addmp4\n"\r
214         txt=txt+ str(type(inst))+"\n"\r
215         txt=txt+str(inst)\r
216     recdblist.addlog(pout, txt, "MP4Box-log-add")\r
217     time.sleep(5)\r
218     os.remove(ptoutb)\r
219     shutil.move(ptadd,padd)\r
220     shutil.move(ptout,pout)\r
221     time.sleep(5)\r
222 def execcomd(cmd):\r
223     txt=""\r
224     try:\r
225         txt=u"Cmd : "+cmd+"\n"\r
226         txt2=commands.getoutput(cmd.encode('utf-8'))\r
227         txt=txt+unicode(txt2,"utf-8")+"\n"\r
228     except:\r
229         ""\r
230     return txt\r
231 \r
232 def deltitle(path,title):\r
233     dp=[]\r
234     dp.append(os.path.join(path,title+".avi"))\r
235     dp.append(os.path.join(path,title+".264"))\r
236     dp.append(os.path.join(path,title+".120.avi"))\r
237     dp.append(os.path.join(path,title+".noodml.avi"))\r
238     dp.append(os.path.join(path,title+".aac"))\r
239     dp.append(os.path.join(path,title+".m2v"))\r
240     dp.append(os.path.join(path,title+"_1.aac"))\r
241     dp.append(os.path.join(path,title+"_2.aac"))\r
242     dp.append(os.path.join(path,title+"_1.mp3"))\r
243     dp.append(os.path.join(path,title+"_2.mp3"))\r
244     dp.append(os.path.join(path,title+".srt"))\r
245     if configreader.getenv("remove_ts")=="1":\r
246         dp.append(os.path.join(path,title+".ts"))\r
247     if os.path.exists(os.path.join(path,title+".ts"))and os.path.exists(os.path.join(path,title+".ts.b25")):\r
248         if os.path.getsize(os.path.join(path,title+".ts"))*10>os.path.getsize(os.path.join(path,title+".ts.b25")):\r
249             if os.path.getsize(os.path.join(path,title+".ts"))>1000*1000*100:\r
250                 dp.append(os.path.join(path,title+".ts.b25"))\r
251     if os.path.exists(os.path.join(path,title+".mp4")):\r
252         if os.path.getsize(os.path.join(path,title+".mp4"))>1000*1000*10:\r
253             for ip in dp:\r
254                 try:\r
255                     os.remove(ip)\r
256                 except:\r
257                     ""\r