OSDN Git Service

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