OSDN Git Service

simplify codes.
[rec10/rec10-git.git] / rec10 / trunk / src / tv2mkv.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
5 import status
6 import os
7 import commands
8 import tv2avi
9 import os.path
10 import time
11 import re
12 import recdblist
13 import configreader
14 def ts2mkv(pin, pout, opt):
15     dir=os.path.split(pout)[0]
16     title=os.path.split(pout)[1]
17     title=os.path.splitext(title)[0]
18     ##use avi
19     """tpavi=os.path.join(dir,title+".avi")
20     tpmkv=os.path.join(dir, title+".mkv")
21     tv2avi.ts2avi(pin, tpavi, opt)
22     time.sleep(10)
23     avi2mkv(tpavi,tpmkv,opt)"""
24     tpraw=os.path.join(dir, title+".264")
25     tpmkv=os.path.join(dir, title+".mkv")
26     tv2avi.ts2raw(pin, tpraw, opt)
27     time.sleep(10)
28     raw2mkv(tpraw, tpmkv, opt)
29 def raw2mkv(pin,pout,opt):#x264
30     dir=os.path.split(pout)[0]
31     title=os.path.split(pout)[1]
32     title=os.path.splitext(title)[0]
33     duration="--default-duration 0:30000/1001fps "
34     ffmpeg=configreader.getpath("ffmpeg")
35     isdualaac=0
36     ispentaaudio=0
37     if re.search("a",opt):
38         duration="--default-duration 0:24000/1001fps "
39     elif re.search("v",opt):
40         duration="--default-duration 0:24000/1001fps "
41     if re.search("d",opt):
42         isdualaac=1
43         duration="--default-duration 0:30000/1001fps "##ffmpegが24fpsに対応していないための措置
44     elif re.search("5",opt):
45         ispentaaudio=1
46         duration="--default-duration 0:30000/1001fps "##ffmpegが24fpsに対応していないための措置
47     exe = configreader.getpath("mkvmerge")
48     txt=""
49     os.environ['LANG']="ja_JP.UTF-8"
50     if isdualaac==1:
51         wineexe=configreader.getpath("wine")
52         pincap=pin.replace(".264",".srt")
53         cap2ass=configreader.getpath("Caption2Ass")
54         pints=pin.replace(".264",".ts")
55         paac1=pin.replace(".264","_1.aac")
56         paac2=pin.replace(".264","_2.aac")
57         recdblist.printutf8(paac1)
58         if not os.path.exists(paac1):
59             paac1=pin.replace(".264","_1.mp3")
60         if not os.path.exists(paac2):
61             paac2=pin.replace(".264","_2.mp3")
62         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
63         e1=exe +" -o \""+pout+u"\" --forced-track 0:no "+duration+"-d 0 -A -S \""+pin+"\" --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --track-order 0:0,1:1,2:1"
64         e1s=exe +" -o \""+pout+u"\" --forced-track 0:no "+duration+"-d 0 -A -S \""+pin+"\" --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --forced-track 0:no -s 0 -D -A \""+pincap+"\" --track-order 0:0,1:1,2:1,3:0"
65         if os.path.isfile(cap2ass):
66             txt=txt+execcomd(e0)
67         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
68             txt=txt+u"\n Cmd:"+e1s+u"\n"+execcomd(e1s)
69         else:
70             txt=txt+u"\n Cmd:"+e1+u"\n"+execcomd(e1)
71     elif ispentaaudio==1:
72         wineexe=configreader.getpath("wine")
73         pincap=pin.replace(".264",".srt")
74         cap2ass=configreader.getpath("Caption2Ass")
75         pints=pin.replace(".264",".ts")
76         paac1=pin.replace(".264","_1.aac")
77         paac2=pin.replace(".264","_2.aac")
78         recdblist.printutf8(paac1)
79         if not os.path.exists(paac1):
80             paac1=pin.replace(".264","_1.mp3")
81         if not os.path.exists(paac2):
82             paac2=pin.replace(".264","_2.mp3")
83         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
84         e1=exe +" -o \""+pout+u"\" --forced-track 0:no "+duration+"-d 0 -A -S \""+pin+"\" --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --track-order 0:0,1:1,2:1"
85         e1s=exe +" -o \""+pout+u"\" --forced-track 0:no "+duration+"-d 0 -A -S \""+pin+"\" --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --forced-track 0:no -s 0 -D -A \""+pincap+"\" --track-order 0:0,1:1,2:1,3:0"
86         if os.path.isfile(cap2ass):
87             txt=txt+execcomd(e0)
88         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
89             txt=txt+u"\n Cmd:"+e1s+u"\n"+execcomd(e1s)
90         else:
91             txt=txt+u"\n Cmd:"+e1+u"\n"+execcomd(e1)
92     else:
93         wineexe=configreader.getpath("wine")
94         pincap=pin.replace(".264",".srt")
95         cap2ass=configreader.getpath("Caption2Ass")
96         pints=pin.replace(".264",".ts")
97         pinaac=pin.replace(".264",".aac")
98         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
99         e1=ffmpeg+" -i \""+pints+"\" -vn -f aac -acodec copy \""+pinaac+"\""
100         e2=exe +" -o \""+pout+u"\" --forced-track 0:no "+duration+"-d 0 -A -S \""+pin+"\" --forced-track 0:no -a 0 -D -S \""+pinaac+"\" --track-order 0:0,1:0"
101         e2s=exe +" -o \""+pout+u"\" --forced-track 0:no "+duration+"-d 0 -A -S \""+pin+"\" --forced-track 0:no -a 0 -D -S \""+pinaac+"\" --forced-track 0:no -s 0 -D -A \""+pincap+"\" --track-order 0:0,1:0,2:0"
102         if os.path.isfile(cap2ass):
103             txt=txt+execcomd(e0)
104         txt=txt+execcomd(e1)
105         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
106             txt=txt+u"\n Cmd:"+e2s+"\n"+execcomd(e2s)
107             recdblist.printutf8(e2s)
108         else:
109             txt=txt+u"\n Cmd:"+e2+"\n"+execcomd(e2)
110             recdblist.printutf8(e2)
111     recdblist.addlog(pout, txt, "mkvmerge-log")
112     logo=pout.replace(".mkv",".log")
113     if status.getSettings_auto_del_tmp()==1:
114         if os.path.exists(pout):
115             if os.path.getsize(pout)>100*1000*1000:
116                 deltitle(dir,title)
117 def avi2mkv(pin,pout,opt):
118     dir=os.path.split(pout)[0]
119     title=os.path.split(pout)[1]
120     title=os.path.splitext(title)[0]
121     isdualaac=0
122     ispentaaudio=0
123     if re.search("d",opt):
124         isdualaac=1
125     elif re.search("5",opt):
126         ispentaaudio=1
127     exe = configreader.getpath("mkvmerge")
128     txt=""
129     os.environ['LANG']="ja_JP.UTF-8"
130     if isdualaac==1:
131         wineexe=configreader.getpath("wine")
132         pincap=pin.replace(".avi",".srt")
133         cap2ass=configreader.getpath("Caption2Ass")
134         pints=pin.replace(".avi",".ts")
135         paac1=pin.replace(".avi","_1.aac")
136         paac2=pin.replace(".avi","_2.aac")
137         recdblist.printutf8(paac1)
138         if not os.path.exists(paac1):
139             paac1=pin.replace(".avi","_1.mp3")
140         if not os.path.exists(paac2):
141             paac2=pin.replace(".avi","_2.mp3")
142         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
143         e1=exe +" -o \""+pout+u"\"  --forced-track 0:no -d 0 -A -S \""+pin+"\" --sync 0:0 --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --track-order 0:0,1:1,2:1"
144         e1s=exe +" -o \""+pout+u"\"  --forced-track 0:no -d 0 -A -S \""+pin+"\" --sync 0:0 --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --forced-track 0:no -s 0 -D -A \""+pincap+"\" --track-order 0:0,1:1,2:1,3:0"
145         if os.path.isfile(cap2ass):
146             txt=txt+execcomd(e0)
147         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
148             txt=txt+execcomd(e1s)
149         else:
150             txt=txt+execcomd(e1)
151     elif ispentaaudio==1:
152         wineexe=configreader.getpath("wine")
153         pincap=pin.replace(".avi",".srt")
154         cap2ass=configreader.getpath("Caption2Ass")
155         pints=pin.replace(".avi",".ts")
156         paac1=pin.replace(".avi","_1.aac")
157         paac2=pin.replace(".avi","_2.aac")
158         recdblist.printutf8(paac1)
159         if not os.path.exists(paac1):
160             paac1=pin.replace(".avi","_1.mp3")
161         if not os.path.exists(paac2):
162             paac2=pin.replace(".avi","_2.mp3")
163         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
164         e1=exe +" -o \""+pout+u"\"  --forced-track 0:no -d 0 -A -S \""+pin+"\" --sync 0:0 --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --track-order 0:0,1:1,2:1"
165         e1s=exe +" -o \""+pout+u"\"  --forced-track 0:no -d 0 -A -S \""+pin+"\" --sync 0:0 --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --forced-track 0:no -s 0 -D -A \""+pincap+"\" --track-order 0:0,1:1,2:1,3:0"
166         if os.path.isfile(cap2ass):
167             txt=txt+execcomd(e0)
168         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
169             txt=txt+execcomd(e1s)
170         else:
171             txt=txt+execcomd(e1)
172     else:
173         wineexe=configreader.getpath("wine")
174         pincap=pin.replace(".avi",".srt")
175         cap2ass=configreader.getpath("Caption2Ass")
176         pints=pin.replace(".avi",".ts")
177         pinaac=pin.replace(".avi",".aac")
178         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
179         e1=ffmpeg+u" -i \""+pints+u"\" -vn -f aac -acodec copy \""+pinaac+"\""
180         e2=exe +" -o \""+pout+u"\"  --forced-track 0:no -d 0 -A -S \""+pin+"\" --sync 0:0 --forced-track 0:no -a 0 -D -S \""+pinaac+"\" --track-order 0:0,1:0"
181         e2s=exe +" -o \""+pout+u"\"  --forced-track 0:no -d 0 -A -S \""+pin+"\" --sync 0:0 --forced-track 0:no -a 0 -D -S \""+pinaac+"\" --forced-track 0:no -s 0 -D -A \""+pincap+"\" --track-order 0:0,1:0,2:0"
182         if os.path.isfile(cap2ass):
183             txt=txt+execcomd(e0)
184         txt=txt+execcomd(e1)
185         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
186             txt=txt+execcomd(e2s)
187         else:
188             txt=txt+execcomd(e2)
189     recdblist.addlog(pout, txt, "mkvmerge-log")
190     logo=pout.replace(".mkv",".log")
191     if status.getSettings_auto_del_tmp()==1:
192         if os.path.exists(pout):
193             if os.path.getsize(pout)>100*1000*1000:
194                 deltitle(dir,title)
195 def execcomd(cmd):
196     txt=""
197     cmdt=u"nice -n 17 "+cmd
198     try:
199         txt=u"Cmd : "+cmd+"\n"
200         txt2=commands.getoutput(cmdt.encode('utf-8'))
201         txt=txt+unicode(txt2,"utf-8")+"\n"
202     except:
203         ""
204     return txt
205 def deltitle(path,title):
206     dp=[]
207     dp.append(os.path.join(path,title+".avi"))
208     dp.append(os.path.join(path,title+".264"))
209     dp.append(os.path.join(path,title+".120.avi"))
210     dp.append(os.path.join(path,title+".noodml.avi"))
211     dp.append(os.path.join(path,title+".aac"))
212     dp.append(os.path.join(path,title+".m2v"))
213     dp.append(os.path.join(path,title+"_1.aac"))
214     dp.append(os.path.join(path,title+"_2.aac"))
215     dp.append(os.path.join(path,title+"_1.mp3"))
216     dp.append(os.path.join(path,title+"_2.mp3"))
217     dp.append(os.path.join(path,title+".srt"))
218     if os.path.exists(os.path.join(path,title+".ts"))and os.path.exists(os.path.join(path,title+".ts.b25")):
219         if os.path.getsize(os.path.join(path,title+".ts"))*10>os.path.getsize(os.path.join(path,title+".ts.b25")):
220             if os.path.getsize(os.path.join(path,title+".ts"))>1000*1000*100:
221                 dp.append(os.path.join(path,title+".ts.b25"))
222     if os.path.exists(os.path.join(path,title+".mkv")):
223         if os.path.getsize(os.path.join(path,title+".mkv"))>1000*1000*10:
224             for ip in dp:
225                 try:
226                     os.remove(ip)
227                 except:
228                     ""
229