OSDN Git Service

fix audio 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 auto_process\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 subprocess\r
15 \r
16 import tv2avi\r
17 import recdblist\r
18 import configreader\r
19 import status\r
20 import tv2audio\r
21 \r
22 def ts2mp4(pin, pout, opt):\r
23     dir=os.path.split(pout)[0]\r
24     title=os.path.split(pout)[1]\r
25     title=os.path.splitext(title)[0]\r
26     tpraw=os.path.join(dir, title+".264")\r
27     tpmp4=os.path.join(dir, title+".mp4")\r
28     if os.path.isfile(pin) and os.path.getsize(pin)>10*1000:\r
29         tv2avi.ts2raw(pin, tpraw, opt)\r
30         time.sleep(10)\r
31         if os.path.isfile(tpraw) and os.path.getsize(tpraw)>10*1000:\r
32             raw2mp4(tpraw, tpmp4, opt)\r
33         time.sleep(10)\r
34         if os.path.exists(tpraw):\r
35             os.remove(tpraw)\r
36 def raw2mp4(pin,pout,opt):\r
37     dir=os.path.split(pout)[0]\r
38     title=os.path.split(pout)[1]\r
39     title=os.path.splitext(title)[0]\r
40     duration="-fps 29.970030 "\r
41     if re.search("a",opt):\r
42         duration="-fps 23.976023 "\r
43     elif re.search("v",opt):\r
44         duration="-fps 23.976023 "\r
45     if re.search("d",opt):\r
46         duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置\r
47     elif re.search("5",opt):\r
48         duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置\r
49     if re.search("I",opt):\r
50         duration="-fps 29.970030 "\r
51     exe = configreader.getpath("mp4box")\r
52     txt=""\r
53     os.environ['LANG']="ja_JP.UTF-8"\r
54     pints=pin.replace(".264",".ts")\r
55     e1=exe +u" "+duration+" -add \""+pin+"\" -new \""+pout+"\""\r
56     execmp4box(pin, pout, e1)\r
57     addAudio(pints, pout, opt)\r
58     addCaption(pints, pout)\r
59     #recdblist.addlog(pout, txt, "mp4boxログ-コマンド")\r
60     #recdblist.addlog(pout, txt, "mp4boxログ-詳細")\r
61     if status.getSettings_auto_del_tmp()==1:\r
62         if os.path.exists(pout):\r
63             if re.search(opt,"MW8") or re.search(opt,"MW9"):\r
64                 auto_process.deltmpfile(dir, title, ".mp4")\r
65             elif re.search(opt,"8") or re.search(opt,"9"):\r
66                 ""\r
67             else:\r
68                 auto_process.deltmpfile(dir, title, ".mp4")\r
69 def mkv2mp4(pin,pout):\r
70     exeb = configreader.getpath(u"mkvextract")\r
71     exe = configreader.getpath(u"mp4Box")\r
72     #dtsedit=configreader.getpath("DtsEdit")\r
73     wineexe=configreader.getpath("wine")\r
74     dir=os.path.split(pin)[0]\r
75     title=os.path.split(pin)[1]\r
76     title=os.path.splitext(title)[0]\r
77     etitle=base64.b16encode(title.encode('utf-8'))\r
78     audiopath=os.path.join(dir,etitle+u"_audio.aac")\r
79     videopath=os.path.join(dir,etitle+u"_video.264")\r
80     timecodepath=os.path.join(dir,etitle+u"_1_timecode.txt")\r
81     tmpmp4=os.path.join(dir,etitle+u".tmp.mp4")\r
82     exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'"\r
83     exe1=exeb+u" timecodes_v2 \'"+pin+"\' 1:\'"+timecodepath+"\'"\r
84     exe2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+tmpmp4+u"\'"\r
85     exe3=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+timecodepath+u"\' \'Z:\\"+tmpmp4+u"\' -o \'Z:\\"+pout+u"\'"\r
86     os.environ['LANG']="ja_JP.UTF-8"\r
87     txt=""\r
88     try:\r
89         txt=txt+execcomd(exe0)+"\n"\r
90         txt=txt+execcomd(exe1)+"\n"\r
91         txt=txt+execcomd(exe2)+"\n"\r
92         txt=txt+execcomd(exe3)+"\n"\r
93     except:\r
94         ""\r
95     recdblist.addlog(pin, txt, u"MKV2MP4-log")\r
96     txt = "\n####MKV2MP4-log####\n"+txt\r
97     time.sleep(10)\r
98     if status.getSettings_auto_del_tmp()==1:\r
99         if os.path.exists(pout):\r
100             auto_process.deltmpfile(dir, title, ".mp4")\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         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pts+"\" \"Z:\\"+pincap+"\""\r
110         recdblist.printutf8(e0)\r
111         p0=subprocess.Popen(e0,shell=True,stdout=subprocess.PIPE)\r
112         time.sleep(100)\r
113         if p0.poll==None:#実行中\r
114             #if os.path.getsize(pincap)<1000:#2mで1kb以下の場合自動で終了\r
115              #   os.kill(p0.pid,signal.SIGKILL)\r
116             #else:\r
117             os.waitpid(p0.pid, 0)\r
118             logt=unicode(p0.communicate()[0], "UTF-8")\r
119             recdblist.addlog(pts,e0, u"Captionログ-コマンド")\r
120             recdblist.addlog(pts,logt, u"Captionログ-詳細")\r
121         if os.path.exists(pincap):\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         if os.path.exists(paac1):\r
139             addmp4(paac1, pmp4, e1a1)\r
140         if os.path.exists(paac2):\r
141             addmp4(paac2, pmp4, e1a2)\r
142     else:\r
143         tv2audio.ts2single_audio(pts)\r
144         pinaac=pts.replace(".ts",".aac")\r
145         if not os.path.exists(pinaac):\r
146             pinaac=pinaac.replace(".aac",".mp3")\r
147         e1a=exe +u" -add \""+pinaac+"\" \""+pmp4+"\""\r
148         if os.path.exists(pinaac):\r
149             addmp4(pinaac, pmp4, e1a)\r
150 def execmp4box(pin,pout,cmd):\r
151     title=os.path.splitext(os.path.split(pin)[1])[0]\r
152     nt=base64.b16encode(title.encode('utf-8'))\r
153     if len(nt)>200:\r
154         nt=nt[:180]\r
155     ptin=os.path.join(os.path.dirname(pin),nt+".264")\r
156     recdblist.printutf8(ptin)\r
157     shutil.move(pin,ptin)\r
158     time.sleep(10)\r
159     ptout=os.path.join(os.path.dirname(pout),nt+".mp4")\r
160     cmdn=string.replace(cmd,pin,ptin)\r
161     cmdn=string.replace(cmdn,pout,ptout)\r
162     recdblist.printutf8(cmdn)\r
163     txt=""\r
164     try:\r
165         txt=execcomd(cmdn)\r
166     except Exception, inst:\r
167         txt= "error occures in execmp4box\n"\r
168         txt=txt+ str(type(inst))+"\n"\r
169         txt=txt+str(inst)\r
170     recdblist.addlog(pin, cmdn, u"MP4Boxログ-コマンド")\r
171     recdblist.addlog(pin, txt, u"MP4Boxログ-詳細")\r
172     time.sleep(5)\r
173     shutil.move(ptin,pin)\r
174     shutil.move(ptout,pout)\r
175     time.sleep(5)\r
176 def addmp4(padd,pout,cmd):#without video\r
177     title=os.path.splitext(os.path.split(padd)[1])[0]\r
178     ext=os.path.splitext(os.path.split(padd)[1])[1]\r
179     nt=base64.b16encode(title.encode('utf-8'))\r
180     if len(nt)>200:\r
181         nt=nt[:180]\r
182     ptadd=os.path.join(os.path.dirname(padd),nt+ext)\r
183     ptoutb=os.path.join(os.path.dirname(pout),nt+"_b.mp4")\r
184     ptout=os.path.join(os.path.dirname(pout),nt+".mp4")\r
185     shutil.move(padd,ptadd)\r
186     if os.path.isfile(pout):\r
187         shutil.move(pout,ptoutb)\r
188     time.sleep(5)\r
189     cmdn=string.replace(cmd,padd,ptadd)\r
190     cmdn=string.replace(cmdn,u"-out \""+pout,u"-add \""+ptoutb+"\" -new \""+ptout)\r
191     cmdn=string.replace(cmdn,u"\""+pout,u"-add \""+ptoutb+"\" -new \""+ptout)\r
192     cmdn=string.replace(cmdn,pout,ptout)\r
193     recdblist.printutf8(cmdn)\r
194     txt=""\r
195     try:\r
196         txt=execcomd(cmdn)\r
197     except Exception, inst:\r
198         txt= "error occures in addmp4\n"\r
199         txt=txt+ str(type(inst))+"\n"\r
200         txt=txt+str(inst)\r
201     recdblist.addlog(pout, cmdn, u"MP4Box追加ログ-コマンド")\r
202     recdblist.addlog(pout, txt, u"MP4Box追加ログ-詳細")\r
203     time.sleep(5)\r
204     shutil.move(ptadd,padd)\r
205     if os.path.exists(ptout):\r
206         shutil.move(ptout,pout)\r
207         os.remove(ptoutb)\r
208     else:\r
209         txtt=padd+u"のインポートエラー"\r
210         recdblist.addlog(pout, txtt, u"MP4Box追加ログ-コマンド")\r
211         shutil.move(ptoutb,pout)\r
212     time.sleep(5)\r
213 def execcomd(cmd):\r
214     txt=""\r
215     try:\r
216         txt=u"Cmd : "+cmd+"\n"\r
217         txt2=commands.getoutput(cmd.encode('utf-8'))\r
218         txt=txt+unicode(txt2,"utf-8")+"\n"\r
219     except:\r
220         ""\r
221     return txt