3 # Rec10 TS Recording Tools
\r
4 # Copyright (C) 2009-2011 Yukikaze
\r
24 def ts2mp4(pin, pout, opt):
\r
25 dir=os.path.split(pout)[0]
\r
26 title=os.path.split(pout)[1]
\r
27 title=os.path.splitext(title)[0]
\r
28 tpraw=os.path.join(dir, title+".264")
\r
29 tpmp4=os.path.join(dir, title+".mp4")
\r
30 if re.search("d",opt) and not os.path.exists(pin.replace(".ts",".m2v")):
\r
31 tv2audio.ts2dualaudio_BonTsDemux(pin,recdblist.BONTSDEMUX_DELAY, opt)
\r
32 if os.path.isfile(pin) and os.path.getsize(pin)>10*1000:
\r
33 tv2avi.ts2raw(pin, tpraw, opt)
\r
35 if os.path.isfile(tpraw) and os.path.getsize(tpraw)>10*1000:
\r
36 raw2mp4(tpraw, tpmp4, opt)
\r
38 if os.path.exists(tpraw):
\r
40 zip.addFile2FileZip(recdblist.getLogTitle(pin)+".command.log", recdblist.getLogTitle(pin)+".log.zip")
\r
41 if os.path.exists(recdblist.getLogTitle(pin)+".command.log"):
\r
42 os.remove(recdblist.getLogTitle(pin)+".command.log")
\r
43 def raw2mp4(pin,pout,opt):
\r
44 dir=os.path.split(pout)[0]
\r
45 title=os.path.split(pout)[1]
\r
46 title=os.path.splitext(title)[0]
\r
47 duration="-fps 29.970030 "
\r
48 if re.search("a",opt):
\r
49 duration="-fps 23.976023 "
\r
50 elif re.search("v",opt):
\r
51 duration="-fps 23.976023 "
\r
52 if re.search("d",opt):
\r
53 duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置
\r
54 elif re.search("5",opt):
\r
55 duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置
\r
56 if re.search("I",opt):
\r
57 duration="-fps 29.970030 "
\r
58 exe = configreader.getConfPath("mp4box")
\r
60 os.environ['LANG']="ja_JP.UTF-8"
\r
61 pints=pin.replace(".264",".ts")
\r
62 e1=exe +u" "+duration+" -add \""+pin+"\" -new \""+pout+"\""
\r
63 execmp4box(pin, pout, e1)
\r
64 addAudio(pints, pout, opt)
\r
65 addCaption(pints, pout)
\r
66 if status.getSettings_auto_del_tmp()==1:
\r
67 if os.path.exists(pout):
\r
68 if re.search(opt,"1") or re.search(opt,"2"):
\r
69 auto_process.deleteTmpFile(dir, title, ".mp4")
\r
71 auto_process.deleteTmpFile(dir, title, ".mp4")
\r
72 def mkv2mp4(pin,pout):
\r
73 exeb = configreader.getConfPath(u"mkvextract")
\r
74 exe = configreader.getConfPath(u"mp4Box")
\r
75 #dtsedit=configreader.getConfPath("DtsEdit")
\r
76 wineexe=configreader.getConfPath("wine")
\r
77 dir=os.path.split(pin)[0]
\r
78 title=os.path.split(pin)[1]
\r
79 title=os.path.splitext(title)[0]
\r
80 etitle=base64.b16encode(title.encode('utf-8'))
\r
81 audiopath=os.path.join(dir,etitle+u"_audio.aac")
\r
82 videopath=os.path.join(dir,etitle+u"_video.264")
\r
83 timecodepath=os.path.join(dir,etitle+u"_1_timecode.txt")
\r
84 tmpmp4=os.path.join(dir,etitle+u".tmp.mp4")
\r
85 exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'"
\r
86 exe1=exeb+u" timecodes_v2 \'"+pin+"\' 1:\'"+timecodepath+"\'"
\r
87 exe2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+tmpmp4+u"\'"
\r
88 exe3=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+timecodepath+u"\' \'Z:\\"+tmpmp4+u"\' -o \'Z:\\"+pout+u"\'"
\r
89 os.environ['LANG']="ja_JP.UTF-8"
\r
92 txt=txt+execcomd(exe0)+"\n"
\r
93 txt=txt+execcomd(exe1)+"\n"
\r
94 txt=txt+execcomd(exe2)+"\n"
\r
95 txt=txt+execcomd(exe3)+"\n"
\r
98 recdblist.addLog(pin, txt, u"MKV2MP4-log")
\r
99 txt = "\n####MKV2MP4-log####\n"+txt
\r
101 if status.getSettings_auto_del_tmp()==1:
\r
102 if os.path.exists(pout):
\r
103 auto_process.deleteTmpFile(dir, title, ".mp4")
\r
104 def addCaption(pts,pmp4):##字幕の追加を試みる。
\r
105 wineexe=configreader.getConfPath("wine")
\r
106 pincap=pts.replace(".ts",".srt")
\r
108 cap2ass=configreader.getConfPath("caption2ass")
\r
111 if os.path.isfile(cap2ass):
\r
112 e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pts+"\" \"Z:\\"+pincap+"\""
\r
113 recdblist.printutf8(e0)
\r
114 p0=subprocess.Popen(e0,shell=True,stdout=subprocess.PIPE)
\r
116 if p0.poll==None:#実行中
\r
117 #if os.path.getsize(pincap)<1000:#2mで1kb以下の場合自動で終了
\r
118 # os.kill(p0.pid,signal.SIGKILL)
\r
120 os.waitpid(p0.pid, 0)
\r
121 logt=unicode(p0.communicate()[0], "UTF-8")
\r
122 recdblist.addLog(pts,e0, u"Captionログ-コマンド")
\r
123 recdblist.addLog(pts,logt, u"Captionログ-詳細")
\r
124 recdblist.addCommandLogZip(pts, "mp4box_caption", "mp4box_caption", e0, logt)
\r
125 if os.path.exists(pincap):
\r
126 if os.path.getsize(pincap)>1000:
\r
127 exe = configreader.getConfPath("mp4box")
\r
128 e1s=exe +u" -add \""+pincap+"\" \""+pmp4+"\""
\r
129 addmp4(pincap,pmp4,e1s)
\r
130 def addAudio(pts,pmp4,opts):#オプションに応じた音声の追加を行う
\r
131 exe = configreader.getConfPath("mp4box")
\r
132 if re.search("d",opts) or re.search("5",opts):#二カ国語放送/5.1ch放送の場合
\r
133 paac1=pts.replace(".ts","_1.aac")
\r
134 paac2=pts.replace(".ts","_2.aac")
\r
135 recdblist.printutf8(paac1)
\r
136 if not os.path.exists(paac1):
\r
137 paac1=pts.replace(".ts","_1.mp3")
\r
138 if not os.path.exists(paac2):
\r
139 paac2=pts.replace(".ts","_2.mp3")
\r
140 e1a1=exe +u" -add \""+paac1+"\" \""+pmp4+"\""
\r
141 e1a2=exe +u" -add \""+paac2+"\" \""+pmp4+"\""
\r
142 if os.path.exists(paac1):
\r
143 addmp4(paac1, pmp4, e1a1)
\r
144 if os.path.exists(paac2):
\r
145 addmp4(paac2, pmp4, e1a2)
\r
147 tv2audio.ts2single_audio(pts,opts)
\r
148 pinaac=pts.replace(".ts",".aac")
\r
149 if not os.path.exists(pinaac):
\r
150 pinaac=pinaac.replace(".aac",".mp3")
\r
151 e1a=exe +u" -add \""+pinaac+"\" \""+pmp4+"\""
\r
152 if os.path.exists(pinaac):
\r
153 addmp4(pinaac, pmp4, e1a)
\r
154 def execmp4box(pin,pout,cmd):
\r
155 title=os.path.splitext(os.path.split(pin)[1])[0]
\r
156 nt=base64.b16encode(title.encode('utf-8'))
\r
159 ptin=os.path.join(os.path.dirname(pin),nt+".264")
\r
160 recdblist.printutf8(ptin)
\r
161 shutil.move(pin,ptin)
\r
163 ptout=os.path.join(os.path.dirname(pout),nt+".mp4")
\r
164 cmdn=string.replace(cmd,pin,ptin)
\r
165 cmdn=string.replace(cmdn,pout,ptout)
\r
166 recdblist.printutf8(cmdn)
\r
167 recdblist.addCommandSelfLog(pin, cmdn)
\r
171 except Exception, inst:
\r
172 txt= "error occures in execmp4box\n"
\r
173 txt=txt+ str(type(inst))+"\n"
\r
175 recdblist.addCommonlogEX("Error", "excecmp4box(tv2mp4.py)", str(type(inst)), str(inst)+traceback.format_exc(),verbose_level=200,log_level=200)
\r
176 recdblist.addLog(pin, cmdn, u"MP4Boxログ-コマンド")
\r
177 recdblist.addLog(pin, txt, u"MP4Boxログ-詳細")
\r
178 recdblist.addCommandLogZip(pin, "MP4Box", "mp4box", cmdn,txt)
\r
180 shutil.move(ptin,pin)
\r
181 shutil.move(ptout,pout)
\r
183 def addmp4(padd,pout,cmd):#without video
\r
184 title=os.path.splitext(os.path.split(padd)[1])[0]
\r
185 ext=os.path.splitext(os.path.split(padd)[1])[1]
\r
186 nt=base64.b16encode(title.encode('utf-8'))
\r
189 ptadd=os.path.join(os.path.dirname(padd),nt+ext)
\r
190 ptoutb=os.path.join(os.path.dirname(pout),nt+"_b.mp4")
\r
191 ptout=os.path.join(os.path.dirname(pout),nt+".mp4")
\r
192 shutil.move(padd,ptadd)
\r
193 if os.path.isfile(pout):
\r
194 shutil.move(pout,ptoutb)
\r
196 cmdn=string.replace(cmd,padd,ptadd)
\r
197 cmdn=string.replace(cmdn,u"-out \""+pout,u"-add \""+ptoutb+"\" -new \""+ptout)
\r
198 cmdn=string.replace(cmdn,u"\""+pout,u"-add \""+ptoutb+"\" -new \""+ptout)
\r
199 cmdn=string.replace(cmdn,pout,ptout)
\r
200 recdblist.printutf8(cmdn)
\r
204 except Exception, inst:
\r
205 txt= "error occures in addmp4\n"
\r
206 txt=txt+ str(type(inst))+"\n"
\r
208 recdblist.addLog(pout, cmdn, u"MP4Box追加ログ-コマンド")
\r
209 recdblist.addLog(pout, txt, u"MP4Box追加ログ-詳細")
\r
210 recdblist.addCommandLogZip(pout, "MP4Box_add", "mp4box_add", cmdn, txt)
\r
212 shutil.move(ptadd,padd)
\r
213 if os.path.exists(ptout):
\r
214 shutil.move(ptout,pout)
\r
217 txtt=padd+u"のインポートエラー"
\r
218 recdblist.addLog(pout, txtt, u"MP4Box追加ログ-コマンド")
\r
219 shutil.move(ptoutb,pout)
\r
224 txt=u"Cmd : "+cmd+"\n"
\r
225 txt2=commands.getoutput(cmd.encode('utf-8'))
\r
226 txt=txt+unicode(txt2,"utf-8")+"\n"
\r