OSDN Git Service

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