OSDN Git Service

implement encode tester.
[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.Commonlogex("Error", "b252ts(tv2avi.py)", str(type(inst)), str(inst))
83     status.changeB25Decoding(-1)
84 def ts2avi(pin, pout, opt):
85     status.changeEncoding(1)
86     try:
87         ts2x264.ts2x264(pin, pout, opt)
88     except Exception, inst:
89         recdblist.printutf8("error occures in tv2avi.py ts2avi")
90         recdblist.printutf8(str(type(inst)))
91         recdblist.printutf8(str(inst))
92     status.changeEncoding(-1)
93 def ts2raw(pin, pout, opt):
94     status.changeEncoding(1)
95     try:
96         ts2x264.ts2x264(pin, pout, opt)
97     except Exception, inst:
98         recdblist.printutf8("error occures in tv2avi.py ts2raw")
99         recdblist.printutf8(str(type(inst)))
100         recdblist.printutf8(str(inst))
101     status.changeEncoding(-1)
102 def dualaudio2sep(pin, delay):
103     """
104     delay is string
105     """
106     xvfb = configreader.getpath('xvfb-run')
107     bontsdemux = configreader.getpath('bontsdemux')
108     wine = configreader.getpath('wine')
109     bonpin = "Z:\\" + pin[1:]
110     outf = os.path.splitext(pin)[0]
111     bonpout = "Z:\\" + outf[1:]
112     exe = wine + ' ' + bontsdemux + " -i \"" + bonpin + "\" -delay " + delay + " -nd -sound 1 -o \"" + bonpout + "\" -start -quit"
113     recdblist.printutf8(exe)
114     exe = xvfb + ' -a ' + exe
115     txt=commands.getoutput(exe.encode('utf-8'))
116     try:
117         recdblist.addCommandLog(pin, u"BonTsDemux 第一音声取り出し", exe, txt)
118     except:
119         ""
120     ffpin = pin.replace(".ts", "")
121     ffpin1 = pin.replace("ts", "wav")
122     ffpin2 = pin.replace("ts", "m2v")
123     ffpout1 = ffpin + "_1.wav"
124     ffpout2 = ffpin + "_2.wav"
125     ffpout3 = ffpin + ".m2v"
126     shutil.move(ffpin1, ffpout1)
127     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay " + delay + " -nd -sound 2 -encode Demux\(wav\) -o \"" + bonpout + "\" -start -quit"
128     recdblist.printutf8(exe)
129     exe = xvfb + ' -a ' + exe
130     txt=commands.getoutput(exe.encode('utf-8'))
131     try:
132         recdblist.addCommandLog(pin, u"BonTsDemux 第二音声取り出し", exe, txt)
133     except:
134         ""
135     #os.system(exe)
136     shutil.move(ffpin1, ffpout2)
137     shutil.move(ffpin2, ffpout3)
138     ffpout21=ffpout1.replace(".wav",".mp3")
139     ffpout22=ffpout2.replace(".wav",".mp3")
140     useNero=0
141     try:
142         if configreader.getpath("useNeroAAC")=="1" and os.path.exists(configreader.getpath("NeroAAC")):
143             useNero=1
144     except:
145         useNero=0
146     if useNero==1:
147         ffpout21=ffpout1.replace(".wav",".aac")
148         ffpout22=ffpout2.replace(".wav",".aac")
149         tv2audio.wav2aac_nero(ffpout1, ffpout21)
150         tv2audio.wav2aac_nero(ffpout2, ffpout22)
151     else:
152         tv2audio.wav2mp3_lame(ffpout1, ffpout21)
153         tv2audio.wav2mp3_lame(ffpout2, ffpout22)
154     time.sleep(3)
155     os.remove(ffpout1)
156     os.remove(ffpout2)
157 def pentaaudio2sep(pin):
158     bontsdemux = configreader.getpath('bontsdemux')
159     wine = configreader.getpath('wine')
160     xvfb = configreader.getpath('xvfb-run')
161     bonpin = "Z:\\" + pin[1:]
162     outf = os.path.splitext(pin)[0]
163     bonpout = "Z:\\" + outf[1:]
164     exe = wine + ' ' + bontsdemux + " -i \"" + bonpin + "\" -delay 167 -nd -sound 3 -o \"" + bonpout + "\" -start -quit"
165     exe = xvfb + ' -a ' + exe
166     recdblist.printutf8(exe)
167     txt=commands.getoutput(exe.encode('utf-8'))
168     try:
169         recdblist.addCommandLog(pin, u"BonTsDemux5.1ch 第一音声取り出し", exe, txt)
170     except:
171         ""
172     ffpin = pin.replace(".ts", "")
173     ffpin1 = pin.replace("ts", "wav")
174     ffpin2 = pin.replace("ts", "m2v")
175     ffpout1 = ffpin + "_1.wav"
176     ffpout2 = ffpin + "_2.aac"
177     shutil.move(ffpin1, ffpout1)
178     exe = "ffmpeg -i '"+pin+"' -vn -f aac -acodec copy '"+ffpout2+"'"
179     recdblist.printutf8(exe)
180     txt=commands.getoutput(exe.encode('utf-8'))
181     try:
182         recdblist.addCommandLog(pin, u"FFmpeg 5.1ch 第二音声(raw AAC)取り出し", exe, txt)
183     except:
184         ""
185     ffpout21=ffpout1.replace(".wav",".mp3")
186     useNero=0
187     try:
188         if configreader.getpath("useNeroAAC")=="1" and os.path.exists(configreader.getpath("NeroAAC")):
189             useNero=1
190     except:
191         useNero=0
192     if useNero==1:
193         ffpout21=ffpout1.replace(".wav",".aac")
194         tv2audio.wav2aac_nero(ffpout1, ffpout21)
195     else:
196         tv2audio.wav2mp3_lame(ffpout1, ffpout21)
197     if not os.path.exists(ffpout21):
198         exe = wine + ' ' + bontsdemux + " -i \"" + bonpin + "\" -delay 167 -nd -sound 0 -o \"" + bonpout + "\" -start -quit"
199         exe = xvfb + ' -a ' + exe
200         recdblist.printutf8(exe)
201         txt=commands.getoutput(exe.encode('utf-8'))
202         try:
203             recdblist.addCommandLog(pin, u"BonTsDemux 修正版第二音声(2chDownmix)取り出し", exe, txt)
204         except:
205             ""
206         shutil.move(ffpin1, ffpout1)
207         if useNero==1:
208             ffpout21=ffpout1.replace(".wav",".aac")
209             tv2audio.wav2aac_nero(ffpout1, ffpout21)
210         else:
211             tv2audio.wav2mp3_lame(ffpout1, ffpout21)
212         time.sleep(3)
213     ffpout21=ffpout1.replace(".wav",".aac")
214     if os.path.exists(ffpout21):
215         if os.path.getsize(ffpout21)>10*1000*1000:
216             os.remove(ffpout1)
217
218 def singleaudio2sep(pin):
219     bontsdemux = configreader.getpath('bontsdemux')
220     wine = configreader.getpath('wine')
221     xvfb = configreader.getpath('xvfb-run')
222     bonpin = "Z:\\" + pin[1:]
223     outf = os.path.splitext(pin)[0]
224     bonpout = "Z:\\" + outf[1:]
225     exe = wine + ' ' + bontsdemux + " -i \"" + bonpin + "\" -delay 167 -nd -sound 0 -o \"" + bonpout + "\" -start -quit"
226     exe = xvfb + ' -a ' + exe
227     recdblist.printutf8(exe)
228     txt=commands.getoutput(exe.encode('utf-8'))
229     try:
230         recdblist.addCommandLog(pin, u"BonTsDemux 音声(wav化)取り出し", exe, txt)
231     except:
232         ""
233     ffpin = pin.replace(".ts", "")
234     ffpin1 = pin.replace("ts", "wav")
235     ffpin2 = pin.replace("ts", "m2v")
236     ffpout1 = ffpin + "_1.wav"
237     ffpout2 = ffpin + "_2.aac"
238     shutil.move(ffpin1, ffpout1)
239     exe = "ffmpeg -i '"+pin+"' -vn -f aac -acodec copy '"+ffpout2+"'"
240     recdblist.printutf8(exe)
241     txt=commands.getoutput(exe.encode('utf-8'))
242     try:
243         recdblist.addCommandLog(pin, u"FFmpeg 音声(raw AAC)取り出し", exe, txt)
244     except:
245         ""
246     ffpout21=ffpout1.replace(".wav",".mp3")
247     useNero=0
248     try:
249         if configreader.getpath("useNeroAAC")=="1" and os.path.exists(configreader.getpath("NeroAAC")):
250             useNero=1
251     except:
252         useNero=0
253     if useNero==1:
254         ffpout21=ffpout1.replace(".wav",".aac")
255         tv2audio.wav2aac_nero(ffpout1, ffpout21)
256     else:
257         tv2audio.wav2mp3_lame(ffpout1, ffpout21)
258     ffpout21=ffpout1.replace(".wav",".aac")
259     if os.path.exists(ffpout21):
260         if os.path.getsize(ffpout21)>10*1000*1000:
261             os.remove(ffpout1)