OSDN Git Service

fix many bugs.
[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-5
43     time.sleep(waitt)
44     tnow = datetime.datetime.now()
45     dt = et-tnow
46     rectime = dt.seconds-10
47     rectime = str(rectime)
48     tv2ts.tv2b25ts(tsout + ".ts.b25", chdb.chtxtsearch(chtxt)['ch'], rectime)
49 def b252ts(pout, chtxt, btime, etime, opt):
50     """
51     poutはタイトル(自動的にtitle.b25 title.tsと名前がつきます。)
52     """
53     #status.setB25Decoding(status.getB25Decoding() + 1)
54     status.changeB25Decoding(1)
55     try:
56         tv2ts.b252ts(pout + ".ts", chdb.chtxtsearch(chtxt)['ch'], chdb.chtxtsearch(chtxt)['csch'])
57         tsout = pout
58         aviin = pout + ".ts"
59         dualaudio = 0
60         pentaaudio = 0
61         if re.search("5", opt):
62             pentaaudio = 1
63         if re.search("d", opt):
64             dualaudio = 1
65         if re.search(u"\[二\]", pout):
66             dualaudio = 1
67         elif re.search(u'(二)', pout):
68             dualaudio = 1
69         elif re.search(u'\(二\)', pout):
70             dualaudio = 1
71         if dualaudio == 1:
72             dualaudio2sep(aviin,"167")
73         if pentaaudio == 1:
74             pentaaudiots2avi(aviin, tsout + ".sa.avi")
75     except Exception, inst:
76         print "Error happened in b252ts in tv2avi"
77         print type(inst)
78         print inst
79     status.changeB25Decoding(-1)
80 def ts2avi(pin, pout, opt):
81     status.changeEncoding(1)
82     try:
83         ts2x264.ts2x264(pin, pout, opt)
84     except Exception, inst:
85         print "error occures in tv2avi.py ts2avi"
86         print type(inst)
87         print inst
88     status.changeEncoding(-1)
89 def dualaudiots2avi(pin, pout):
90     bontsdemux = configreader.getpath('bontsdemux')
91     bonpin = "Z:\\" + pin[1:]
92     outf = os.path.splitext(pin)[0]
93     bonpout = "Z:\\" + outf[1:]
94     xvfb = configreader.getpath('xvfb-run')
95     logpath=pin.replace("ts","log")
96     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay 167 -nd -sound 0 -o \"" + bonpout + "\" -start -quit"
97     #recdblist.printutf8(exe)
98     exe = xvfb + ' -a ' + exe
99     recdblist.printutf8(exe)
100     os.system(exe.encode('utf-8'))
101     ffpin1 = pin.replace("ts", "wav")
102     ffpin2 = pin.replace("ts", "m2v")
103     exe = "ffmpeg -y  -i \'" + ffpin1 + "\' -i \'" + ffpin2 + "\' -r 29.97 -vsync 200 -vcodec copy -acodec libmp3lame -async 200 -ab 128k -f avi  \'" + pout + "\'"
104     recdblist.printutf8(exe)
105     #commands.getoutput(exe)
106     os.system(exe.encode('utf-8'))
107     os.remove(ffpin1)
108     os.remove(ffpin2)
109 def dualaudio2sep(pin, delay):
110     """
111     delay is string
112     """
113     bontsdemux = configreader.getpath('bontsdemux')
114     wine = configreader.getpath('wine')
115     xvfb = configreader.getpath('xvfb-run')
116     bonpin = "Z:\\" + pin[1:]
117     outf = os.path.splitext(pin)[0]
118     bonpout = "Z:\\" + outf[1:]
119     exe = wine + ' ' + bontsdemux + " -i \"" + bonpin + "\" -delay " + delay + " -nd -sound 1 -o \"" + bonpout + "\" -start -quit"
120     exe = xvfb + ' -a ' + exe
121     recdblist.printutf8(exe)
122     txt=commands.getoutput(exe.encode('utf-8'))
123     try:
124         recdblist.addlog(pin, txt, "dualaudio-sep1 BontsDemux")
125     except:
126         ""
127     ffpin = pin.replace(".ts", "")
128     ffpin1 = pin.replace("ts", "wav")
129     ffpin2 = pin.replace("ts", "m2v")
130     ffpout1 = ffpin + "_1.wav"
131     ffpout2 = ffpin + "_2.wav"
132     ffpout3 = ffpin + ".m2v"
133     shutil.move(ffpin1, ffpout1)
134     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay " + delay + " -nd -sound 2 -encode Demux\(wav\) -o \"" + bonpout + "\" -start -quit"
135     exe = xvfb + ' -a ' + exe
136     recdblist.printutf8(exe)
137     txt=commands.getoutput(exe.encode('utf-8'))
138     try:
139         recdblist.addlog(pin, txt, "dualaudio-sep1 BontsDemux")
140     except:
141         ""
142     #os.system(exe)
143     shutil.move(ffpin1, ffpout2)
144     shutil.move(ffpin2, ffpout3)
145     ffpout21=ffpout1.replace(".wav",".mp3")
146     ffpout22=ffpout2.replace(".wav",".mp3")
147     useNero=0
148     try:
149         if configreader.getpath("useNeroAAC")=="1" and os.path.exists(configreader.getpath("NeroAAC")):
150             useNero=1
151     except:
152         useNero=0
153     if useNero==1:
154         ffpout21=ffpout1.replace(".wav",".aac")
155         ffpout22=ffpout2.replace(".wav",".aac")
156         tv2audio.wav2aac_nero(ffpout1, ffpout21)
157         tv2audio.wav2aac_nero(ffpout2, ffpout22)
158     else:
159         tv2audio.wav2mp3_lame(ffpout1, ffpout21)
160         tv2audio.wav2mp3_lame(ffpout2, ffpout22)
161     time.sleep(3)
162     os.remove(ffpout1)
163     os.remove(ffpout2)
164
165 def pentaaudiots2avi(pin, pout):
166     bontsdemux = configreader.getpath('bontsdemux')
167     bonpin = "Z:\\" + pin[1:]
168     xvfb = configreader.getpath('xvfb-run')
169     outf = os.path.splitext(pin)[0]
170     bonpout = "Z:\\" + outf[1:]
171     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay 167 -nd -sound 3 -o \"" + bonpout + "\" -start -quit"
172     exe = xvfb + ' -a ' + exe
173     recdblist.printutf8(exe)
174     txt=u"Cmd : "+exe+"\n"
175     txt=txt+commands.getoutput(exe.encode('utf-8'))
176     recdblist.addlog(pin, txt, "5.1ch audio Demux log")
177     ffpin1 = pin.replace("ts", "wav")
178     ffpin2 = pin.replace("ts", "m2v")
179
180
181     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 + "\""
182     recdblist.printutf8(exe)
183     txt=u"Cmd : "+exe+"\n"
184     txtt=commands.getoutput(exe.encode('utf-8'))
185     try:
186         txt=txt+txtt
187     except:
188         txt=txt+"skip logging (not UTF-8)"
189     recdblist.addlog(pin, txt, "5.1ch audio Mux log")
190     if os.path.exists(pout):
191         if os.path.getsize(pout)>100*1000:
192             os.remove(ffpin1)
193             os.remove(ffpin2)
194