OSDN Git Service

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