OSDN Git Service

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