OSDN Git Service

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