OSDN Git Service

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