OSDN Git Service

implement new mp4 encoding(beta2).
[rec10/rec10-git.git] / rec10 / trunk / src / tv2mkv.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
5 import status
6 import os
7 import commands
8 import tv2avi
9 import os.path
10 import time
11 import re
12 import recdblist
13 import configreader
14 def ts2mkv(pin, pout, opt):
15     dir=os.path.split(pout)[0]
16     title=os.path.split(pout)[1]
17     title=os.path.splitext(title)[0]
18     ##use avi
19     """tpavi=os.path.join(dir,title+".avi")
20     tpmkv=os.path.join(dir, title+".mkv")
21     tv2avi.ts2avi(pin, tpavi, opt)
22     time.sleep(10)
23     avi2mkv(tpavi,tpmkv,opt)"""
24     tpraw=os.path.join(dir, title+".264")
25     tpmkv=os.path.join(dir, title+".mkv")
26     tv2avi.ts2raw(pin, tpraw, opt)
27     time.sleep(10)
28     raw2mkv(tpraw, tpmkv, opt)
29 def raw2mkv(pin,pout,opt):#x264
30     dir=os.path.split(pout)[0]
31     title=os.path.split(pout)[1]
32     title=os.path.splitext(title)[0]
33     duration="--default-duration 0:30000/1001fps "
34     isdualaac=0
35     ispentaaudio=0
36     if re.search("a",opt):
37         duration="--default-duration 0:24000/1001fps "
38     elif re.search("v",opt):
39         duration="--default-duration 0:24000/1001fps "
40     if re.search("d",opt):
41         isdualaac=1
42         duration="--default-duration 0:30000/1001fps "##ffmpegが24fpsに対応していないための措置
43     elif re.search("5",opt):
44         ispentaaudio=1
45         duration="--default-duration 0:30000/1001fps "##ffmpegが24fpsに対応していないための措置
46     exe = configreader.getpath("mkvmerge")
47     txt=""
48     os.environ['LANG']="ja_JP.UTF-8"
49     if isdualaac==1:
50         wineexe=configreader.getpath("wine")
51         pincap=pin.replace(".264",".srt")
52         cap2ass=configreader.getpath("Caption2Ass")
53         pints=pin.replace(".264",".ts")
54         paac1=pin.replace(".264","_1.aac")
55         paac2=pin.replace(".264","_2.aac")
56         recdblist.printutf8(paac1)
57         if not os.path.exists(paac1):
58             paac1=pin.replace(".264","_1.mp3")
59         if not os.path.exists(paac2):
60             paac2=pin.replace(".264","_2.mp3")
61         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
62         e1=exe +" -o \""+pout+u"\" --forced-track 0:no "+duration+"-d 0 -A -S \""+pin+"\" --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --track-order 0:0,1:1,2:1"
63         e1s=exe +" -o \""+pout+u"\" --forced-track 0:no "+duration+"-d 0 -A -S \""+pin+"\" --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --forced-track 0:no -s 0 -D -A \""+pincap+"\" --track-order 0:0,1:1,2:1,3:0"
64         if os.path.isfile(cap2ass):
65             txt=txt+execcomd(e0)
66         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
67             txt=txt+u"\n Cmd:"+e1s+u"\n"+execcomd(e1s)
68         else:
69             txt=txt+u"\n Cmd:"+e1+u"\n"+execcomd(e1)
70     elif ispentaaudio==1:
71         wineexe=configreader.getpath("wine")
72         pincap=pin.replace(".264",".srt")
73         cap2ass=configreader.getpath("Caption2Ass")
74         pints=pin.replace(".264",".ts")
75         paac1=pin.replace(".264","_1.aac")
76         paac2=pin.replace(".264","_2.aac")
77         recdblist.printutf8(paac1)
78         if not os.path.exists(paac1):
79             paac1=pin.replace(".264","_1.mp3")
80         if not os.path.exists(paac2):
81             paac2=pin.replace(".264","_2.mp3")
82         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
83         e1=exe +" -o \""+pout+u"\" --forced-track 0:no "+duration+"-d 0 -A -S \""+pin+"\" --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --track-order 0:0,1:1,2:1"
84         e1s=exe +" -o \""+pout+u"\" --forced-track 0:no "+duration+"-d 0 -A -S \""+pin+"\" --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --forced-track 0:no -s 0 -D -A \""+pincap+"\" --track-order 0:0,1:1,2:1,3:0"
85         if os.path.isfile(cap2ass):
86             txt=txt+execcomd(e0)
87         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
88             txt=txt+u"\n Cmd:"+e1s+u"\n"+execcomd(e1s)
89         else:
90             txt=txt+u"\n Cmd:"+e1+u"\n"+execcomd(e1)
91     else:
92         wineexe=configreader.getpath("wine")
93         pincap=pin.replace(".264",".srt")
94         cap2ass=configreader.getpath("Caption2Ass")
95         pints=pin.replace(".264",".ts")
96         pinaac=pin.replace(".264",".aac")
97         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
98         e1="ffmpeg -i \""+pints+"\" -vn -f aac -acodec copy \""+pinaac+"\""
99         e2=exe +" -o \""+pout+u"\" --forced-track 0:no "+duration+"-d 0 -A -S \""+pin+"\" --forced-track 0:no -a 0 -D -S \""+pinaac+"\" --track-order 0:0,1:0"
100         e2s=exe +" -o \""+pout+u"\" --forced-track 0:no "+duration+"-d 0 -A -S \""+pin+"\" --forced-track 0:no -a 0 -D -S \""+pinaac+"\" --forced-track 0:no -s 0 -D -A \""+pincap+"\" --track-order 0:0,1:0,2:0"
101         if os.path.isfile(cap2ass):
102             txt=txt+execcomd(e0)
103         txt=txt+execcomd(e1)
104         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
105             txt=txt+u"\n Cmd:"+e2s+"\n"+execcomd(e2s)
106             recdblist.printutf8(e2s)
107         else:
108             txt=txt+u"\n Cmd:"+e2+"\n"+execcomd(e2)
109             recdblist.printutf8(e2)
110     recdblist.addlog(pout, txt, "mkvmerge-log")
111     logo=pout.replace(".mkv",".log")
112     if status.getSettings_auto_del_tmp()==1:
113         if os.path.exists(pout):
114             if os.path.getsize(pout)>100*1000*1000:
115                 deltitle(dir,title)
116 def avi2mkv(pin,pout,opt):
117     dir=os.path.split(pout)[0]
118     title=os.path.split(pout)[1]
119     title=os.path.splitext(title)[0]
120     isdualaac=0
121     ispentaaudio=0
122     if re.search("d",opt):
123         isdualaac=1
124     elif re.search("5",opt):
125         ispentaaudio=1
126     exe = configreader.getpath("mkvmerge")
127     txt=""
128     os.environ['LANG']="ja_JP.UTF-8"
129     if isdualaac==1:
130         wineexe=configreader.getpath("wine")
131         pincap=pin.replace(".avi",".srt")
132         cap2ass=configreader.getpath("Caption2Ass")
133         pints=pin.replace(".avi",".ts")
134         paac1=pin.replace(".avi","_1.aac")
135         paac2=pin.replace(".avi","_2.aac")
136         recdblist.printutf8(paac1)
137         if not os.path.exists(paac1):
138             paac1=pin.replace(".avi","_1.mp3")
139         if not os.path.exists(paac2):
140             paac2=pin.replace(".avi","_2.mp3")
141         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
142         e1=exe +" -o \""+pout+u"\"  --forced-track 0:no -d 0 -A -S \""+pin+"\" --sync 0:0 --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --track-order 0:0,1:1,2:1"
143         e1s=exe +" -o \""+pout+u"\"  --forced-track 0:no -d 0 -A -S \""+pin+"\" --sync 0:0 --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --forced-track 0:no -s 0 -D -A \""+pincap+"\" --track-order 0:0,1:1,2:1,3:0"
144         if os.path.isfile(cap2ass):
145             txt=txt+execcomd(e0)
146         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
147             txt=txt+execcomd(e1s)
148         else:
149             txt=txt+execcomd(e1)
150     elif ispentaaudio==1:
151         wineexe=configreader.getpath("wine")
152         pincap=pin.replace(".avi",".srt")
153         cap2ass=configreader.getpath("Caption2Ass")
154         pints=pin.replace(".avi",".ts")
155         paac1=pin.replace(".avi","_1.aac")
156         paac2=pin.replace(".avi","_2.aac")
157         recdblist.printutf8(paac1)
158         if not os.path.exists(paac1):
159             paac1=pin.replace(".avi","_1.mp3")
160         if not os.path.exists(paac2):
161             paac2=pin.replace(".avi","_2.mp3")
162         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
163         e1=exe +" -o \""+pout+u"\"  --forced-track 0:no -d 0 -A -S \""+pin+"\" --sync 0:0 --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --track-order 0:0,1:1,2:1"
164         e1s=exe +" -o \""+pout+u"\"  --forced-track 0:no -d 0 -A -S \""+pin+"\" --sync 0:0 --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --forced-track 0:no -s 0 -D -A \""+pincap+"\" --track-order 0:0,1:1,2:1,3:0"
165         if os.path.isfile(cap2ass):
166             txt=txt+execcomd(e0)
167         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
168             txt=txt+execcomd(e1s)
169         else:
170             txt=txt+execcomd(e1)
171     else:
172         wineexe=configreader.getpath("wine")
173         pincap=pin.replace(".avi",".srt")
174         cap2ass=configreader.getpath("Caption2Ass")
175         pints=pin.replace(".avi",".ts")
176         pinaac=pin.replace(".avi",".aac")
177         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
178         e1="ffmpeg -i \""+pints+"\" -vn -f aac -acodec copy \""+pinaac+"\""
179         e2=exe +" -o \""+pout+u"\"  --forced-track 0:no -d 0 -A -S \""+pin+"\" --sync 0:0 --forced-track 0:no -a 0 -D -S \""+pinaac+"\" --track-order 0:0,1:0"
180         e2s=exe +" -o \""+pout+u"\"  --forced-track 0:no -d 0 -A -S \""+pin+"\" --sync 0:0 --forced-track 0:no -a 0 -D -S \""+pinaac+"\" --forced-track 0:no -s 0 -D -A \""+pincap+"\" --track-order 0:0,1:0,2:0"
181         if os.path.isfile(cap2ass):
182             txt=txt+execcomd(e0)
183         txt=txt+execcomd(e1)
184         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
185             txt=txt+execcomd(e2s)
186         else:
187             txt=txt+execcomd(e2)
188     recdblist.addlog(pout, txt, "mkvmerge-log")
189     logo=pout.replace(".mkv",".log")
190     if status.getSettings_auto_del_tmp()==1:
191         if os.path.exists(pout):
192             if os.path.getsize(pout)>100*1000*1000:
193                 deltitle(dir,title)
194 def execcomd(cmd):
195     txt=""
196     cmdt=u"nice -n 17 "+cmd
197     try:
198         txt=u"Cmd : "+cmd+"\n"
199         txt2=commands.getoutput(cmdt.encode('utf-8'))
200         txt=txt+unicode(txt2,"utf-8")+"\n"
201     except:
202         ""
203     return txt
204 def deltitle(path,title):
205     dp=[]
206     dp.append(os.path.join(path,title+".avi"))
207     dp.append(os.path.join(path,title+".264"))
208     dp.append(os.path.join(path,title+".120.avi"))
209     dp.append(os.path.join(path,title+".noodml.avi"))
210     dp.append(os.path.join(path,title+".aac"))
211     dp.append(os.path.join(path,title+".m2v"))
212     dp.append(os.path.join(path,title+"_1.aac"))
213     dp.append(os.path.join(path,title+"_2.aac"))
214     dp.append(os.path.join(path,title+"_1.mp3"))
215     dp.append(os.path.join(path,title+"_2.mp3"))
216     dp.append(os.path.join(path,title+".srt"))
217     if os.path.exists(os.path.join(path,title+".ts"))and os.path.exists(os.path.join(path,title+".ts.b25")):
218         if os.path.getsize(os.path.join(path,title+".ts"))*10>os.path.getsize(os.path.join(path,title+".ts.b25")):
219             if os.path.getsize(os.path.join(path,title+".ts"))>1000*1000*100:
220                 dp.append(os.path.join(path,title+".ts.b25"))
221     if os.path.exists(os.path.join(path,title+".mkv")):
222         if os.path.getsize(os.path.join(path,title+".mkv"))>1000*1000*10:
223             for ip in dp:
224                 try:
225                     os.remove(ip)
226                 except:
227                     ""
228