OSDN Git Service

add Que system.
[rec10/rec10-git.git] / rec10 / trunk / src / auto_process.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
5 import os
6 import glob
7 import time
8 import datetime
9 import commands
10 import re
11 import os.path
12
13 import chdb
14 import status
15 import configreader
16 import recdblist
17 import rec10d
18 import tv2mp4
19 import epgdb
20 def Update_to_MP4(path):
21     """
22     !現在未使用!
23     録画一時フォルダ内mp4ファイルを検索
24     """
25     avilist = glob.glob(path + "/*.avi")
26     mkvlist = glob.glob(path+"/*.mkv")
27     avilist=avilist+mkvlist
28     for avif in avilist:
29         if avif.rfind("sa.avi")==-1:
30             dir = os.path.split(avif)[0]
31             title = os.path.split(avif)[1]
32             title = title.replace(".avi", "")
33             title = title.replace(".mkv", "")
34             avipath = os.path.join(dir, title + ".avi")
35             mkvpath = os.path.join(dir, title + ".mkv")
36             mp4path = os.path.join(dir, title + ".mp4")
37             if not os.path.exists(mp4path):
38                 if os.path.exists(avipath):
39                     avidtime = int(time.time()-os.path.getmtime(avipath))
40                     if avidtime>300:
41                         if os.path.exists(mkvpath):
42                             mkvdtime = int(time.time()-os.path.getmtime(mkvpath))
43                             if mkvdtime>300:
44                                 recdblist.printutf8(mkvpath+":"+mp4path)
45                                 tv2mp4.mkv2mp4(mkvpath,mp4path)
46                             else:
47                                 recdblist.printutf8(avipath+":"+mp4path)
48                                 tv2mp4.avi2mp4(avipath,mp4path)
49                         else:
50                             recdblist.printutf8(avipath+":"+mp4path)
51                             tv2mp4.avi2mp4(avipath,mp4path)
52                 else:
53                     if not os.path.exists(avipath):
54                         if os.path.exists(mkvpath):
55                             mkvdtime = int(time.time()-os.path.getmtime(mkvpath))
56                             if mkvdtime>300:
57                                 recdblist.printutf8(mkvpath+":"+mp4path)
58                                 tv2mp4.mkv2mp4(mkvpath,mp4path)
59 def auto_check(path):
60     avilist = glob.glob(path + "/*.avi")
61     mkvlist = glob.glob(path+"/*.mkv")
62     mp4list = glob.glob(path+"/*.mp4")
63     tslist = glob.glob(path+"/*.ts")
64     b25list = glob.glob(path+"/*.ts.b25")
65     filelist=avilist+mkvlist+tslist+b25list+mp4list
66     add=[]
67     tbtime=datetime.datetime.now()+datetime.timedelta(seconds=60)
68     tbtime2=datetime.datetime.now()+datetime.timedelta(seconds=1200)
69     for fn in filelist:
70         if os.path.exists(fn):
71             if fn.rfind("sa.avi")==-1 and fn.rfind(".120.avi")==-1 and os.path.getsize(fn)>1*1000*1000:
72                 dir = os.path.split(fn)[0]
73                 file = os.path.split(fn)[1]
74                 title = os.path.splitext(file)[0]
75                 title = unicode(title,'utf-8')
76                 ext = os.path.splitext(file)[1]
77                 if ext == ".b25":
78                     title=title.replace(".ts","")
79                 dbs=rec10d.rec10db.select_by_name_time_timeline(title,tbtime,tbtime2)
80                 if len(dbs)==0:
81                     proc=check_process(dir, title)
82                     chtxtt=rec10d.rec10db.select_chtxt_by_title_timeline_log(title)
83                     nchtxt=""
84                     if chtxtt!=None:
85                         nchtxt=chtxtt
86                     if proc=="b25":
87                         add.append([recdblist.REC_AUTO_SUGGEST_DECODE,title,nchtxt])
88                     elif proc=="ts":
89                         add.append([recdblist.REC_AUTO_SUGGEST_ENCODE,title,nchtxt])
90                     elif proc =="264":
91                         add.append([recdblist.REC_AUTO_SUGGEST_AVI2FP,title,nchtxt])
92                     #elif proc =="mp4":
93                     #    add.append([recdblist.REC_AUTO_SUGGEST_AP2FP,title,nchtxt])
94         #print add
95     if len(add)>0:
96         rec10d.rec10db.new_auto_proc()
97         for a in add:
98             rec10d.rec10db.add_auto_proc(a[0],a[1],a[2])
99         time.sleep(1)
100 """
101     処理がどの段階まで言ったのかを調査し返す。
102     return
103     recording
104     b25
105     b25decoding
106     tssplitting
107     ts
108     encoding
109     avi
110     mp4making
111     mp4
112 """
113 def check_process(path,title):
114     path1 = os.path.join(path,title+".mkv")
115     if os.path.exists(path1):
116         if int(time.time()-os.path.getmtime(path1))>300:
117             return "mkv"
118         else:
119             return "mkvmaking"
120     elif os.path.exists(os.path.join(path,title+".mp4")):
121         if int(time.time()-os.path.getmtime(os.path.join(path,title+".mp4")))>300:
122             return "mp4"
123         else:
124             return "mp4making"
125     elif os.path.exists(os.path.join(path,title+".264")):
126         if int(time.time()-os.path.getmtime(os.path.join(path,title+".264")))>300:
127             return "264"
128         else:
129             return "encoding"
130     elif os.path.exists(os.path.join(path,title+".ts.log")):
131         if int(time.time()-os.path.getmtime(os.path.join(path,title+".ts")))<300:
132             return "encoding"
133         else:
134             return "ts"
135     elif os.path.exists(os.path.join(path,title+".ts")):
136         if int(time.time()-os.path.getmtime(os.path.join(path,title+".ts")))>300:
137             return "ts"
138         else:
139             return "tssplitting"
140     elif os.path.exists(os.path.join(path,title+".sa.avi")):
141         if int(time.time()-os.path.getmtime(os.path.join(path,title+".sa.avi")))>300:
142             return "ts"
143         else:
144             return "tssplitting"
145     elif os.path.exists(os.path.join(path,title+".ts.b25")):
146         if int(time.time()-os.path.getmtime(os.path.join(path,title+".ts.b25")))>300:
147             return "b25"
148         else:
149             return "recording"
150 def update_all_timeline_epg():
151     now=datetime.datetime.now()
152     et=now+datetime.timedelta(days=7)
153     update_timeline_epg(now.strftime("%Y-%m-%d %H:%M:%S"), et.strftime("%Y-%m-%d %H:%M:%S"))
154 def update_timeline_epg(btime,etime):
155     update_timeline_epg_schedule(btime,etime)
156     update_timeline_dup(btime,etime)
157 def update_timeline_epg_schedule(btime,etime):
158     dbl=rec10d.rec10db.select_bytime_all_timeline(btime, etime)
159     #print dbl
160     for db in dbl:
161         ret=[]
162         #[chtxtt, title, btime, etime,exp,longexp,category]
163         if db['type']==recdblist.REC_KEYWORD or db['type']==recdblist.REC_KEYWORD_EVERY_SOME_DAYS:
164             ret=epgdb.searchtime2(db['title'], db['btime'], db['deltatime'], db['chtxt'])
165             if len(ret)>4 and len(ret[2])>18:
166                 rec10d.rec10db.update_epg_timeline(db['type'], db['chtxt'], db['title'], db['btime'], ret[2],ret[3],ret[1],ret[4])
167                 if not (db['btime'] == ret[2] and  db['etime']==ret[3]):
168                     rec10d.rec10db.update_status_change_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "1")
169                 else:
170                     rec10d.rec10db.update_status_change_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "0")
171             else:
172                 rec10d.rec10db.update_status_change_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "1")
173         elif db['type']==recdblist.REC_RESERVE or db['type']==recdblist.REC_FINAL_RESERVE :
174             ret=epgdb.searchtime2(db['title'], db['btime'],"5", db['chtxt'])
175             if len(ret)>4 and len(ret[2])>18:
176                 rec10d.rec10db.update_epg_timeline(db['type'], db['chtxt'], db['title'], db['btime'], ret[2],ret[3],ret[1],ret[4])
177                 if not (db['btime'] == ret[2] and  db['etime']==ret[3]):
178                     rec10d.rec10db.update_status_change_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "1")
179                 else:
180                     rec10d.rec10db.update_status_change_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "0")
181             else:
182                 rec10d.rec10db.update_status_change_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "1")
183 def update_timeline_dup(epgbtime,epgetime):
184     dbl=rec10d.rec10db.select_byepgtime_all_timeline(epgbtime, epgetime)
185     for db in dbl:
186         if db['type']==recdblist.REC_KEYWORD or db['type']==recdblist.REC_KEYWORD_EVERY_SOME_DAYS:
187             dbn=epgdb.count_epgschedule(db['epgbtime'], db['epgetime'])
188             try:
189                 bctypet=chdb.chtxtsearch(db['chtxt'])['bctype']
190                 if bctypet.find("cs") > -1 or bctypet.find("bs") > -1 :
191                     if dbn[1]>status.getRecordingMax()[1]:
192                         rec10d.rec10db.update_status_dup_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "1")
193                     else:
194                         rec10d.rec10db.update_status_dup_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "0")
195                 else:
196                     if dbn[0]>status.getRecordingMax()[0]:
197                         rec10d.rec10db.update_status_dup_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "1")
198                     else:
199                         rec10d.rec10db.update_status_dup_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "0")
200             except:
201                 ""
202 def kill_dead_encode(path):
203     rawlist = glob.glob(path + "/*.264")
204     for fn in rawlist:
205         if int(time.time()-os.path.getmtime(fn))>3000:
206             #print fn
207
208             tspath=unicode(fn.replace(".264",".ts"),'utf-8')
209             m2vpath=unicode(fn.replace(".264",".m2v"),'utf-8')
210             x264path=unicode(fn.replace(".264",".264"),'utf-8')
211             os.environ['LANG']="ja_JP.UTF-8"
212
213             #ktmp=u"ps auxw | grep \""+tspath+u"\" | egrep -v grep | egrep -v \"sh -c\" | grep mencoder | awk '{print $2}'"
214             #kmmp=u"ps auxw | grep \""+m2vpath+u"\" | egrep -v grep | egrep -v \"sh -c\" | grep mencoder | awk '{print $2}'"
215             #ktmt=commands.getoutput(ktmp.encode('utf-8'))
216             #kmmt=commands.getoutput(kmmp.encode('utf-8'))
217
218             ktmp=u"ps auxw | grep \""+x264path+u"\" | egrep -v grep | egrep -v \"sh -c\" | grep x264 | awk '{print $2}'"
219             #recdblist.printutf8(ktmp)
220             ktmt=commands.getoutput(ktmp.encode('utf-8'))
221             #print ktmt
222             if len(ktmt)>0:
223                 ktmt=ktmt[0]
224             try:
225                 if int(ktmt)>0:
226                     ktmp=u"kill -9 `"+ktmp+u"`"
227                     #print ktmp
228                     recdblist.printutf8(ktmp)
229                     os.system(ktmp.encode('utf-8'))
230             except:
231                 ""
232             """if len(kmmt)>0:
233                 kmmt=kmmt[0]
234             try:
235                 if int(kmmt)>0:
236                     kmmp=u"kill -9 '"+kmmp+u"'"
237                     os.system(kmmp.encode('utf-8'))
238             except:
239                 """""
240 def deltmpfile(path,title,ext):
241     level= 0##0:b25 1:ts(del tsmix and ts.b25) 5:x264(del 2 and so on) 10:mp4/mkv
242     smsize= 0
243     if re.search(".ts",ext):
244         level= 1
245         smsize = 100*1000*1000
246     elif re.search(".264",ext):
247         level= 5
248         smsize = 10*1000*1000
249     elif re.search(".mp4",ext):
250         level = 10
251         smsize = 10*1000*1000
252     elif re.search(".mkv",ext):
253         level = 15
254         smsize = 10*1000*1000
255     dp=[]
256     if level > 0 :
257         if os.path.exists(os.path.join(path,title+".ts")) and os.path.exists(os.path.join(path,title+".ts.b25")):
258             if os.path.getsize(os.path.join(path,title+".ts"))*10>os.path.getsize(os.path.join(path,title+".ts.b25")):
259                 dp.append(os.path.join(path,title+".ts.b25"))
260     if level > 4 :
261         if configreader.getenv("remove_ts")=="1":
262             dp.append(os.path.join(path,title+".ts"))
263     if level > 9 :
264         dp.append(os.path.join(path,title+".avi"))
265         dp.append(os.path.join(path,title+".264"))
266         dp.append(os.path.join(path,title+".120.avi"))
267         dp.append(os.path.join(path,title+".noodml.avi"))
268         dp.append(os.path.join(path,title+".aac"))
269         dp.append(os.path.join(path,title+".m2v"))
270         dp.append(os.path.join(path,title+"_1.aac"))
271         dp.append(os.path.join(path,title+"_2.aac"))
272         dp.append(os.path.join(path,title+"_1.mp3"))
273         dp.append(os.path.join(path,title+"_2.mp3"))
274         dp.append(os.path.join(path,title+".srt"))
275     if level > 14 :
276         dp.append(os.path.join(path,title+".mp4"))
277     if os.path.exists(os.path.join(path,title+ext)):
278         if os.path.getsize(os.path.join(path,title+ext))>smsize:
279             for ip in dp:
280                 try:
281                     os.remove(ip)
282                 except:
283                     ""