OSDN Git Service

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