OSDN Git Service

implement caption/audio 0 byte error auto skip system.
[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         if re.search("5", opt):
63             pentaaudio = 1
64         if re.search("d", opt):
65             dualaudio = 1
66         if re.search(u"\[二\]", pout):
67             dualaudio = 1
68         elif re.search(u'(二)', pout):
69             dualaudio = 1
70         elif re.search(u'\(二\)', pout):
71             dualaudio = 1
72         if dualaudio == 1:
73             dualaudio2sep(aviin,"167")
74         if pentaaudio == 1:
75             pentaaudio2sep(aviin)
76     except Exception, inst:
77         recdblist.printutf8("Error happened in b252ts in tv2avi")
78         recdblist.printutf8(str(type(inst)))
79         recdblist.printutf8(str(inst))
80     status.changeB25Decoding(-1)
81 def ts2avi(pin, pout, opt):
82     status.changeEncoding(1)
83     try:
84         ts2x264.ts2x264(pin, pout, opt)
85     except Exception, inst:
86         recdblist.printutf8("error occures in tv2avi.py ts2avi")
87         recdblist.printutf8(str(type(inst)))
88         recdblist.printutf8(str(inst))
89     status.changeEncoding(-1)
90 def ts2raw(pin, pout, opt):
91     status.changeEncoding(1)
92     try:
93         ts2x264.ts2x264(pin, pout, opt)
94     except Exception, inst:
95         recdblist.printutf8("error occures in tv2avi.py ts2raw")
96         recdblist.printutf8(str(type(inst)))
97         recdblist.printutf8(str(inst))
98     status.changeEncoding(-1)
99 def dualaudiots2avi(pin, pout):
100     bontsdemux = configreader.getpath('bontsdemux')
101     bonpin = "Z:\\" + pin[1:]
102     outf = os.path.splitext(pin)[0]
103     bonpout = "Z:\\" + outf[1:]
104     xvfb = configreader.getpath('xvfb-run')
105     logpath=pin.replace("ts","log")
106     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay 167 -nd -sound 0 -o \"" + bonpout + "\" -start -quit"
107     #recdblist.printutf8(exe)
108     exe = xvfb + ' -a ' + exe
109     recdblist.printutf8(exe)
110     os.system(exe.encode('utf-8'))
111     ffpin1 = pin.replace("ts", "wav")
112     ffpin2 = pin.replace("ts", "m2v")
113     exe = "ffmpeg -y  -i \'" + ffpin1 + "\' -i \'" + ffpin2 + "\' -r 29.97 -vsync 200 -vcodec copy -acodec libmp3lame -async 200 -ab 128k -f avi  \'" + pout + "\'"
114     recdblist.printutf8(exe)
115     #commands.getoutput(exe)
116     os.system(exe.encode('utf-8'))
117     os.remove(ffpin1)
118     os.remove(ffpin2)
119 def dualaudio2sep(pin, delay):
120     """
121     delay is string
122     """
123     xvfb = configreader.getpath('xvfb-run')
124     bontsdemux = configreader.getpath('bontsdemux')
125     wine = configreader.getpath('wine')
126     bonpin = "Z:\\" + pin[1:]
127     outf = os.path.splitext(pin)[0]
128     bonpout = "Z:\\" + outf[1:]
129     exe = wine + ' ' + bontsdemux + " -i \"" + bonpin + "\" -delay " + delay + " -nd -sound 1 -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     ffpin = pin.replace(".ts", "")
138     ffpin1 = pin.replace("ts", "wav")
139     ffpin2 = pin.replace("ts", "m2v")
140     ffpout1 = ffpin + "_1.wav"
141     ffpout2 = ffpin + "_2.wav"
142     ffpout3 = ffpin + ".m2v"
143     shutil.move(ffpin1, ffpout1)
144     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay " + delay + " -nd -sound 2 -encode Demux\(wav\) -o \"" + bonpout + "\" -start -quit"
145     recdblist.printutf8(exe)
146     exe = xvfb + ' -a ' + exe
147     txt=commands.getoutput(exe.encode('utf-8'))
148     try:
149         recdblist.addlog(pin, txt, "dualaudio-sep1 BontsDemux")
150     except:
151         ""
152     #os.system(exe)
153     shutil.move(ffpin1, ffpout2)
154     shutil.move(ffpin2, ffpout3)
155     ffpout21=ffpout1.replace(".wav",".mp3")
156     ffpout22=ffpout2.replace(".wav",".mp3")
157     useNero=0
158     try:
159         if configreader.getpath("useNeroAAC")=="1" and os.path.exists(configreader.getpath("NeroAAC")):
160             useNero=1
161     except:
162         useNero=0
163     if useNero==1:
164         ffpout21=ffpout1.replace(".wav",".aac")
165         ffpout22=ffpout2.replace(".wav",".aac")
166         tv2audio.wav2aac_nero(ffpout1, ffpout21)
167         tv2audio.wav2aac_nero(ffpout2, ffpout22)
168     else:
169         tv2audio.wav2mp3_lame(ffpout1, ffpout21)
170         tv2audio.wav2mp3_lame(ffpout2, ffpout22)
171     time.sleep(3)
172     os.remove(ffpout1)
173     os.remove(ffpout2)
174 def pentaaudio2sep(pin):
175     bontsdemux = configreader.getpath('bontsdemux')
176     wine = configreader.getpath('wine')
177     xvfb = configreader.getpath('xvfb-run')
178     bonpin = "Z:\\" + pin[1:]
179     outf = os.path.splitext(pin)[0]
180     bonpout = "Z:\\" + outf[1:]
181     exe = wine + ' ' + bontsdemux + " -i \"" + bonpin + "\" -delay 167 -nd -sound 3 -o \"" + bonpout + "\" -start -quit"
182     exe = xvfb + ' -a ' + exe
183     recdblist.printutf8(exe)
184     txt=commands.getoutput(exe.encode('utf-8'))
185     try:
186         recdblist.addlog(pin, txt, "pentaaudio-sep1 BontsDemux")
187     except:
188         ""
189     ffpin = pin.replace(".ts", "")
190     ffpin1 = pin.replace("ts", "wav")
191     ffpin2 = pin.replace("ts", "m2v")
192     ffpout1 = ffpin + "_1.wav"
193     ffpout2 = ffpin + "_2.aac"
194     shutil.move(ffpin1, ffpout1)
195     exe = "ffmpeg -i '"+pin+"' -vn -f aac -acodec copy '"+ffpout2+"'"
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     xvfb = configreader.getpath('xvfb-run')
237     bonpin = "Z:\\" + pin[1:]
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