OSDN Git Service

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