OSDN Git Service

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