OSDN Git Service

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