OSDN Git Service

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