OSDN Git Service

try to fix caption 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 import subprocess\r
18 import signal\r
19 import tv2audio\r
20 \r
21 def ts2mp4(pin, pout, opt):\r
22     dir=os.path.split(pout)[0]\r
23     title=os.path.split(pout)[1]\r
24     title=os.path.splitext(title)[0]\r
25     tpraw=os.path.join(dir, title+".264")\r
26     tpmp4=os.path.join(dir, title+".mp4")\r
27     if os.path.isfile(pin) and os.path.getsize(pin)>10*1000:\r
28         tv2avi.ts2raw(pin, tpraw, opt)\r
29         time.sleep(10)\r
30         if os.path.isfile(tpraw) and os.path.getsize(tpraw)>10*1000:\r
31             raw2mp4(tpraw, tpmp4, opt)\r
32         time.sleep(10)\r
33         if os.path.exists(tpraw):\r
34             os.remove(tpraw)\r
35 def raw2mp4(pin,pout,opt):\r
36     dir=os.path.split(pout)[0]\r
37     title=os.path.split(pout)[1]\r
38     title=os.path.splitext(title)[0]\r
39     duration="-fps 29.970030 "\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         duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置\r
46     elif re.search("5",opt):\r
47         duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置\r
48     exe = configreader.getpath("mp4box")\r
49     txt=""\r
50     os.environ['LANG']="ja_JP.UTF-8"\r
51     pints=pin.replace(".264",".ts")\r
52     e1=exe +u" "+duration+" -add \""+pin+"\" -new \""+pout+"\""\r
53     execmp4box(pin, pout, e1)\r
54     addAudio(pints, pout, opt)\r
55     addCaption(pints, pout)\r
56     recdblist.addlog(pout, txt, "mp4box-log")\r
57     if status.getSettings_auto_del_tmp()==1:\r
58         if os.path.exists(pout):\r
59             if re.search(opt,"MW8") or re.search(opt,"MW9"):\r
60                 if os.path.getsize(pout)>50*1000*1000:\r
61                     deltitle(dir,title)\r
62             elif re.search(opt,"8") or re.search(opt,"9"):\r
63                 ""\r
64             else:\r
65                 if os.path.getsize(pout)>100*1000*1000:\r
66                     deltitle(dir,title)\r
67 def mkv2mp4(pin,pout):\r
68     exeb = configreader.getpath(u"mkvextract")\r
69     exe = configreader.getpath(u"mp4Box")\r
70     #dtsedit=configreader.getpath("DtsEdit")\r
71     wineexe=configreader.getpath("wine")\r
72     dir=os.path.split(pin)[0]\r
73     title=os.path.split(pin)[1]\r
74     title=os.path.splitext(title)[0]\r
75     etitle=base64.b16encode(title.encode('utf-8'))\r
76     audiopath=os.path.join(dir,etitle+u"_audio.aac")\r
77     videopath=os.path.join(dir,etitle+u"_video.264")\r
78     timecodepath=os.path.join(dir,etitle+u"_1_timecode.txt")\r
79     tmpmp4=os.path.join(dir,etitle+u".tmp.mp4")\r
80     exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'"\r
81     exe1=exeb+u" timecodes_v2 \'"+pin+"\' 1:\'"+timecodepath+"\'"\r
82     exe2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+tmpmp4+u"\'"\r
83     exe3=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+timecodepath+u"\' \'Z:\\"+tmpmp4+u"\' -o \'Z:\\"+pout+u"\'"\r
84     os.environ['LANG']="ja_JP.UTF-8"\r
85     txt=""\r
86     try:\r
87         txt=txt+execcomd(exe0)+"\n"\r
88         txt=txt+execcomd(exe1)+"\n"\r
89         txt=txt+execcomd(exe2)+"\n"\r
90         txt=txt+execcomd(exe3)+"\n"\r
91     except:\r
92         ""\r
93     recdblist.addlog(pin, txt, "MKV2MP4-log")\r
94     txt = "\n####MKV2MP4-log####\n"+txt\r
95     time.sleep(10)\r
96     if status.getSettings_auto_del_tmp()==1:\r
97         if os.path.exists(pout):\r
98             if os.path.getsize(pout)>100*1000*1000:\r
99                 deltitle(dir,title)\r
100 \r
101 def addCaption(pts,pmp4):##字幕の追加を試みる。\r
102     wineexe=configreader.getpath("wine")\r
103     pincap=pts.replace(".ts",".srt")\r
104     try:\r
105         cap2ass=configreader.getpath("caption2ass")\r
106     except:\r
107         cap2ass=""\r
108     if os.path.isfile(cap2ass):\r
109         print 1\r
110         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pts+"\" \"Z:\\"+pincap+"\""\r
111         recdblist.printutf8(e0)\r
112         print 2\r
113         p0=subprocess.Popen(e0,shell=True,env="LANG=ja_JP.UTF-8",stdout=subprocess.PIPE)\r
114         print 3\r
115         time.sleep(120)\r
116         print p0.poll\r
117         print 4\r
118         if p0.poll==None:#実行中\r
119             print "4a"\r
120             if os.path.getsize(pincap)<1000:#2mで1kb以下の場合自動で終了\r
121                 print "5a"\r
122                 os.kill(p0.pid,signal.SIGKILL)\r
123             else:\r
124                 print "4b"\r
125                 os.waitpid(p0.pid, 0)\r
126                 print "5b"\r
127         logt=unicode(p0.communicate, "UTF-8")\r
128         recdblist.printutf8(logt)\r
129         recdblist.addlog(pts,logt, "Caption-Log")\r
130         print "6"\r
131         if os.path.getsize(pincap)>1000:\r
132             print "7"\r
133             exe = configreader.getpath("mp4box")\r
134             print "8"\r
135             e1s=exe +u" -add \""+pincap+"\" \""+pout+"\""\r
136             print "9"\r
137             addmp4(pincap,pmp4,e1s)\r
138 def addAudio(pts,pmp4,opt):#オプションに応じた音声の追加を行う\r
139     exe = configreader.getpath("mp4box")\r
140     if re.search("d",opt) or re.search("5",opt):#二カ国語放送/5.1ch放送の場合\r
141         paac1=pts.replace(".ts","_1.aac")\r
142         paac2=pts.replace(".ts","_2.aac")\r
143         recdblist.printutf8(paac1)\r
144         if not os.path.exists(paac1):\r
145             paac1=pts.replace(".ts","_1.mp3")\r
146         if not os.path.exists(paac2):\r
147             paac2=pts.replace(".ts","_2.mp3")\r
148         e1a1=exe +u" -add \""+paac1+"\" \""+pmp4+"\""\r
149         e1a2=exe +u" -add \""+paac2+"\" \""+pmp4+"\""\r
150         addmp4(paac1, pmp4, e1a1)\r
151         addmp4(paac2, pmp4, e1a2)\r
152     else:\r
153         tv2audio.ts2single_audio(pts)\r
154         pinaac=pts.replace(".ts",".aac")\r
155         if not os.path.exists(pinaac):\r
156             pinaac=pinaac.replace(".aac",".mp3")\r
157         e1a=exe +u" -add \""+pinaac+"\" \""+pmp4+"\""\r
158         addmp4(pinaac, pmp4, e1a)\r
159 def execmp4box(pin,pout,cmd):\r
160     title=os.path.splitext(os.path.split(pin)[1])[0]\r
161     nt=base64.b16encode(title.encode('utf-8'))\r
162     if len(nt)>200:\r
163         nt=nt[:180]\r
164     ptin=os.path.join(os.path.dirname(pin),nt+".264")\r
165     recdblist.printutf8(ptin)\r
166     shutil.move(pin,ptin)\r
167     time.sleep(10)\r
168     ptout=os.path.join(os.path.dirname(pout),nt+".mp4")\r
169     cmdn=string.replace(cmd,pin,ptin)\r
170     cmdn=string.replace(cmdn,pout,ptout)\r
171     recdblist.printutf8(cmdn)\r
172     txt=""\r
173     try:\r
174         txt=execcomd(cmdn)\r
175     except Exception, inst:\r
176         txt= "error occures in execmp4box\n"\r
177         txt=txt+ str(type(inst))+"\n"\r
178         txt=txt+str(inst)\r
179     recdblist.addlog(pin, txt, "MP4Box-log")\r
180     time.sleep(5)\r
181     shutil.move(ptin,pin)\r
182     shutil.move(ptout,pout)\r
183     time.sleep(5)\r
184 def addmp4(padd,pout,cmd):#without video\r
185     title=os.path.splitext(os.path.split(padd)[1])[0]\r
186     ext=os.path.splitext(os.path.split(padd)[1])[1]\r
187     nt=base64.b16encode(title.encode('utf-8'))\r
188     if len(nt)>200:\r
189         nt=nt[:180]\r
190     ptadd=os.path.join(os.path.dirname(padd),nt+ext)\r
191     ptoutb=os.path.join(os.path.dirname(pout),nt+"_b.mp4")\r
192     ptout=os.path.join(os.path.dirname(pout),nt+".mp4")\r
193     shutil.move(padd,ptadd)\r
194     if os.path.isfile(pout):\r
195         shutil.move(pout,ptoutb)\r
196     time.sleep(5)\r
197     cmdn=string.replace(cmd,padd,ptadd)\r
198     cmdn=string.replace(cmdn,u"-out \""+pout,u"-add \""+ptoutb+"\" -new \""+ptout)\r
199     cmdn=string.replace(cmdn,u"\""+pout,u"-add \""+ptoutb+"\" -new \""+ptout)\r
200     cmdn=string.replace(cmdn,pout,ptout)\r
201     recdblist.printutf8(cmdn)\r
202     txt=""\r
203     try:\r
204         txt=execcomd(cmdn)\r
205     except Exception, inst:\r
206         txt= "error occures in addmp4\n"\r
207         txt=txt+ str(type(inst))+"\n"\r
208         txt=txt+str(inst)\r
209     recdblist.addlog(pout, txt, "MP4Box-log-add")\r
210     time.sleep(5)\r
211     os.remove(ptoutb)\r
212     shutil.move(ptadd,padd)\r
213     shutil.move(ptout,pout)\r
214     time.sleep(5)\r
215 def execcomd(cmd):\r
216     txt=""\r
217     try:\r
218         txt=u"Cmd : "+cmd+"\n"\r
219         txt2=commands.getoutput(cmd.encode('utf-8'))\r
220         txt=txt+unicode(txt2,"utf-8")+"\n"\r
221     except:\r
222         ""\r
223     return txt\r
224 \r
225 def deltitle(path,title):\r
226     dp=[]\r
227     dp.append(os.path.join(path,title+".avi"))\r
228     dp.append(os.path.join(path,title+".264"))\r
229     dp.append(os.path.join(path,title+".120.avi"))\r
230     dp.append(os.path.join(path,title+".noodml.avi"))\r
231     dp.append(os.path.join(path,title+".aac"))\r
232     dp.append(os.path.join(path,title+".m2v"))\r
233     dp.append(os.path.join(path,title+"_1.aac"))\r
234     dp.append(os.path.join(path,title+"_2.aac"))\r
235     dp.append(os.path.join(path,title+"_1.mp3"))\r
236     dp.append(os.path.join(path,title+"_2.mp3"))\r
237     dp.append(os.path.join(path,title+".srt"))\r
238     if configreader.getenv("remove_ts")=="1":\r
239         dp.append(os.path.join(path,title+".ts"))\r
240     if os.path.exists(os.path.join(path,title+".ts"))and os.path.exists(os.path.join(path,title+".ts.b25")):\r
241         if os.path.getsize(os.path.join(path,title+".ts"))*10>os.path.getsize(os.path.join(path,title+".ts.b25")):\r
242             if os.path.getsize(os.path.join(path,title+".ts"))>1000*1000*100:\r
243                 dp.append(os.path.join(path,title+".ts.b25"))\r
244     if os.path.exists(os.path.join(path,title+".mp4")):\r
245         if os.path.getsize(os.path.join(path,title+".mp4"))>1000*1000*10:\r
246             for ip in dp:\r
247                 try:\r
248                     os.remove(ip)\r
249                 except:\r
250                     ""\r