OSDN Git Service

sys.setdefaultencoding('UTF-8') required version.
[rec10/rec10-git.git] / rec10 / branches / 0.9.0 / src / tv2avi.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
5 import commands
6 import datetime
7 import os
8 import re
9 import shutil
10 import time
11
12 import chdb
13 import configreader
14 import status
15 import ts2x264
16 import ts2xvid
17 import tv2ts
18 global Bitrate_SD
19 global Bitrate_HD
20 global Bitrate_FHD
21 global Bitrate_Short
22 global Bitrate_LowHD
23 Bitrate_SD = "1250"
24 Bitrate_HD = "3750"
25 Bitrate_LowHD = "2500"
26 Bitrate_FHD = "5000"
27 Bitrate_Short = "1250"
28 #Bitrate_Tall = "2500"
29 #Bitrate_Grande = "3750"
30 #Bitrate_Venti = "5000"
31 def timetv2b25(pout, chtxt, btime, etime, opt):
32     """
33     poutはタイトル
34     """
35     bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
36     et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
37     extt = os.path.splitext(pout)
38     tsout = extt[0]
39     tnow = datetime.datetime.now()
40     wt = bt-tnow
41     waitt = wt.seconds-5
42     time.sleep(waitt)
43     tnow = datetime.datetime.now()
44     dt = et-tnow
45     rectime = dt.seconds-10
46     rectime = str(rectime)
47     tv2ts.tv2b25ts(tsout + ".ts.b25", chdb.chtxtsearch(chtxt)['ch'], rectime)
48 def b252ts(pout, chtxt, btime, etime, opt):
49     """
50     poutはタイトル(自動的にtitle.b25 title.tsと名前がつきます。)
51     """
52     #status.setB25Decoding(status.getB25Decoding() + 1)
53     status.changeB25Decoding(1)
54     try:
55         tv2ts.b252ts(pout + ".ts", chdb.chtxtsearch(chtxt)['ch'], chdb.chtxtsearch(chtxt)['csch'])
56         tsout = pout
57         aviin = pout + ".ts"
58         dualaudio = 0
59         pentaaudio = 0
60         if re.search("5", opt):
61             pentaaudio = 1
62         if re.search("d", opt):
63             dualaudio = 1
64         if re.search("\[二\]", pout):
65             dualaudio = 1
66         elif re.search('(二)', pout):
67             dualaudio = 1
68         elif re.search('\(二\)', pout):
69             dualaudio = 1
70         if opt == "":
71             opts = ""
72             if chdb.chtxtsearch(chtxt)['bctype'] == 'cs':
73                 opts = opts + "S2"
74                 if chtxt == "disch":
75                     ""
76                 elif chtxt == "hisch":
77                     ""
78                 else:
79                     opts = opts + "a"
80             else:
81                 opts = opts + "Ha2"
82         else:
83             opts = opt
84         if dualaudio == 1:
85             dualaudiots2avi(aviin, tsout + ".sa.avi")
86             aviin = tsout + ".sa.avi"
87         if pentaaudio == 1:
88             dualaudiots2avi(aviin, tsout + ".sa.avi")
89             aviin = tsout + ".sa.avi"
90     except:
91         ""
92     #status.setB25Decoding(status.getB25Decoding()-1)
93     status.changeB25Decoding(-1)
94 def timetv2avi(pout, chtxt, btime, etime, opt):
95     bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
96     et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
97     extt = os.path.splitext(pout)
98     tsout = extt[0]
99     tnow = datetime.datetime.now()
100     wt = bt-tnow
101     waitt = wt.seconds-5
102     time.sleep(waitt)
103     tnow = datetime.datetime.now()
104     dt = et-tnow
105     rectime = dt.seconds-10
106     rectime = str(rectime)
107     #print tsout+".ts,"+chdb.chtxtsearch(chtxt)['ch']+","+chdb.chtxtsearch(chtxt)['csch']+","+rectime
108     tv2ts.tv2ts(tsout + ".ts", chdb.chtxtsearch(chtxt)['ch'], chdb.chtxtsearch(chtxt)['csch'], rectime)
109     aviin = tsout + ".ts"
110     dualaudio = 0
111     pentaaudio = 0
112     if re.search("5", opt):
113         pentaaudio = 1
114     if re.search("d", opt):
115         dualaudio = 1
116     if re.search("\[二\]", pout):
117         dualaudio = 1
118     elif re.search('(二)', pout):
119         dualaudio = 1
120     elif re.search('\(二\)', pout):
121         dualaudio = 1
122     if opt == "":
123         opts = ""
124         if chdb.chtxtsearch(chtxt)['bctype'] == 'cs':
125             opts = opts + "S2"
126             if chtxt == "disch":
127                 ""
128             elif chtxt == "hisch":
129                 ""
130             else:
131                 opts = opts + "a"
132         else:
133             opts = opts + "Ha2"
134     else:
135         opts = opt
136     if dualaudio == 1:
137         dualaudiots2avi(aviin, tsout + ".sa.avi")
138         aviin = tsout + ".sa.avi"
139     if pentaaudio == 1:
140         dualaudiots2avi(aviin, tsout + ".sa.avi")
141         aviin = tsout + ".sa.avi"
142 def ts2avi(pin, pout, opt):
143     status.changeEncoding(1)
144     #status.setEncoding(status.getEncoding() + 1)
145     if re.search("x", opt):
146         try:
147             ts2xvid.ts2xvid(pin, pout, opt)
148         except:
149             ""
150     else:
151         try:
152             ts2x264.ts2x264(pin, pout, opt)
153         except:
154             ""
155     status.changeEncoding(-1)
156     #status.setEncoding(status.getEncoding()-1)
157 def tv2avi(pout, chtxt, btime, etime, opt):
158     bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
159     et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
160     dt = et-bt
161     time = dt.seconds-10
162     time = str(time)
163     extt = os.path.splitext(pout)
164     tsout = extt[0]
165     tv2ts.tv2ts(tsout + ".ts", chdb.chtxtsearch(chtxt)['ch'], chdb.chtxtsearch(chtxt)['csch'], time)
166     aviin = tsout + ".ts"
167     dualaudio = 0
168     pentaaudio = 0 
169     if re.search("5", opt):
170         pentaaudio = 1
171     if re.search("d", opt):
172         dualaudio = 1
173     if re.search('\[二\]', pout):
174         dualaudio = 1
175     elif re.search('(二)', pout):
176         dualaudio = 1
177     elif re.search('\(二\)', pout):
178         dualaudio = 1
179     opts = ""
180     if chdb.chtxtsearch(chtxt)['bctype'] == 'cs':
181         opts = opts + "S2"
182         if chtxt == "disch":
183             ""
184         elif chtxt == "hisch":
185             ""
186         else:
187             opts = opts + "a"
188     else:
189         opts = opts + "Ha2"
190     if dualaudio == 1:
191         dualaudiots2avi(aviin, tsout + ".sa.avi")
192         aviin = tsout + ".sa.avi"
193     if pentaaudio == 1:
194         dualaudiots2avi(aviin, tsout + ".sa.avi")
195         aviin = tsout + ".sa.avi"
196     ts2x264.ts2x264(aviin, pout, opts)
197     #os.remove(aviin)
198 def b252avi(pout, chtxt, opt):
199     extt = os.path.splitext(pout)
200     tsout = extt[0]
201     tv2ts.b252ts(tsout + ".ts", chdb.chtxtsearch(chtxt)['ch'], chdb.chtxtsearch(chtxt)['csch'])
202     aviin = tsout + ".ts"
203     dualaudio = 0
204     pentaaudio = 0
205     if re.search("5", opt):
206         pentaaudio = 1
207     if re.search("d", opt):
208         dualaudio = 1
209     if re.search('\[二\]', pout):
210         dualaudio = 1
211     elif re.search('(二)', pout):
212         dualaudio = 1
213     elif re.search('\(二\)', pout):
214         dualaudio = 1
215     if opt == "":
216         opts = ""
217         if chdb.chtxtsearch(chtxt)['bctype'] == 'cs':
218             opts = opts + "S2"
219             if chtxt == "disch":
220                 ""
221             elif chtxt == "hisch":
222                 ""
223             else:
224                 opts = opts + "a"
225         else:
226             opts = opts + "Ha2"
227     else:
228         opts = opt
229     if re.search("x", opt):
230         makexvid = 0
231     else:
232         makexvid = 1
233     if dualaudio == 1:
234         dualaudiots2avi(aviin, tsout + ".sa.avi")
235         aviin = tsout + ".sa.avi"
236     if pentaaudio == 1:
237         dualaudiots2avi(aviin, tsout + ".sa.avi")
238         aviin = tsout + ".sa.avi"
239     if makexvid == 1:
240         ts2xvid.ts2xvid(aviin, pout, opts)
241     else:
242         ts2x264.ts2x264(aviin, pout, opts)
243 def dualaudiots2avi(pin, pout):
244     bontsdemux = configreader.getpath('bontsdemux')
245     bonpin = "Z:\\" + pin[1:]
246     outf = os.path.splitext(pin)[0]
247     bonpout = "Z:\\" + outf[1:]
248     xvfb = configreader.getpath('xvfb-run')
249     logpath=pin.replace("ts","log")
250     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay 167 -nd -sound 0 -o \"" + bonpout + "\" -start -quit"
251     #print exe
252     exe = xvfb + ' -a ' + exe
253     print exe
254     os.system(exe)
255     ffpin1 = pin.replace("ts", "wav")
256     ffpin2 = pin.replace("ts", "m2v")
257     exe = "ffmpeg -y  -i \'" + ffpin1 + "\' -i \'" + ffpin2 + "\' -r 29.97 -vsync 200 -vcodec copy -acodec libmp3lame -async 200 -ab 128k -f avi  \'" + pout + "\'"
258     print exe
259     #commands.getoutput(exe)
260     os.system(exe)
261     os.remove(ffpin1)
262     os.remove(ffpin2)
263 def dualaudio2sep(pin, pout, delay):
264     """
265     delay is string
266     """
267     bontsdemux = configreader.getpath('bontsdemux')
268     wine = configreader.getpath('wine')
269     xvfb = configreader.getpath('xvfb-run')
270     bonpin = "Z:\\" + pin[1:]
271     outf = os.path.splitext(pin)[0]
272     bonpout = "Z:\\" + outf[1:]
273     exe = wine + ' ' + bontsdemux + " -i \"" + bonpin + "\" -delay " + delay + " -nd -sound 1 -o \"" + bonpout + "\" -start -quit"
274     #print exe
275     exe = xvfb + ' -a ' + exe
276     print exe
277     commands.getoutput(exe)
278     ffpin = pin.replace(".ts", "")
279     ffpin1 = pin.replace("ts", "wav")
280     ffpin2 = pin.replace("ts", "m2v")
281     ffpout1 = ffpin + "1_delay" + delay + ".wav"
282     ffpout2 = ffpin + "2_delay" + delay + ".wav"
283     ffpout3 = ffpin + "_delay" + delay + ".m2v"
284     shutil.move(ffpin1, ffpout1)
285     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay " + delay + " -nd -sound 2 -encode Demux\(wav\) -o \"" + bonpout + "\" -start -quit"
286     exe = xvfb + ' -a ' + exe
287     print exe
288     commands.getoutput(exe)
289     #os.system(exe)
290     shutil.move(ffpin1, ffpout2)
291     shutil.move(ffpin2, ffpout3)
292     #ffpout21=ffpout1.replace(".wav",".ogg")
293     #ffpout22=ffpout2.replace(".wav",".ogg")
294     #aexe1="ffmpeg -i "+ffpout1+" -vn -acodec vorbis -ar 48000 -ab 128k "+ffpout21
295     #aexe2="ffmpeg -i "+ffpout2+" -vn -acodec vorbis -ar 48000 -ab 128k "+ffpout22
296     aexe = "ffmpeg -i \"" + ffpout3 + "\" -i \"" + ffpout1 + "\" -i \"" + ffpout2 + "\" -r 29.97 -vsync 200 -vcodec copy -acodec copy -f avi " + pout + " -acodec copy -newaudio"
297     print aexe
298     #os.system(aexe1)
299     #os.system(aexe2)
300     commands.getoutput(aexe)
301     #exe = "ffmpeg -y  -i \'" + ffpin1 + "\' -i \'" + ffpin2 + "\' -r 29.97 -vsync 200 -vcodec copy -acodec libmp3lame -async 200 -ab 128k -f avi  \'" + pout + "\'"
302     #print exe
303     
304     #os.remove(ffpin1)
305     #os.remove(ffpin2)
306
307 def pentaaudiots2avi(pin, pout):
308     bontsdemux = configreader.getpath('bontsdemux')
309     bonpin = "Z:\\" + pin[1:]
310     xvfb = configreader.getpath('xvfb-run')
311     outf = os.path.splitext(pin)[0]
312     bonpout = "Z:\\" + outf[1:]
313     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay 167 -nd -sound 3 -o \"" + bonpout + "\" -start -quit"
314     #print exe
315     exe = xvfb + ' -a ' + exe
316     #exe = 'xvfb-run -a ' + exe
317     #exe='export Display=:2&&'+exe
318     print exe
319     commands.getoutput(exe)
320     #os.system(exe)
321     ffpin1 = pin.replace("ts", "wav")
322     ffpin2 = pin.replace("ts", "m2v")
323     exe = "ffmpeg -y  -i \'" + ffpin1 + "\' -i \'" + ffpin2 + "\' -r 29.97 -vsync 200 -vcodec copy -acodec libmp3lame -async 200 -ab 128k -f avi  \'" + pout + "\'"
324     print exe
325     commands.getoutput(exe)
326     #os.system(exe)
327     os.remove(ffpin1)
328     os.remove(ffpin2)
329