OSDN Git Service

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