OSDN Git Service

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