OSDN Git Service

170bf72e4868838a0e20a8c3692f179e17c8baeb
[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     commands.getoutput(exe.encode('utf-8'))
123     ffpin = pin.replace(".ts", "")
124     ffpin1 = pin.replace("ts", "wav")
125     ffpin2 = pin.replace("ts", "m2v")
126     ffpout1 = ffpin + "_1.wav"
127     ffpout2 = ffpin + "_2.wav"
128     ffpout3 = ffpin + ".m2v"
129     shutil.move(ffpin1, ffpout1)
130     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay " + delay + " -nd -sound 2 -encode Demux\(wav\) -o \"" + bonpout + "\" -start -quit"
131     exe = xvfb + ' -a ' + exe
132     recdblist.printutf8(exe)
133     commands.getoutput(exe.encode('utf-8'))
134     #os.system(exe)
135     shutil.move(ffpin1, ffpout2)
136     shutil.move(ffpin2, ffpout3)
137     ffpout21=ffpout1.replace(".wav",".mp3")
138     ffpout22=ffpout2.replace(".wav",".mp3")
139     useNero=0
140     try:
141         if configreader.getpath("useNeroAAC")=="1" and os.path.exists(configreader.getpath("NeroAAC")):
142             useNero=1
143     except:
144         useNero=0
145     if useNero==1:
146         ffpout21=ffpout1.replace(".wav",".aac")
147         ffpout22=ffpout2.replace(".wav",".aac")
148         tv2audio.wav2aac_nero(ffpout1, ffpout21)
149         tv2audio.wav2aac_nero(ffpout2, ffpout22)
150     else:
151         tv2audio.wav2mp3_lame(ffpout1, ffpout21)
152         tv2audio.wav2mp3_lame(ffpout2, ffpout22)
153     time.sleep(3)
154     os.remove(ffpout1)
155     os.remove(ffpout2)
156
157 def pentaaudiots2avi(pin, pout):
158     bontsdemux = configreader.getpath('bontsdemux')
159     bonpin = "Z:\\" + pin[1:]
160     xvfb = configreader.getpath('xvfb-run')
161     outf = os.path.splitext(pin)[0]
162     bonpout = "Z:\\" + outf[1:]
163     exe = 'wine ' + bontsdemux + " -i \"" + bonpin + "\" -delay 167 -nd -sound 3 -o \"" + bonpout + "\" -start -quit"
164     exe = xvfb + ' -a ' + exe
165     recdblist.printutf8(exe)
166     commands.getoutput(exe.encode('utf-8'))
167     ffpin1 = pin.replace("ts", "wav")
168     ffpin2 = pin.replace("ts", "m2v")
169     exe = "ffmpeg -y  -i \'" + ffpin1 + "\' -i \'" + ffpin2 + "\' -r 29.97 -vsync 200 -vcodec copy -acodec libmp3lame -async 200 -ab 128k -f avi  \'" + pout + "\'"
170     recdblist.printutf8(exe)
171     commands.getoutput(exe.encode('utf-8'))
172     os.remove(ffpin1)
173     os.remove(ffpin2)
174