OSDN Git Service

change mobile encode option
[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 import traceback\r
16 \r
17 import tv2avi\r
18 import recdblist\r
19 import configreader\r
20 import status\r
21 import tv2audio\r
22 \r
23 def ts2mp4(pin, pout, opt):\r
24     dir=os.path.split(pout)[0]\r
25     title=os.path.split(pout)[1]\r
26     title=os.path.splitext(title)[0]\r
27     tpraw=os.path.join(dir, title+".264")\r
28     tpmp4=os.path.join(dir, title+".mp4")\r
29     if re.search("d",opt) and not os.path.exists(pin.replace(".ts",".m2v")):\r
30         tv2audio.ts2dualaudio_BonTsDemux(pin,recdblist.BONTSDEMUX_DELAY, opt)\r
31     if os.path.isfile(pin) and os.path.getsize(pin)>10*1000:\r
32         tv2avi.ts2raw(pin, tpraw, opt)\r
33         time.sleep(10)\r
34         if os.path.isfile(tpraw) and os.path.getsize(tpraw)>10*1000:\r
35             raw2mp4(tpraw, tpmp4, opt)\r
36         time.sleep(10)\r
37         if os.path.exists(tpraw):\r
38             os.remove(tpraw)\r
39 def raw2mp4(pin,pout,opt):\r
40     dir=os.path.split(pout)[0]\r
41     title=os.path.split(pout)[1]\r
42     title=os.path.splitext(title)[0]\r
43     duration="-fps 29.970030 "\r
44     if re.search("a",opt):\r
45         duration="-fps 23.976023 "\r
46     elif re.search("v",opt):\r
47         duration="-fps 23.976023 "\r
48     if re.search("d",opt):\r
49         duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置\r
50     elif re.search("5",opt):\r
51         duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置\r
52     if re.search("I",opt):\r
53         duration="-fps 29.970030 "\r
54     exe = configreader.getpath("mp4box")\r
55     txt=""\r
56     os.environ['LANG']="ja_JP.UTF-8"\r
57     pints=pin.replace(".264",".ts")\r
58     e1=exe +u" "+duration+" -add \""+pin+"\" -new \""+pout+"\""\r
59     execmp4box(pin, pout, e1)\r
60     addAudio(pints, pout, opt)\r
61     addCaption(pints, pout)\r
62     #recdblist.addlog(pout, txt, "mp4boxログ-コマンド")\r
63     #recdblist.addlog(pout, txt, "mp4boxログ-詳細")\r
64     if status.getSettings_auto_del_tmp()==1:\r
65         if os.path.exists(pout):\r
66             if re.search(opt,"1") or re.search(opt,"2"):\r
67                 auto_process.deltmpfile(dir, title, ".mp4")\r
68             else:\r
69                 auto_process.deltmpfile(dir, title, ".mp4")\r
70 def mkv2mp4(pin,pout):\r
71     exeb = configreader.getpath(u"mkvextract")\r
72     exe = configreader.getpath(u"mp4Box")\r
73     #dtsedit=configreader.getpath("DtsEdit")\r
74     wineexe=configreader.getpath("wine")\r
75     dir=os.path.split(pin)[0]\r
76     title=os.path.split(pin)[1]\r
77     title=os.path.splitext(title)[0]\r
78     etitle=base64.b16encode(title.encode('utf-8'))\r
79     audiopath=os.path.join(dir,etitle+u"_audio.aac")\r
80     videopath=os.path.join(dir,etitle+u"_video.264")\r
81     timecodepath=os.path.join(dir,etitle+u"_1_timecode.txt")\r
82     tmpmp4=os.path.join(dir,etitle+u".tmp.mp4")\r
83     exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'"\r
84     exe1=exeb+u" timecodes_v2 \'"+pin+"\' 1:\'"+timecodepath+"\'"\r
85     exe2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+tmpmp4+u"\'"\r
86     exe3=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+timecodepath+u"\' \'Z:\\"+tmpmp4+u"\' -o \'Z:\\"+pout+u"\'"\r
87     os.environ['LANG']="ja_JP.UTF-8"\r
88     txt=""\r
89     try:\r
90         txt=txt+execcomd(exe0)+"\n"\r
91         txt=txt+execcomd(exe1)+"\n"\r
92         txt=txt+execcomd(exe2)+"\n"\r
93         txt=txt+execcomd(exe3)+"\n"\r
94     except:\r
95         ""\r
96     recdblist.addlog(pin, txt, u"MKV2MP4-log")\r
97     txt = "\n####MKV2MP4-log####\n"+txt\r
98     time.sleep(10)\r
99     if status.getSettings_auto_del_tmp()==1:\r
100         if os.path.exists(pout):\r
101             auto_process.deltmpfile(dir, title, ".mp4")\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(100)\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.exists(pincap):\r
123             if os.path.getsize(pincap)>1000:\r
124                 exe = configreader.getpath("mp4box")\r
125                 e1s=exe +u" -add \""+pincap+"\" \""+pmp4+"\""\r
126                 addmp4(pincap,pmp4,e1s)\r
127 def addAudio(pts,pmp4,opts):#オプションに応じた音声の追加を行う\r
128     exe = configreader.getpath("mp4box")\r
129     if re.search("d",opts) or re.search("5",opts):#二カ国語放送/5.1ch放送の場合\r
130         paac1=pts.replace(".ts","_1.aac")\r
131         paac2=pts.replace(".ts","_2.aac")\r
132         recdblist.printutf8(paac1)\r
133         if not os.path.exists(paac1):\r
134             paac1=pts.replace(".ts","_1.mp3")\r
135         if not os.path.exists(paac2):\r
136             paac2=pts.replace(".ts","_2.mp3")\r
137         e1a1=exe +u" -add \""+paac1+"\" \""+pmp4+"\""\r
138         e1a2=exe +u" -add \""+paac2+"\" \""+pmp4+"\""\r
139         if os.path.exists(paac1):\r
140             addmp4(paac1, pmp4, e1a1)\r
141         if os.path.exists(paac2):\r
142             addmp4(paac2, pmp4, e1a2)\r
143     else:\r
144         tv2audio.ts2single_audio(pts,opts)\r
145         pinaac=pts.replace(".ts",".aac")\r
146         if not os.path.exists(pinaac):\r
147             pinaac=pinaac.replace(".aac",".mp3")\r
148         e1a=exe +u" -add \""+pinaac+"\" \""+pmp4+"\""\r
149         if os.path.exists(pinaac):\r
150             addmp4(pinaac, pmp4, e1a)\r
151 def execmp4box(pin,pout,cmd):\r
152     title=os.path.splitext(os.path.split(pin)[1])[0]\r
153     nt=base64.b16encode(title.encode('utf-8'))\r
154     if len(nt)>200:\r
155         nt=nt[:180]\r
156     ptin=os.path.join(os.path.dirname(pin),nt+".264")\r
157     recdblist.printutf8(ptin)\r
158     shutil.move(pin,ptin)\r
159     time.sleep(10)\r
160     ptout=os.path.join(os.path.dirname(pout),nt+".mp4")\r
161     cmdn=string.replace(cmd,pin,ptin)\r
162     cmdn=string.replace(cmdn,pout,ptout)\r
163     recdblist.printutf8(cmdn)\r
164     txt=""\r
165     try:\r
166         txt=execcomd(cmdn)\r
167     except Exception, inst:\r
168         txt= "error occures in execmp4box\n"\r
169         txt=txt+ str(type(inst))+"\n"\r
170         txt=txt+str(inst)\r
171         recdblist.Commonlogex("Error", "excecmp4box(tv2mp4.py)", str(type(inst)), str(inst)+traceback.format_exc(),verbose_level=200,log_level=200)\r
172     recdblist.addlog(pin, cmdn, u"MP4Boxログ-コマンド")\r
173     recdblist.addlog(pin, txt, u"MP4Boxログ-詳細")\r
174     time.sleep(5)\r
175     shutil.move(ptin,pin)\r
176     shutil.move(ptout,pout)\r
177     time.sleep(5)\r
178 def addmp4(padd,pout,cmd):#without video\r
179     title=os.path.splitext(os.path.split(padd)[1])[0]\r
180     ext=os.path.splitext(os.path.split(padd)[1])[1]\r
181     nt=base64.b16encode(title.encode('utf-8'))\r
182     if len(nt)>200:\r
183         nt=nt[:180]\r
184     ptadd=os.path.join(os.path.dirname(padd),nt+ext)\r
185     ptoutb=os.path.join(os.path.dirname(pout),nt+"_b.mp4")\r
186     ptout=os.path.join(os.path.dirname(pout),nt+".mp4")\r
187     shutil.move(padd,ptadd)\r
188     if os.path.isfile(pout):\r
189         shutil.move(pout,ptoutb)\r
190     time.sleep(5)\r
191     cmdn=string.replace(cmd,padd,ptadd)\r
192     cmdn=string.replace(cmdn,u"-out \""+pout,u"-add \""+ptoutb+"\" -new \""+ptout)\r
193     cmdn=string.replace(cmdn,u"\""+pout,u"-add \""+ptoutb+"\" -new \""+ptout)\r
194     cmdn=string.replace(cmdn,pout,ptout)\r
195     recdblist.printutf8(cmdn)\r
196     txt=""\r
197     try:\r
198         txt=execcomd(cmdn)\r
199     except Exception, inst:\r
200         txt= "error occures in addmp4\n"\r
201         txt=txt+ str(type(inst))+"\n"\r
202         txt=txt+str(inst)\r
203     recdblist.addlog(pout, cmdn, u"MP4Box追加ログ-コマンド")\r
204     recdblist.addlog(pout, txt, u"MP4Box追加ログ-詳細")\r
205     time.sleep(5)\r
206     shutil.move(ptadd,padd)\r
207     if os.path.exists(ptout):\r
208         shutil.move(ptout,pout)\r
209         os.remove(ptoutb)\r
210     else:\r
211         txtt=padd+u"のインポートエラー"\r
212         recdblist.addlog(pout, txtt, u"MP4Box追加ログ-コマンド")\r
213         shutil.move(ptoutb,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