OSDN Git Service

add comment and log.
[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 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 tv2ts
17 import recdblist
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     #recdblist.printutf8(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(u"\[二\]", pout):
117         dualaudio = 1
118     elif re.search(u'(二)', pout):
119         dualaudio = 1
120     elif re.search(u'\(二\)', 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     try:
146         ts2x264.ts2x264(pin, pout, opt)
147     except:
148         ""
149     status.changeEncoding(-1)
150     #status.setEncoding(status.getEncoding()-1)
151 def tv2avi(pout, chtxt, btime, etime, opt):
152     bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
153     et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
154     dt = et-bt
155     time = dt.seconds-10
156     time = str(time)
157     extt = os.path.splitext(pout)
158     tsout = extt[0]
159     tv2ts.tv2ts(tsout + ".ts", chdb.chtxtsearch(chtxt)['ch'], chdb.chtxtsearch(chtxt)['csch'], time)
160     aviin = tsout + ".ts"
161     dualaudio = 0
162     pentaaudio = 0 
163     if re.search("5", opt):
164         pentaaudio = 1
165     if re.search("d", opt):
166         dualaudio = 1
167     if re.search(u'\[二\]', pout):
168         dualaudio = 1
169     elif re.search(u'(二)', pout):
170         dualaudio = 1
171     elif re.search(u'\(二\)', pout):
172         dualaudio = 1
173     opts = ""
174     if chdb.chtxtsearch(chtxt)['bctype'] == 'cs':
175         opts = opts + "S2"
176         if chtxt == "disch":
177             ""
178         elif chtxt == "hisch":
179             ""
180         else:
181             opts = opts + "a"
182     else:
183         opts = opts + "Ha2"
184     if dualaudio == 1:
185         dualaudiots2avi(aviin, tsout + ".sa.avi")
186         aviin = tsout + ".sa.avi"
187     if pentaaudio == 1:
188         dualaudiots2avi(aviin, tsout + ".sa.avi")
189         aviin = tsout + ".sa.avi"
190     ts2x264.ts2x264(aviin, pout, opts)
191     #os.remove(aviin)
192 def b252avi(pout, chtxt, opt):
193     extt = os.path.splitext(pout)
194     tsout = extt[0]
195     tv2ts.b252ts(tsout + ".ts", chdb.chtxtsearch(chtxt)['ch'], chdb.chtxtsearch(chtxt)['csch'])
196     aviin = tsout + ".ts"
197     dualaudio = 0
198     pentaaudio = 0
199     if re.search("5", opt):
200         pentaaudio = 1
201     if re.search("d", opt):
202         dualaudio = 1
203     if re.search(u'\[二\]', pout):
204         dualaudio = 1
205     elif re.search(u'(二)', pout):
206         dualaudio = 1
207     elif re.search(u'\(二\)', pout):
208         dualaudio = 1
209     if opt == "":
210         opts = ""
211         if chdb.chtxtsearch(chtxt)['bctype'] == 'cs':
212             opts = opts + "S2"
213             if chtxt == "disch":
214                 ""
215             elif chtxt == "hisch":
216                 ""
217             else:
218                 opts = opts + "a"
219         else:
220             opts = opts + "Ha2"
221     else:
222         opts = opt
223     if dualaudio == 1:
224         dualaudiots2avi(aviin, tsout + ".sa.avi")
225         aviin = tsout + ".sa.avi"
226     if pentaaudio == 1:
227         dualaudiots2avi(aviin, tsout + ".sa.avi")
228         aviin = tsout + ".sa.avi"
229     ts2x264.ts2x264(aviin, pout, opts)
230 def dualaudiots2avi(pin, pout):
231     bontsdemux = configreader.getpath('bontsdemux')
232     bonpin = "Z:\\" + pin[1:]
233     outf = os.path.splitext(pin)[0]
234     bonpout = "Z:\\" + outf[1:]
235     xvfb = configreader.getpath('xvfb-run')
236     logpath=pin.replace("ts","log")
237     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay 167 -nd -sound 0 -o \"" + bonpout + "\" -start -quit"
238     #recdblist.printutf8(exe)
239     exe = xvfb + ' -a ' + exe
240     recdblist.printutf8(exe)
241     os.system(exe)
242     ffpin1 = pin.replace("ts", "wav")
243     ffpin2 = pin.replace("ts", "m2v")
244     exe = "ffmpeg -y  -i \'" + ffpin1 + "\' -i \'" + ffpin2 + "\' -r 29.97 -vsync 200 -vcodec copy -acodec libmp3lame -async 200 -ab 128k -f avi  \'" + pout + "\'"
245     recdblist.printutf8(exe)
246     #commands.getoutput(exe)
247     os.system(exe)
248     os.remove(ffpin1)
249     os.remove(ffpin2)
250 def dualaudio2sep(pin, pout, delay):
251     """
252     delay is string
253     """
254     bontsdemux = configreader.getpath('bontsdemux')
255     wine = configreader.getpath('wine')
256     xvfb = configreader.getpath('xvfb-run')
257     bonpin = "Z:\\" + pin[1:]
258     outf = os.path.splitext(pin)[0]
259     bonpout = "Z:\\" + outf[1:]
260     exe = wine + ' ' + bontsdemux + " -i \"" + bonpin + "\" -delay " + delay + " -nd -sound 1 -o \"" + bonpout + "\" -start -quit"
261     #recdblist.printutf8(exe)
262     exe = xvfb + ' -a ' + exe
263     recdblist.printutf8(exe)
264     commands.getoutput(exe.encode('utf-8'))
265     ffpin = pin.replace(".ts", "")
266     ffpin1 = pin.replace("ts", "wav")
267     ffpin2 = pin.replace("ts", "m2v")
268     ffpout1 = ffpin + "1_delay" + delay + ".wav"
269     ffpout2 = ffpin + "2_delay" + delay + ".wav"
270     ffpout3 = ffpin + "_delay" + delay + ".m2v"
271     shutil.move(ffpin1, ffpout1)
272     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay " + delay + " -nd -sound 2 -encode Demux\(wav\) -o \"" + bonpout + "\" -start -quit"
273     exe = xvfb + ' -a ' + exe
274     recdblist.printutf8(exe)
275     commands.getoutput(exe.encode('utf-8'))
276     #os.system(exe)
277     shutil.move(ffpin1, ffpout2)
278     shutil.move(ffpin2, ffpout3)
279     #ffpout21=ffpout1.replace(".wav",".ogg")
280     #ffpout22=ffpout2.replace(".wav",".ogg")
281     #aexe1="ffmpeg -i "+ffpout1+" -vn -acodec vorbis -ar 48000 -ab 128k "+ffpout21
282     #aexe2="ffmpeg -i "+ffpout2+" -vn -acodec vorbis -ar 48000 -ab 128k "+ffpout22
283     aexe = "ffmpeg -i \"" + ffpout3 + "\" -i \"" + ffpout1 + "\" -i \"" + ffpout2 + "\" -r 29.97 -vsync 200 -vcodec copy -acodec copy -f avi " + pout + " -acodec copy -newaudio"
284     recdblist.printutf8(aexe)
285     #os.system(aexe1)
286     #os.system(aexe2)
287     commands.getoutput(aexe.encode('utf-8'))
288     #exe = "ffmpeg -y  -i \'" + ffpin1 + "\' -i \'" + ffpin2 + "\' -r 29.97 -vsync 200 -vcodec copy -acodec libmp3lame -async 200 -ab 128k -f avi  \'" + pout + "\'"
289     #recdblist.printutf8(exe)
290     
291     #os.remove(ffpin1)
292     #os.remove(ffpin2)
293
294 def pentaaudiots2avi(pin, pout):
295     bontsdemux = configreader.getpath('bontsdemux')
296     bonpin = "Z:\\" + pin[1:]
297     xvfb = configreader.getpath('xvfb-run')
298     outf = os.path.splitext(pin)[0]
299     bonpout = "Z:\\" + outf[1:]
300     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay 167 -nd -sound 3 -o \"" + bonpout + "\" -start -quit"
301     #recdblist.printutf8(exe)
302     exe = xvfb + ' -a ' + exe
303     #exe = 'xvfb-run -a ' + exe
304     #exe='export Display=:2&&'+exe
305     recdblist.printutf8(exe)
306     commands.getoutput(exe.encode('utf-8'))
307     #os.system(exe)
308     ffpin1 = pin.replace("ts", "wav")
309     ffpin2 = pin.replace("ts", "m2v")
310     exe = "ffmpeg -y  -i \'" + ffpin1 + "\' -i \'" + ffpin2 + "\' -r 29.97 -vsync 200 -vcodec copy -acodec libmp3lame -async 200 -ab 128k -f avi  \'" + pout + "\'"
311     recdblist.printutf8(exe)
312     commands.getoutput(exe.encode('utf-8'))
313     #os.system(exe)
314     os.remove(ffpin1)
315     os.remove(ffpin2)
316