OSDN Git Service

change caption2ass option.
[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     tpavi=os.path.join(dir,title+".avi")
19     tpmkv=os.path.join(dir, title+".mkv")
20     tv2avi.ts2avi(pin, tpavi, opt)
21     time.sleep(10)
22     avi2mkv(tpavi,tpmkv,opt)
23 def avi2mkv(pin,pout,opt):
24     dir=os.path.split(pout)[0]
25     title=os.path.split(pout)[1]
26     title=os.path.splitext(title)[0]
27     isdualaac=0
28     ispentaaudio=0
29     if re.search("d",opt):
30         isdualaac=1
31     elif re.search("5",opt):
32         ispentaaudio=1
33     exe = configreader.getpath("mkvmerge")
34     txt=""
35     os.environ['LANG']="ja_JP.UTF-8"
36     if isdualaac==1:
37         wineexe=configreader.getpath("wine")
38         pincap=pin.replace(".avi",".srt")
39         cap2ass=configreader.getpath("Caption2Ass")
40         pints=pin.replace(".avi",".ts")
41         paac1=pin.replace(".avi","_1.aac")
42         paac2=pin.replace(".avi","_2.aac")
43         recdblist.printutf8(paac1)
44         if not os.path.exists(paac1):
45             paac1=pin.replace(".avi","_1.mp3")
46         if not os.path.exists(paac2):
47             paac2=pin.replace(".avi","_2.mp3")
48         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
49         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"
50         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"
51         if os.path.isfile(cap2ass):
52             txt=txt+execcomd(e0)
53         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
54             txt=txt+execcomd(e1s)
55         else:
56             txt=txt+execcomd(e1)
57     elif ispentaaudio==1:
58         wineexe=configreader.getpath("wine")
59         pincap=pin.replace(".avi",".srt")
60         cap2ass=configreader.getpath("Caption2Ass")
61         pints=pin.replace(".avi",".ts")
62         paac1=pin.replace(".avi","_1.aac")
63         paac2=pin.replace(".avi","_2.aac")
64         recdblist.printutf8(paac1)
65         if not os.path.exists(paac1):
66             paac1=pin.replace(".avi","_1.mp3")
67         if not os.path.exists(paac2):
68             paac2=pin.replace(".avi","_2.mp3")
69         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
70         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"
71         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"
72         if os.path.isfile(cap2ass):
73             txt=txt+execcomd(e0)
74         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
75             txt=txt+execcomd(e1s)
76         else:
77             txt=txt+execcomd(e1)
78     else:
79         wineexe=configreader.getpath("wine")
80         pincap=pin.replace(".avi",".srt")
81         cap2ass=configreader.getpath("Caption2Ass")
82         pints=pin.replace(".avi",".ts")
83         pinaac=pin.replace(".avi",".aac")
84         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
85         e1="ffmpeg -i \""+pints+"\" -vn -f aac -acodec copy \""+pinaac+"\""
86         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"
87         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"
88         if os.path.isfile(cap2ass):
89             txt=txt+execcomd(e0)
90         txt=txt+execcomd(e1)
91         if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
92             txt=txt+execcomd(e2s)
93         else:
94             txt=txt+execcomd(e2)
95     recdblist.addlog(pout, txt, "mkvmerge-log")
96     logo=pout.replace(".mkv",".log")
97     if status.getSettings_auto_del_tmp()==1:
98         if os.path.exists(pout):
99             if os.path.getsize(pout)>100*1000*1000:
100                 deltitle(dir,title)
101 def execcomd(cmd):
102     txt=""
103     cmdt=u"nice -n 17 "+cmd
104     try:
105         txt=u"Cmd : "+cmd+"\n"
106         txt2=commands.getoutput(cmdt.encode('utf-8'))
107         txt=txt+unicode(txt2,"utf-8")+"\n"
108     except:
109         ""
110     return txt
111 def deltitle(path,title):
112     dp=[]
113     dp.append(os.path.join(path,title+".avi"))
114     dp.append(os.path.join(path,title+".120.avi"))
115     dp.append(os.path.join(path,title+".noodml.avi"))
116     dp.append(os.path.join(path,title+".aac"))
117     dp.append(os.path.join(path,title+".m2v"))
118     dp.append(os.path.join(path,title+"_1.aac"))
119     dp.append(os.path.join(path,title+"_2.aac"))
120     dp.append(os.path.join(path,title+"_1.mp3"))
121     dp.append(os.path.join(path,title+"_2.mp3"))
122     dp.append(os.path.join(path,title+".srt"))
123     if os.path.exists(os.path.join(path,title+".ts"))and os.path.exists(os.path.join(path,title+".ts.b25")):
124         if os.path.getsize(os.path.join(path,title+".ts"))*10>os.path.getsize(os.path.join(path,title+".ts.b25")):
125             if os.path.getsize(os.path.join(path,title+".ts"))>1000*1000*100:
126                 dp.append(os.path.join(path,title+".ts.b25"))
127     if os.path.exists(os.path.join(path,title+".mkv")):
128         if os.path.getsize(os.path.join(path,title+".mkv"))>1000*1000*10:
129             for ip in dp:
130                 try:
131                     os.remove(ip)
132                 except:
133                     ""
134