OSDN Git Service

change encoding.
[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 -d 0 -A -S \""+pin+"\" --sync 0:0 "+duration+u"--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 -d 0 -A -S \""+pin+"\" --sync 0:0 "+duration+u"--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+execcomd(e1s)
68         else:
69             txt=txt+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 -d 0 -A -S \""+pin+"\" --sync 0:0 "+duration+u"--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 -d 0 -A -S \""+pin+"\" --sync 0:0 "+duration+u"--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+execcomd(e1s)
89         else:
90             txt=txt+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 -d 0 -A -S \""+pin+"\" --sync 0:0 "+duration+u"--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 -d 0 -A -S \""+pin+"\" --sync 0:0 "+duration+u"--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+execcomd(e2s)
106         else:
107             txt=txt+execcomd(e2)
108     recdblist.addlog(pout, txt, "mkvmerge-log")
109     logo=pout.replace(".mkv",".log")
110     if status.getSettings_auto_del_tmp()==1:
111         if os.path.exists(pout):
112             if os.path.getsize(pout)>100*1000*1000:
113                 deltitle(dir,title)
114 def avi2mkv(pin,pout,opt):
115     dir=os.path.split(pout)[0]
116     title=os.path.split(pout)[1]
117     title=os.path.splitext(title)[0]
118     isdualaac=0
119     ispentaaudio=0
120     if re.search("d",opt):
121         isdualaac=1
122     elif re.search("5",opt):
123         ispentaaudio=1
124     exe = configreader.getpath("mkvmerge")
125     txt=""
126     os.environ['LANG']="ja_JP.UTF-8"
127     if isdualaac==1:
128         wineexe=configreader.getpath("wine")
129         pincap=pin.replace(".avi",".srt")
130         cap2ass=configreader.getpath("Caption2Ass")
131         pints=pin.replace(".avi",".ts")
132         paac1=pin.replace(".avi","_1.aac")
133         paac2=pin.replace(".avi","_2.aac")
134         recdblist.printutf8(paac1)
135         if not os.path.exists(paac1):
136             paac1=pin.replace(".avi","_1.mp3")
137         if not os.path.exists(paac2):
138             paac2=pin.replace(".avi","_2.mp3")
139         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
140         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"
141         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"
142         if os.path.isfile(cap2ass):
143             txt=txt+execcomd(e0)
144         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
145             txt=txt+execcomd(e1s)
146         else:
147             txt=txt+execcomd(e1)
148     elif ispentaaudio==1:
149         wineexe=configreader.getpath("wine")
150         pincap=pin.replace(".avi",".srt")
151         cap2ass=configreader.getpath("Caption2Ass")
152         pints=pin.replace(".avi",".ts")
153         paac1=pin.replace(".avi","_1.aac")
154         paac2=pin.replace(".avi","_2.aac")
155         recdblist.printutf8(paac1)
156         if not os.path.exists(paac1):
157             paac1=pin.replace(".avi","_1.mp3")
158         if not os.path.exists(paac2):
159             paac2=pin.replace(".avi","_2.mp3")
160         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
161         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"
162         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"
163         if os.path.isfile(cap2ass):
164             txt=txt+execcomd(e0)
165         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
166             txt=txt+execcomd(e1s)
167         else:
168             txt=txt+execcomd(e1)
169     else:
170         wineexe=configreader.getpath("wine")
171         pincap=pin.replace(".avi",".srt")
172         cap2ass=configreader.getpath("Caption2Ass")
173         pints=pin.replace(".avi",".ts")
174         pinaac=pin.replace(".avi",".aac")
175         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
176         e1="ffmpeg -i \""+pints+"\" -vn -f aac -acodec copy \""+pinaac+"\""
177         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"
178         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"
179         if os.path.isfile(cap2ass):
180             txt=txt+execcomd(e0)
181         txt=txt+execcomd(e1)
182         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
183             txt=txt+execcomd(e2s)
184         else:
185             txt=txt+execcomd(e2)
186     recdblist.addlog(pout, txt, "mkvmerge-log")
187     logo=pout.replace(".mkv",".log")
188     if status.getSettings_auto_del_tmp()==1:
189         if os.path.exists(pout):
190             if os.path.getsize(pout)>100*1000*1000:
191                 deltitle(dir,title)
192 def execcomd(cmd):
193     txt=""
194     cmdt=u"nice -n 17 "+cmd
195     try:
196         txt=u"Cmd : "+cmd+"\n"
197         txt2=commands.getoutput(cmdt.encode('utf-8'))
198         txt=txt+unicode(txt2,"utf-8")+"\n"
199     except:
200         ""
201     return txt
202 def deltitle(path,title):
203     dp=[]
204     dp.append(os.path.join(path,title+".avi"))
205     dp.append(os.path.join(path,title+".120.avi"))
206     dp.append(os.path.join(path,title+".noodml.avi"))
207     dp.append(os.path.join(path,title+".aac"))
208     dp.append(os.path.join(path,title+".m2v"))
209     dp.append(os.path.join(path,title+"_1.aac"))
210     dp.append(os.path.join(path,title+"_2.aac"))
211     dp.append(os.path.join(path,title+"_1.mp3"))
212     dp.append(os.path.join(path,title+"_2.mp3"))
213     dp.append(os.path.join(path,title+".srt"))
214     if os.path.exists(os.path.join(path,title+".ts"))and os.path.exists(os.path.join(path,title+".ts.b25")):
215         if os.path.getsize(os.path.join(path,title+".ts"))*10>os.path.getsize(os.path.join(path,title+".ts.b25")):
216             if os.path.getsize(os.path.join(path,title+".ts"))>1000*1000*100:
217                 dp.append(os.path.join(path,title+".ts.b25"))
218     if os.path.exists(os.path.join(path,title+".mkv")):
219         if os.path.getsize(os.path.join(path,title+".mkv"))>1000*1000*10:
220             for ip in dp:
221                 try:
222                     os.remove(ip)
223                 except:
224                     ""
225