OSDN Git Service

fix some miss.
[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 def timetv2b25(pout, chtxt, btime, etime, opt):
33     """
34     poutはタイトル
35     """
36     bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
37     et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
38     extt = os.path.splitext(pout)
39     tsout = extt[0]
40     tnow = datetime.datetime.now()
41     wt = bt-tnow
42     waitt = wt.seconds-5
43     time.sleep(waitt)
44     tnow = datetime.datetime.now()
45     dt = et-tnow
46     rectime = dt.seconds-10
47     rectime = str(rectime)
48     tv2ts.tv2b25ts(tsout + ".ts.b25", chdb.chtxtsearch(chtxt)['ch'], rectime)
49 def b252ts(pout, chtxt, btime, etime, opt):
50     """
51     poutはタイトル(自動的にtitle.b25 title.tsと名前がつきます。)
52     """
53     #status.setB25Decoding(status.getB25Decoding() + 1)
54     status.changeB25Decoding(1)
55     try:
56         tv2ts.b252ts(pout + ".ts", chdb.chtxtsearch(chtxt)['ch'], chdb.chtxtsearch(chtxt)['csch'])
57         tsout = pout
58         aviin = pout + ".ts"
59         dualaudio = 0
60         pentaaudio = 0
61         if re.search("5", opt):
62             pentaaudio = 1
63         if re.search("d", opt):
64             dualaudio = 1
65         if re.search(u"\[二\]", pout):
66             dualaudio = 1
67         elif re.search(u'(二)', pout):
68             dualaudio = 1
69         elif re.search(u'\(二\)', pout):
70             dualaudio = 1
71         if dualaudio == 1:
72             dualaudio2sep(aviin,"167")
73         if pentaaudio == 1:
74             pentaaudio2sep(aviin)
75     except Exception, inst:
76         recdblist.printutf8("Error happened in b252ts in tv2avi")
77         recdblist.printutf8(str(type(inst)))
78         recdblist.printutf8(str(inst))
79     status.changeB25Decoding(-1)
80 def ts2avi(pin, pout, opt):
81     status.changeEncoding(1)
82     try:
83         ts2x264.ts2x264(pin, pout, opt)
84     except Exception, inst:
85         recdblist.printutf8("error occures in tv2avi.py ts2avi")
86         recdblist.printutf8(str(type(inst)))
87         recdblist.printutf8(str(inst))
88     status.changeEncoding(-1)
89 def ts2raw(pin, pout, opt):
90     status.changeEncoding(1)
91     try:
92         ts2x264.ts2x264(pin, pout, opt)
93     except Exception, inst:
94         recdblist.printutf8("error occures in tv2avi.py ts2raw")
95         recdblist.printutf8(str(type(inst)))
96         recdblist.printutf8(str(inst))
97     status.changeEncoding(-1)
98 def dualaudiots2avi(pin, pout):
99     bontsdemux = configreader.getpath('bontsdemux')
100     bonpin = "Z:\\" + pin[1:]
101     outf = os.path.splitext(pin)[0]
102     bonpout = "Z:\\" + outf[1:]
103     xvfb = configreader.getpath('xvfb-run')
104     logpath=pin.replace("ts","log")
105     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay 167 -nd -sound 0 -o \"" + bonpout + "\" -start -quit"
106     #recdblist.printutf8(exe)
107     exe = xvfb + ' -a ' + exe
108     recdblist.printutf8(exe)
109     os.system(exe.encode('utf-8'))
110     ffpin1 = pin.replace("ts", "wav")
111     ffpin2 = pin.replace("ts", "m2v")
112     exe = "ffmpeg -y  -i \'" + ffpin1 + "\' -i \'" + ffpin2 + "\' -r 29.97 -vsync 200 -vcodec copy -acodec libmp3lame -async 200 -ab 128k -f avi  \'" + pout + "\'"
113     recdblist.printutf8(exe)
114     #commands.getoutput(exe)
115     os.system(exe.encode('utf-8'))
116     os.remove(ffpin1)
117     os.remove(ffpin2)
118 def dualaudio2sep(pin, delay):
119     """
120     delay is string
121     """
122     bontsdemux = configreader.getpath('bontsdemux')
123     wine = configreader.getpath('wine')
124     xvfb = configreader.getpath('xvfb-run')
125     bonpin = "Z:\\" + pin[1:]
126     outf = os.path.splitext(pin)[0]
127     bonpout = "Z:\\" + outf[1:]
128     exe = wine + ' ' + bontsdemux + " -i \"" + bonpin + "\" -delay " + delay + " -nd -sound 1 -o \"" + bonpout + "\" -start -quit"
129     exe = xvfb + ' -a ' + exe
130     recdblist.printutf8(exe)
131     txt=commands.getoutput(exe.encode('utf-8'))
132     try:
133         recdblist.addlog(pin, txt, "dualaudio-sep1 BontsDemux")
134     except:
135         ""
136     ffpin = pin.replace(".ts", "")
137     ffpin1 = pin.replace("ts", "wav")
138     ffpin2 = pin.replace("ts", "m2v")
139     ffpout1 = ffpin + "_1.wav"
140     ffpout2 = ffpin + "_2.wav"
141     ffpout3 = ffpin + ".m2v"
142     shutil.move(ffpin1, ffpout1)
143     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay " + delay + " -nd -sound 2 -encode Demux\(wav\) -o \"" + bonpout + "\" -start -quit"
144     exe = xvfb + ' -a ' + exe
145     recdblist.printutf8(exe)
146     txt=commands.getoutput(exe.encode('utf-8'))
147     try:
148         recdblist.addlog(pin, txt, "dualaudio-sep1 BontsDemux")
149     except:
150         ""
151     #os.system(exe)
152     shutil.move(ffpin1, ffpout2)
153     shutil.move(ffpin2, ffpout3)
154     ffpout21=ffpout1.replace(".wav",".mp3")
155     ffpout22=ffpout2.replace(".wav",".mp3")
156     useNero=0
157     try:
158         if configreader.getpath("useNeroAAC")=="1" and os.path.exists(configreader.getpath("NeroAAC")):
159             useNero=1
160     except:
161         useNero=0
162     if useNero==1:
163         ffpout21=ffpout1.replace(".wav",".aac")
164         ffpout22=ffpout2.replace(".wav",".aac")
165         tv2audio.wav2aac_nero(ffpout1, ffpout21)
166         tv2audio.wav2aac_nero(ffpout2, ffpout22)
167     else:
168         tv2audio.wav2mp3_lame(ffpout1, ffpout21)
169         tv2audio.wav2mp3_lame(ffpout2, ffpout22)
170     time.sleep(3)
171     os.remove(ffpout1)
172     os.remove(ffpout2)
173 def pentaaudio2sep(pin):
174     bontsdemux = configreader.getpath('bontsdemux')
175     wine = configreader.getpath('wine')
176     xvfb = configreader.getpath('xvfb-run')
177     bonpin = "Z:\\" + pin[1:]
178     outf = os.path.splitext(pin)[0]
179     bonpout = "Z:\\" + outf[1:]
180     exe = wine + ' ' + bontsdemux + " -i \"" + bonpin + "\" -delay 167 -nd -sound 3 -o \"" + bonpout + "\" -start -quit"
181     exe = xvfb + ' -a ' + exe
182     recdblist.printutf8(exe)
183     txt=commands.getoutput(exe.encode('utf-8'))
184     try:
185         recdblist.addlog(pin, txt, "pentaaudio-sep1 BontsDemux")
186     except:
187         ""
188     ffpin = pin.replace(".ts", "")
189     ffpin1 = pin.replace("ts", "wav")
190     ffpin2 = pin.replace("ts", "m2v")
191     ffpout1 = ffpin + "_1.wav"
192     ffpout2 = ffpin + "_2.aac"
193     shutil.move(ffpin1, ffpout1)
194     exe = "ffmpeg -i '"+pin+"' -vn -f aac -acodec copy '"+ffpout2+"'"
195     exe = xvfb + ' -a ' + exe
196     recdblist.printutf8(exe)
197     txt=commands.getoutput(exe.encode('utf-8'))
198     try:
199         recdblist.addlog(pin, txt, "pentaaudio-sep2(raw aac) ffmpeg")
200     except:
201         ""
202     ffpout21=ffpout1.replace(".wav",".mp3")
203     useNero=0
204     try:
205         if configreader.getpath("useNeroAAC")=="1" and os.path.exists(configreader.getpath("NeroAAC")):
206             useNero=1
207     except:
208         useNero=0
209     if useNero==1:
210         ffpout21=ffpout1.replace(".wav",".aac")
211         tv2audio.wav2aac_nero(ffpout1, ffpout21)
212     else:
213         tv2audio.wav2mp3_lame(ffpout1, ffpout21)
214     if not os.path.exists(ffpout21):
215         exe = wine + ' ' + bontsdemux + " -i \"" + bonpin + "\" -delay 167 -nd -sound 0 -o \"" + bonpout + "\" -start -quit"
216         exe = xvfb + ' -a ' + exe
217         recdblist.printutf8(exe)
218         txt=commands.getoutput(exe.encode('utf-8'))
219         try:
220             recdblist.addlog(pin, txt, "pentaaudio-sep Downmix (5.1ch error) BontsDemux")
221         except:
222             ""
223         shutil.move(ffpin1, ffpout1)
224         if useNero==1:
225             ffpout21=ffpout1.replace(".wav",".aac")
226             tv2audio.wav2aac_nero(ffpout1, ffpout21)
227         else:
228             tv2audio.wav2mp3_lame(ffpout1, ffpout21)
229         time.sleep(3)
230     ffpout21=ffpout1.replace(".wav",".aac")
231     if os.path.exists(ffoput21):
232         if os.path.getsize(ffpout21)>10*1000*1000:
233             os.remove(ffpout1)
234 def pentaaudiots2avi(pin, pout):
235     bontsdemux = configreader.getpath('bontsdemux')
236     bonpin = "Z:\\" + pin[1:]
237     xvfb = configreader.getpath('xvfb-run')
238     outf = os.path.splitext(pin)[0]
239     bonpout = "Z:\\" + outf[1:]
240     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay 167 -nd -sound 3 -o \"" + bonpout + "\" -start -quit"
241     exe = xvfb + ' -a ' + exe
242     recdblist.printutf8(exe)
243     txt=u"Cmd : "+exe+"\n"
244     txt=txt+commands.getoutput(exe.encode('utf-8'))
245     recdblist.addlog(pin, txt, "5.1ch audio Demux log")
246     ffpin1 = pin.replace("ts", "wav")
247     ffpin2 = pin.replace("ts", "m2v")
248
249
250     exe = u"ffmpeg -y  -i \"" + ffpin1 + u"\" -i \'" + ffpin2 + u"\' -r 29.97 -vsync 200 -vcodec copy -acodec libmp3lame -async 200 -ab 128k -f avi  \"" + pout + "\""
251     recdblist.printutf8(exe)
252     txt=u"Cmd : "+exe+"\n"
253     txtt=commands.getoutput(exe.encode('utf-8'))
254     try:
255         txt=txt+txtt
256     except:
257         txt=txt+"skip logging (not UTF-8)"
258     recdblist.addlog(pin, txt, "5.1ch audio Mux log")
259     if os.path.exists(pout):
260         if os.path.getsize(pout)>100*1000:
261             os.remove(ffpin1)
262             os.remove(ffpin2)
263