OSDN Git Service

6cab3876180eb3a7add9d2dbe81a1cabee1dcab6
[rec10/rec10-git.git] / rec10 / trunk / src / tv2avi.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
5 import shutil
6 import time
7 import chdb
8 import configreader
9 import datetime
10 import os
11 import re
12 import ts2x264
13 import ts2xvid
14 import tv2ts
15 import recdb
16
17 global Bitrate_HD
18 global Bitrate_FHD
19 global Bitrate_SD
20 global Bitrate_WQVGA
21 Bitrate_HD="2000"
22 Bitrate_FHD="2500"
23 Bitrate_SD="1250"
24 Bitrate_WQVGA="200"
25 def timetv2b25(pout, chtxt, btime, etime,opt):
26     bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
27     et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
28     extt = os.path.splitext(pout)
29     tsout = extt[0]
30     #tsout=pout####
31     #print "4"+tsout
32     tnow = datetime.datetime.now()
33     wt = bt-tnow
34     waitt = wt.seconds-5
35     time.sleep(waitt)
36     tnow = datetime.datetime.now()
37     dt = et-tnow
38     rectime = dt.seconds-10
39     rectime = str(rectime)
40     tv2ts.tv2b25ts(tsout+".ts.b25", chdb.chtxtsearch(chtxt)['ch'], rectime)
41 def b252ts(pout,chtxt,btime,etime,opt):
42     tv2ts.b252ts(pout+".ts", chdb.chtxtsearch(chtxt)['ch'], chdb.chtxtsearch(chtxt)['csch'])
43     aviin = tsout + ".ts"
44     dualaudio = 0
45     pentaaudio = 0
46     if re.search("5",opt):
47         pentaaudio = 1
48     if re.search("d",opt):
49         dualaudio = 1
50     if re.search("\[二\]", pout):
51         dualaudio = 1
52     elif re.search('(二)', pout):
53         dualaudio = 1
54     elif re.search('\(二\)', pout):
55         dualaudio = 1
56     if opt=="":
57         opts=""
58         if chdb.chtxtsearch(chtxt)['bctype'] == 'cs':
59             opts=opts+"S2"
60             if chtxt == "disch":
61                 ""
62             elif chtxt == "hisch":
63                 ""
64             else:
65                 opts=opts+"a"
66         else:
67             opts=opts+"Ha2"
68     else:
69         opts=opt
70     if re.search("x",opt):
71         makexvid = 0
72     else:
73         makexvid = 1
74     if dualaudio == 1:
75         dualaudiots2avi(aviin, tsout + "sa.avi")
76         aviin = tsout + "sa.avi"
77     if pentaaudio ==1:
78         dualaudiots2avi(aviin, tsout + "sa.avi")
79         aviin = tsout + "sa.avi"
80 def timetv2avi(pout, chtxt, btime, etime,opt):
81     bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
82     et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
83     extt = os.path.splitext(pout)
84     tsout = extt[0]
85     #tsout=pout####
86     #print "4"+tsout
87     tnow = datetime.datetime.now()
88     wt = bt-tnow
89     waitt = wt.seconds-5
90     time.sleep(waitt)
91     tnow = datetime.datetime.now()
92     dt = et-tnow
93     rectime = dt.seconds-10
94     rectime = str(rectime)
95     #print tsout+".ts,"+chdb.chtxtsearch(chtxt)['ch']+","+chdb.chtxtsearch(chtxt)['csch']+","+rectime
96     tv2ts.tv2ts(tsout + ".ts", chdb.chtxtsearch(chtxt)['ch'], chdb.chtxtsearch(chtxt)['csch'], rectime)
97     aviin = tsout + ".ts"
98     dualaudio = 0
99     pentaaudio = 0
100     if re.search("5",opt):
101         pentaaudio = 1
102     if re.search("d",opt):
103         dualaudio = 1
104     if re.search("\[二\]", pout):
105         dualaudio = 1
106     elif re.search('(二)', pout):
107         dualaudio = 1
108     elif re.search('\(二\)', pout):
109         dualaudio = 1
110     if opt=="":
111         opts=""
112         if chdb.chtxtsearch(chtxt)['bctype'] == 'cs':
113             opts=opts+"S2"
114             if chtxt == "disch":
115                 ""
116             elif chtxt == "hisch":
117                 ""
118             else:
119                 opts=opts+"a"
120         else:
121             opts=opts+"Ha2"
122     else:
123         opts=opt
124     if re.search("x",opt):
125         makexvid = 0
126     else:
127         makexvid = 1
128     if dualaudio == 1:
129         dualaudiots2avi(aviin, tsout + "sa.avi")
130         aviin = tsout + "sa.avi"
131     if pentaaudio ==1:
132         dualaudiots2avi(aviin, tsout + "sa.avi")
133         aviin = tsout + "sa.avi"
134     bt=datetime.datetime.now()+datetime.timedelta(seconds=600)
135     btime=bt.strftime("%Y-%m-%d %H:%M:%S")
136     et=datetime.datetime.now()+datetime.timedelta(seconds=600)
137     etime=et.strftime("%Y-%m-%d %H:%M:%S")
138     title=os.path.split(tsout)[1]
139     #try:
140     #    os.remove(aviin)
141     #except:
142     #    ""
143 def tv2avi(pout, chtxt, btime, etime,opt):
144     bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
145     et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
146     dt = et-bt
147     time = dt.seconds-10
148     time = str(time)
149     extt = os.path.splitext(pout)
150     tsout = extt[0]
151     tv2ts.tv2ts(tsout + ".ts", chdb.chtxtsearch(chtxt)['ch'], chdb.chtxtsearch(chtxt)['csch'], time)
152     aviin = tsout + ".ts"
153     dualaudio = 0
154     pentaaudio = 0 
155     if re.search("5",opt):
156         pentaaudio = 1
157     if re.search("d",opt):
158         dualaudio = 1
159     if re.search('\[二\]', pout):
160         dualaudio = 1
161     elif re.search('(二)', pout):
162         dualaudio = 1
163     elif re.search('\(二\)', pout):
164         dualaudio = 1
165     opts=""
166     if chdb.chtxtsearch(chtxt)['bctype'] == 'cs':
167         opts=opts+"S2"
168         if chtxt == "disch":
169             ""
170         elif chtxt == "hisch":
171             ""
172         else:
173             opts=opts+"a"
174     else:
175         opts=opts+"Ha2"
176     if dualaudio == 1:
177         dualaudiots2avi(aviin, tsout + "sa.avi")
178         aviin = tsout + "sa.avi"
179     if pentaaudio ==1:
180         dualaudiots2avi(aviin, tsout + "sa.avi")
181         aviin = tsout + "sa.avi"
182     ts2x264.ts2x264(aviin, pout,opts)
183     #os.remove(aviin)
184 def b252avi(pout, chtxt,opt):
185     extt = os.path.splitext(pout)
186     tsout = extt[0]
187     tv2ts.b252ts(tsout + ".ts", chdb.chtxtsearch(chtxt)['ch'], chdb.chtxtsearch(chtxt)['csch'])
188     aviin = tsout + ".ts"
189     dualaudio = 0
190     pentaaudio = 0
191     if re.search("5",opt):
192         pentaaudio = 1
193     if re.search("d",opt):
194         dualaudio = 1
195     if re.search('\[二\]', pout):
196         dualaudio = 1
197     elif re.search('(二)', pout):
198         dualaudio = 1
199     elif re.search('\(二\)', pout):
200         dualaudio = 1
201     if opt=="":
202         opts=""
203         if chdb.chtxtsearch(chtxt)['bctype'] == 'cs':
204             opts=opts+"S2"
205             if chtxt == "disch":
206                 ""
207             elif chtxt == "hisch":
208                 ""
209             else:
210                 opts=opts+"a"
211         else:
212             opts=opts+"Ha2"
213     else:
214         opts=opt
215     if re.search("x",opt):
216         makexvid = 0
217     else:
218         makexvid = 1
219     if dualaudio == 1:
220         dualaudiots2avi(aviin, tsout + "sa.avi")
221         aviin = tsout + "sa.avi"
222     if pentaaudio ==1:
223         dualaudiots2avi(aviin, tsout + "sa.avi")
224         aviin = tsout + "sa.avi"
225     if makexvid==1:
226         ts2xvid.ts2xvid(aviin, pout,opts)
227     else:
228         ts2x264.ts2x264(aviin, pout,opts)
229     #try:
230     #    os.remove(aviin)
231     #except:
232     #    ""
233
234 def dualaudiots2avi(pin, pout):
235     bontsdemux = configreader.getpath('bontsdemux')
236     wine = configreader.getpath('wine')
237     bonpin = "Z:\\" + pin[1:]
238     outf = os.path.splitext(pin)[0]
239     bonpout = "Z:\\" + outf[1:]
240     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay 167 -nd -sound 0 -o \"" + bonpout + "\" -start -quit"
241     #print exe
242     exe = 'xvfb-run -a ' + exe
243     #exe='export Display=:2&&'+exe
244     print exe
245     os.system(exe)
246     ffpin1 = pin.replace("ts", "wav")
247     ffpin2 = pin.replace("ts", "m2v")
248     exe = "ffmpeg -y  -i \'" + ffpin1 + "\' -i \'" + ffpin2 + "\' -r 29.97 -vsync 200 -vcodec copy -acodec libmp3lame -async 200 -ab 128k -f avi  \'" + pout + "\'"
249     #print exe
250     os.system(exe)
251     os.remove(ffpin1)
252     os.remove(ffpin2)
253 def dualaudio2sep(pin,pout,delay):
254     bontsdemux = configreader.getpath('bontsdemux')
255     wine = configreader.getpath('wine')
256     bonpin = "Z:\\" + pin[1:]
257     outf = os.path.splitext(pin)[0]
258     bonpout = "Z:\\" + outf[1:]
259     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay "+delay+" -nd -sound 1 -o \"" + bonpout + "\" -start -quit"
260     #print exe
261     exe = 'xvfb-run -a ' + exe
262     #exe='export Display=:2&&'+exe
263     print exe
264     os.system(exe)
265     ffpin=pin.replace(".ts","")
266     ffpin1 = pin.replace("ts", "wav")
267     ffpin2 = pin.replace("ts", "m2v")
268     ffpout1=ffpin+"1_delay"+delay+".wav"
269     ffpout2=ffpin+"2_delay"+delay+".wav"
270     shutil.move(ffpin1, ffpout1)
271     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay "+delay+" -nd -sound 2 -encode Demux\(wav\) -o \"" + bonpout + "\" -start -quit"
272     exe = 'xvfb-run -a ' + exe
273     os.system(exe)
274     shutil.move(ffpin1, ffpout2)
275     ffpout21=ffpout1.replace(".wav",".ogg")
276     ffpout22=ffpout2.replace(".wav",".ogg")
277     aexe1="ffmpeg -i "+ffpout1+" -vn -acodec vorbis -ar 48000 -ab 128k "+ffpout21
278     aexe2="ffmpeg -i "+ffpout2+" -vn -acodec vorbis -ar 48000 -ab 128k "+ffpout22
279     os.system(aexe1)
280     os.system(aexe2)
281
282     #exe = "ffmpeg -y  -i \'" + ffpin1 + "\' -i \'" + ffpin2 + "\' -r 29.97 -vsync 200 -vcodec copy -acodec libmp3lame -async 200 -ab 128k -f avi  \'" + pout + "\'"
283     #print exe
284     
285     #os.remove(ffpin1)
286     #os.remove(ffpin2)
287
288 def pentaaudiots2avi(pin, pout):
289     bontsdemux = configreader.getpath('bontsdemux')
290     wine = configreader.getpath('wine')
291     bonpin = "Z:\\" + pin[1:]
292     outf = os.path.splitext(pin)[0]
293     bonpout = "Z:\\" + outf[1:]
294     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay 167 -nd -sound 3 -o \"" + bonpout + "\" -start -quit"
295     #print exe
296     exe = 'xvfb-run -a ' + exe
297     #exe='export Display=:2&&'+exe
298     print exe
299     os.system(exe)
300     ffpin1 = pin.replace("ts", "wav")
301     ffpin2 = pin.replace("ts", "m2v")
302     exe = "ffmpeg -y  -i \'" + ffpin1 + "\' -i \'" + ffpin2 + "\' -r 29.97 -vsync 200 -vcodec copy -acodec libmp3lame -async 200 -ab 128k -f avi  \'" + pout + "\'"
303     #print exe
304     os.system(exe)
305     os.remove(ffpin1)
306     os.remove(ffpin2)
307