OSDN Git Service

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