OSDN Git Service

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