OSDN Git Service

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