OSDN Git Service

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