OSDN Git Service

a73313f746fe53054e27f81709cf6c6492096dc2
[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     #recdblist.printutf8(avilist)
25     for avif in avilist:
26         if avif.rfind("sa.avi")==-1:
27             ##b25f is title.ts.b25  avi is title.avi
28             dir = os.path.split(avif)[0]
29             title = os.path.split(avif)[1]
30             title = title.replace(".avi", "")
31             title = title.replace(".mkv", "")
32             avipath = os.path.join(dir, title + ".avi")
33             mkvpath = os.path.join(dir, title + ".mkv")
34             mp4path = os.path.join(dir, title + ".mp4")
35             if not os.path.exists(mp4path):
36                 if os.path.exists(avipath):
37                     avidtime = int(time.time()-os.path.getmtime(avipath))
38                     if avidtime>300:
39                         if os.path.exists(mkvpath):
40                             mkvdtime = int(time.time()-os.path.getmtime(mkvpath))
41                             if mkvdtime>300:
42                                 recdblist.printutf8(mkvpath+":"+mp4path)
43                                 tv2mp4.mkv2mp4(mkvpath,mp4path)
44                             else:
45                                 recdblist.printutf8(avipath+":"+mp4path)
46                                 tv2mp4.avi2mp4(avipath,mp4path)
47                         else:
48                             recdblist.printutf8(avipath+":"+mp4path)
49                             tv2mp4.avi2mp4(avipath,mp4path)
50                 else:
51                     if not os.path.exists(avipath):
52                         if os.path.exists(mkvpath):
53                             mkvdtime = int(time.time()-os.path.getmtime(mkvpath))
54                             if mkvdtime>300:
55                                 recdblist.printutf8(mkvpath+":"+mp4path)
56                                 tv2mp4.mkv2mp4(mkvpath,mp4path)
57 def auto_check(path):
58     avilist = glob.glob(path + "/*.avi")
59     mkvlist = glob.glob(path+"/*.mkv")
60     mp4list = glob.glob(path+"/*.mp4")
61     tslist = glob.glob(path+"/*.ts")
62     b25list = glob.glob(path+"/*.ts.b25")
63     filelist=avilist+mkvlist+tslist+b25list+mp4list
64     add=[]
65     tbtime=datetime.datetime.now()+datetime.timedelta(seconds=60)
66     tbtime2=datetime.datetime.now()+datetime.timedelta(seconds=1200)
67     for fn in filelist:
68         if fn.rfind("sa.avi")==-1 and fn.rfind(".120.avi")==-1 and os.path.getsize(fn)>1*1000*1000:
69             dir = os.path.split(fn)[0]
70             file = os.path.split(fn)[1]
71             title = os.path.splitext(file)[0]
72             title = unicode(title,'utf-8')
73             ext = os.path.splitext(file)[1]
74             if ext == ".b25":
75                 title=title.replace(".ts","")
76             dbs=rec10d.rec10db.select_by_name_time_timeline(title,tbtime,tbtime2)
77             if len(dbs)==0:
78                 proc=check_process(dir, title)
79                 chtxtt=rec10d.rec10db.select_chtxt_by_title_timeline_log(title)
80                 nchtxt=""
81                 if chtxtt!=None:
82                     nchtxt=chtxtt
83                 if proc=="b25":
84                     add.append([recdblist.REC_AUTO_SUGGEST_DECODE,title,nchtxt])
85                 elif proc=="ts":
86                     add.append([recdblist.REC_AUTO_SUGGEST_ENCODE,title,nchtxt])
87                 elif proc =="264":
88                     add.append([recdblist.REC_AUTO_SUGGEST_AVI2FP,title,nchtxt])
89                 #elif proc =="mp4":
90                 #    add.append([recdblist.REC_AUTO_SUGGEST_AP2FP,title,nchtxt])
91     #print add
92     if len(add)>0:
93         rec10d.rec10db.new_auto_proc()
94         for a in add:
95             rec10d.rec10db.add_auto_proc(a[0],a[1],a[2])
96         time.sleep(1)
97 """
98     処理がどの段階まで言ったのかを調査し返す。
99     return
100     recording
101     b25
102     b25decoding
103     tssplitting
104     ts
105     encoding
106     avi
107     mp4making
108     mp4
109 """
110 def check_process(path,title):
111     path1 = os.path.join(path,title+".mkv")
112     if os.path.exists(path1):
113         if int(time.time()-os.path.getmtime(path1))>300:
114             return "mkv"
115         else:
116             return "mkvmaking"
117     elif os.path.exists(os.path.join(path,title+".mp4")):
118         if int(time.time()-os.path.getmtime(os.path.join(path,title+".mp4")))>300:
119             return "mp4"
120         else:
121             return "mp4making"
122     elif os.path.exists(os.path.join(path,title+".264")):
123         if int(time.time()-os.path.getmtime(os.path.join(path,title+".264")))>300:
124             return "264"
125         else:
126             return "encoding"
127     elif os.path.exists(os.path.join(path,title+".ts.log")):
128         if int(time.time()-os.path.getmtime(os.path.join(path,title+".ts")))<300:
129             return "encoding"
130         else:
131             return "ts"
132     elif os.path.exists(os.path.join(path,title+".ts")):
133         if int(time.time()-os.path.getmtime(os.path.join(path,title+".ts")))>300:
134             return "ts"
135         else:
136             return "tssplitting"
137     elif os.path.exists(os.path.join(path,title+".sa.avi")):
138         if int(time.time()-os.path.getmtime(os.path.join(path,title+".sa.avi")))>300:
139             return "ts"
140         else:
141             return "tssplitting"
142     elif os.path.exists(os.path.join(path,title+".ts.b25")):
143         if int(time.time()-os.path.getmtime(os.path.join(path,title+".ts.b25")))>300:
144             return "b25"
145         else:
146             return "recording"
147 def update_all_timeline_epg():
148     now=datetime.datetime.now()
149     et=now+datetime.timedelta(days=7)
150     update_timeline_epg(now.strftime("%Y-%m-%d %H:%M:%S"), et.strftime("%Y-%m-%d %H:%M:%S"))
151 def update_timeline_epg(btime,etime):
152     update_timeline_epg_schedule(btime,etime)
153     update_timeline_dup(btime,etime)
154 def update_timeline_epg_schedule(btime,etime):
155     dbl=rec10d.rec10db.select_bytime_all_timeline(btime, etime)
156     #print dbl
157     for db in dbl:
158         ret=[]
159         #[chtxtt, title, btime, etime,exp,longexp,category]
160         if db['type']==recdblist.REC_KEYWORD or db['type']==recdblist.REC_KEYWORD_EVERY_SOME_DAYS:
161             ret=epgdb.searchtime2(db['title'], db['btime'], db['deltatime'], db['chtxt'])
162             if len(ret)>4 and len(ret[2])>18:
163                 rec10d.rec10db.update_epg_timeline(db['type'], db['chtxt'], db['title'], db['btime'], ret[2],ret[3],ret[1])
164                 if not (db['btime'] == ret[2] and  db['etime']==ret[3]):
165                     rec10d.rec10db.update_status_change_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "1")
166                 else:
167                     rec10d.rec10db.update_status_change_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "0")
168             else:
169                 rec10d.rec10db.update_status_change_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "1")
170         elif db['type']==recdblist.REC_RESERVE or db['type']==recdblist.REC_FINAL_RESERVE :
171             ret=epgdb.searchtime2(db['title'], db['btime'],"5", db['chtxt'])
172             if len(ret)>4 and len(ret[2])>18:
173                 rec10d.rec10db.update_epg_timeline(db['type'], db['chtxt'], db['title'], db['btime'], ret[2],ret[3],ret[1])
174                 if not (db['btime'] == ret[2] and  db['etime']==ret[3]):
175                     rec10d.rec10db.update_status_change_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "1")
176                 else:
177                     rec10d.rec10db.update_status_change_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "0")
178             else:
179                 rec10d.rec10db.update_status_change_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "1")
180 def update_timeline_dup(epgbtime,epgetime):
181     dbl=rec10d.rec10db.select_byepgtime_all_timeline(epgbtime, epgetime)
182     for db in dbl:
183         if db['type']==recdblist.REC_KEYWORD or db['type']==recdblist.REC_KEYWORD_EVERY_SOME_DAYS:
184             dbn=epgdb.count_epgschedule(db['epgbtime'], db['epgetime'])
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")