OSDN Git Service

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