OSDN Git Service

536ed851216dd686a53753c0e7514c69af87d305
[rec10/rec10-git.git] / rec10 / trunk / src / timerec.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
5 import datetime
6 import os
7 import os.path
8 import re
9 import sys
10 import time
11 import shutil
12
13 import auto_rec
14 import auto_process
15 import chdb
16 import checker
17 import configreader
18 import epgdb
19 import rec10d
20 import recdb
21 import status
22 import tv2avi
23 import tv2mkv
24 import tv2mp4
25 import install
26 import recdblist
27 recpath = configreader.getpath('recpath')
28 movepath = configreader.getpath('move_destpath')
29 path = str(os.path.dirname(os.path.abspath(__file__))) + "/"
30 def task():
31     """
32     数分毎に実行されるタスク処理
33     予定によって子プロセスを生成し処理する。
34     """
35     try:
36         if rec10d.rec10db.select_installed_in_status()==0 or rec10d.rec10db.select_version_in_status()<recdblist.version:
37             install.install()
38     except:
39         install.install()
40     recdb.delete_old("24")
41     recdb.delete_old_auto_bayes("1")
42     recdb.delete_old_auto_keyword("1")
43     tasks = recdb.getnow("3")
44     inum = recdb.countRecNow_minutes("10")
45     recdblist.printutf8(u"rec10処理開始"+ datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
46     recdblist.printutf8(u"直近録画予約件数:" + str(inum) + u"BS/CS録画中件数:" + str(status.getBSCSRecording()) + u"TE録画中件数:" + str(status.getTERecording()) )
47     encodenum=0
48     b25num=0
49     dnow=datetime.datetime.now()
50     if dnow.minute%10<5:
51         pid = os.fork()
52         if pid != 0:
53             ""
54         else:
55             for k in recdb.get_key():
56                 search_keyword(k)
57             sys.exit()
58     else:
59         pid = os.fork()
60         if pid!=0:
61             ""
62         else:
63             time.sleep(10)
64             auto_process.auto_check(recpath)
65             time.sleep(10)
66             auto_process.kill_dead_encode(recpath)
67             sys.exit()
68     update = chdb.update()
69     if len(update) > 0:
70         pid = os.fork()
71         if pid != 0:
72             ""
73         else:
74             i = 0
75             for bctype in update:
76                 recnum = 0
77                 if bctype.find('te') > -1:
78                     recnum = status.getTERecording() + recdb.countRecNow_minutes_TE("10")
79                     recdblist.printutf8(u"放送種別:"+bctype + u"||該当チューナー実行中件数:" + str(status.getTERecording()) + u":直近予約件数:" + str(recdb.countRecNow_minutes_TE("10")))
80                     recnum = int(configreader.getenv("te_max")) -recnum
81                 else:
82                     recnum = status.getBSCSRecording() + recdb.countRecNow_minutes_BSCS("10")
83                     recdblist.printutf8(u"放送種別:"+bctype + u"||該当チューナー実行中件数:" + str(status.getBSCSRecording()) + u":直近予約件数:" + str(recdb.countRecNow_minutes_BSCS("10")))
84                     recnum = int(configreader.getenv("bscs_max")) -recnum
85                 if recnum >0 :
86                     recdblist.printutf8(str(update))
87                     rec10d.rec10db.update_status_by_bctype_epg_ch(bctype, "0")
88                     epgdb.updatebc(bctype)
89                     update = chdb.update()
90                     i = i + 1
91                 if i > 0:
92                     break
93             time.sleep(5)
94             auto_process.update_all_timeline_epg()
95             sys.exit()
96     else:
97         pid = os.fork()
98         if pid != 0:
99             ""
100         else:
101             auto_process.update_all_timeline_epg()
102             sys.exit()
103     for task in tasks:
104         typetxt = task["type"]
105         try:
106             chtxt = task['chtxt']
107         except:
108             chtxt = ""
109         try:
110             title = task['title']
111         except:
112             title = ""
113         try:
114             btime = task['btime']
115             bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
116         except:
117             btime = ""
118             bt = datetime.datetime.strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
119         try:
120             etime = task['etime']
121             et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
122         except:
123             etime = ""
124             et = datetime.datetime.strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
125         try:
126             opt = task['opt']
127         except:
128             opt = ""
129         tnow = datetime.datetime.now()
130         dtt = bt-tnow
131         dt = dtt.days * 24 * 60 * 60 + dtt.seconds
132         if task["type"] == recdblist.REC_RESERVE:#"res,"+chtxt+","+title+","+btime+","+etime+","+opt
133             pid = os.fork()
134             if pid != 0:#親プロセスの場合
135                 ""
136             else:#子プロセスの場合 アップデートを行って終了
137                 type_reserve(task["type"],chtxt,title,bt,et,opt)
138                 sys.exit()
139         elif task["type"] == recdblist.REC_KEYWORD:#"key,"+chtxt+","+keyword+","+btime+","+deltatime+","+opt
140             deltatime = task['deltatime']
141             pid = os.fork()
142             if pid != 0:#親プロセスの場合
143                 ""
144             else:#子プロセスの場合 アップデートを行って終了
145                 type_keyword(task["type"],chtxt,title, bt, et, opt, deltatime)
146                 sys.exit()
147         elif task["type"] == recdblist.REC_KEYWORD_EVERY_SOME_DAYS:#"keyevery,"+chtxt+","+keyword+","+btime+","+deltatime+","+opt+","+deltaday
148             deltatime = task['deltatime']
149             deltaday = task['deltaday']
150             try:
151                 keyeverycounter=task['counter']
152             except:
153                 keyeverycounter=-1
154             pid = os.fork()
155             if pid != 0:#親プロセスの場合
156                 ""
157             else:#子プロセスの場合 アップデートを行って終了
158                 type_keyword_every_day(task["type"],chtxt, title, bt, et, opt, deltatime, deltaday,keyeverycounter)
159                 sys.exit()
160         elif task["type"] == recdblist.REC_FINAL_RESERVE:#"rec,"+chtxt+","+title+","+btime+","+etime+","+opt
161             pid = os.fork()
162             if pid != 0:#親プロセスの場合
163                 ""
164             else:#子プロセスの場合 アップデートを行って終了
165                 type_final(task["type"],chtxt, title, bt, et, opt)
166                 sys.exit()
167         elif task["type"] == recdblist.REC_TS_DECODE_QUE:
168             b25num=b25num+1
169             pid = os.fork()
170             if pid != 0:#親プロセスの場合
171                 ""
172             else:#子プロセスの場合 アップデートを行って終了
173                 time.sleep(5*b25num)
174                 type_decode_que(task["type"],chtxt, title, bt, et, opt)
175                 sys.exit()
176         elif task["type"] == recdblist.REC_ENCODE_QUE:
177             encodenum=encodenum+1
178             pid = os.fork()
179             if pid != 0:#親プロセスの場合
180                 ""
181             else:#子プロセスの場合 アップデートを行って終了
182                 time.sleep(5*encodenum)
183                 type_encode_que(task["type"],chtxt, title, bt, et, opt)
184                 sys.exit()
185         elif task["type"] == recdblist.REC_AVI_TO_MKV:
186             if dt < 10 * 60:
187                 pid = os.fork()
188                 if pid > 0:#親プロセスの場合
189                     ""
190                 else:
191                     makeMP4=0
192                     try:
193                         if configreader.getenv("make_mp4")==1:
194                             makeMP4=1
195                     except:
196                         ""
197                     if re.search("m", opt):
198                         makeMP4=0
199                     if re.search("4", opt):
200                         makeMP4=1
201                     recdb.del_reckey(recdblist.REC_AVI_TO_MKV, title, chtxt, btime)
202                     recdb.rec_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime, etime, opt)
203                     if makeMP4==1:
204                         tv2mp4.raw2mp4(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mp4"),opt)
205                     else:
206                         tv2mkv.raw2mkv(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mkv"),opt)
207                     recdb.del_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime)
208                     sys.exit()
209         elif task["type"] == recdblist.REC_AVI_TO_MP4:
210             if dt < 10 * 60:
211                 pid = os.fork()
212                 if pid > 0:#親プロセスの場合
213                     ""
214                 else:
215                     makeMP4=0
216                     try:
217                         if configreader.getenv("make_mp4")==1:
218                             makeMP4=1
219                     except:
220                         ""
221                     if re.search("m", opt):
222                         makeMP4=0
223                     if re.search("4", opt):
224                         makeMP4=1
225                     recdb.del_reckey(recdblist.REC_AVI_TO_MP4, title, chtxt, btime)
226                     recdb.rec_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime, etime, opt)
227                     if makeMP4==1:
228                         tv2mp4.ts2mp4(pin, pout, opt)
229                         tv2mp4.raw2mp4(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mp4"),opt)
230                     else:
231                         tv2mkv.raw2mkv(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mkv"),opt)
232                     recdb.del_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime)
233                     sys.exit()
234         elif task["type"] == recdblist.REC_MKV_TO_MP4:
235             if dt < 10 * 60:
236                 pid = os.fork()
237                 if pid > 0:#親プロセスの場合
238                     ""
239                 else:
240                     recdb.del_reckey(recdblist.REC_MKV_TO_MP4, title, chtxt, btime)
241                     recdb.rec_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime, etime, opt)
242                     tv2mp4.mkv2mp4(os.path.join(recpath,title+".mkv"),os.path.join(recpath,title+".mp4"))
243                     recdb.del_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime)
244                     sys.exit()
245     sys.exit()
246 def search_keyword(key):
247     tnow = datetime.datetime.now()
248     nows =tnow.strftime("%Y-%m-%d %H:%M:%S")
249     for k in key:
250         recdatum = epgdb.searchtime_auto(k,nows, "144")
251         for recdata in recdatum:
252             if recdata[1] != "":
253                 chtxtt = recdata[0]
254                 titlet = recdata[1]
255                 btimet = recdata[2]
256                 etimet = recdata[3]
257                 btt = datetime.datetime.strptime(btimet, "%Y-%m-%d %H:%M:%S")
258                 ett = datetime.datetime.strptime(etimet, "%Y-%m-%d %H:%M:%S")
259                 btimet = btt.strftime("%Y-%m-%d %H:%M:%S")
260                 etimet = ett.strftime("%Y-%m-%d %H:%M:%S")
261                 if status.getSettings_auto_jbk()==1:
262                     topt=status.getSettings_auto_opt()
263                     try:
264                         maxnum=0
265                         if len(ch['ch'])>2:
266                             maxnum=epgdb.count_schedule_timeline(btimet, etimet)[1]
267                             maxnum=int(configreader.getenv("bscs_max"))-maxnum
268                         else:
269                             maxnum=epgdb.count_schedule_timeline(btimet, etimet)[0]
270                             maxnum=int(configreader.getenv("te_max"))-maxnum
271                         if maxnum>0:
272                             topt=status.getSettings_auto_opt()
273                             recdb.recreserv(titlet,chtxtt, btimet, etimet,topt)
274                     except Exception, inst:
275                         recdblist.printutf8("error occures in search_keyword_auto_jbk")
276                         recdblist.printutf8(str(type(inst)))
277                         recdblist.printutf8(str(inst))
278                 else:
279                     try:
280                         recdb.add_auto_keyword(chtxtt, titlet, btimet, etimet)
281                     except Exception, inst:
282                         recdblist.printutf8("error occures in search_keyword")
283                         recdblist.printutf8(str(type(inst)))
284                         recdblist.printutf8(str(inst))
285 def type_reserve(typetxt,chtxt,title,bt,et,opt):
286     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
287     etime = et.strftime("%Y-%m-%d %H:%M:%S")
288     typetxtnow=typetxt
289     typetxtfinal=""
290     if typetxt==recdblist.REC_RESERVE:
291         typetxtfinal=recdblist.REC_FINAL_RESERVE
292     tnow = datetime.datetime.now()
293     dtt = bt-tnow
294     dt = dtt.days * 24 * 60 * 60 + dtt.seconds
295     if (dt < 58 * 60 and dt > 30 * 60):
296         bctypet = chdb.chtxtsearch(chtxt)['bctype']
297         chdatat = rec10d.rec10db.select_by_bctype_epg_ch(bctypet)
298         dt1 = datetime.datetime.strptime(chdatat[0][5], "%Y-%m-%d %H:%M:%S")-datetime.datetime.now()
299         dt1 = dt1.days * 24 * 60 * 60 + dt1.seconds
300         if dt1 < 60 * 60:
301             recdata = epgdb.searchtime2(title, btime, "5", chtxt)
302             chtxtn = recdata[0]
303             titlen = recdata[1]
304             btimen = recdata[2]
305             etimen = recdata[3]
306             exp = recdata[4]
307             longexp = recdata[5]
308             category=recdata[6]
309             bt = datetime.datetime.strptime(btimen, "%Y-%m-%d %H:%M:%S")
310             et = datetime.datetime.strptime(etimen, "%Y-%m-%d %H:%M:%S")
311             btimen = bt.strftime("%Y-%m-%d %H:%M:%S")
312             etimen = et.strftime("%Y-%m-%d %H:%M:%S")
313             if chtxt != "":
314                 try:
315                     recdb.del_reckey(typetxtnow, title, chtxt, btime)
316                     recdb.rec_reckey(typetxtfinal, titlen, chtxtn, btimen, etimen, opt)
317                     recdb.rec_reclog(titlen, chtxtn, btimen, etimen, opt, exp, longexp, category)
318                     auto_rec.add_key(chtxt, titlen,exp+" "+longexp)
319                     auto_rec.add_key("ALL", titlen,exp+" "+longexp)
320                     recdblist.printutf8(u"追いかけ機能実行中: "+title+" : "+titlen+" "+btimen+" "+etimen)
321                 except Exception, inst:
322                     recdblist.printutf8("Error happened in Oikake DB")
323                     recdblist.printutf8(str(type(inst)))
324                     recdblist.printutf8(str(inst))
325             else:
326                 recdblist.printutf8(u"追いかけ機能エラー:番組データが見付かりません。")
327         else:
328             if rec10d.rec10db.select_by_bctype_epg_ch(bctypet)[0][6] != "0":
329                 rec10d.rec10db.update_status_by_bctype_epg_ch(bctypet, "3")
330         sys.exit()
331     elif (dt <= 30 * 60 and dt > 20 * 60):
332         recdata = epgdb.searchtime2(title, btime, "5", chtxt)
333         chtxtn = recdata[0]
334         titlen = recdata[1]
335         btimen = recdata[2]
336         etimen = recdata[3]
337         exp = recdata[4]
338         longexp = recdata[5]
339         category=recdata[6]
340         bt = datetime.datetime.strptime(btimen, "%Y-%m-%d %H:%M:%S")
341         et = datetime.datetime.strptime(etimen, "%Y-%m-%d %H:%M:%S")
342         btimen = bt.strftime("%Y-%m-%d %H:%M:%S")
343         etimen = et.strftime("%Y-%m-%d %H:%M:%S")
344         if chtxt != "":
345             try:
346                 recdb.del_reckey(typetxtnow, title, chtxt, btime)
347                 recdb.rec_reckey(typetxtfinal, titlen, chtxtn, btimen, etimen, opt)
348                 recdb.rec_reclog(titlen, chtxtn, btimen, etimen, opt, exp, longexp, category)
349                 auto_rec.add_key(chtxt, titlen,exp+" "+longexp)
350                 auto_rec.add_key("ALL", titlen,exp+" "+longexp)
351                 recdblist.printutf8(u"追いかけ機能実行中: "+title+" : "+titlen+" "+btimen+" "+etimen)
352             except Exception, inst:
353                 recdblist.printutf8("Error happened in Oikake DB")
354                 recdblist.printutf8(str(type(inst)))
355                 recdblist.printutf8(str(inst))
356     elif dt <= 20 * 60:
357         try:
358             recdb.del_reckey(typetxtnow, title, chtxt, btime)
359             recdb.rec_reckey(typetxtfinal, title, chtxt, btime, etime, opt)
360         except Exception, inst:
361             recdblist.printutf8(str(type(inst)))
362             recdblist.printutf8(str(inst))
363 def type_final(typetxt,chtxt,title,bt,et,opt):
364     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
365     etime = et.strftime("%Y-%m-%d %H:%M:%S")
366     typetxtnow=typetxt
367     typetxting=""
368     typetxtdecque=""
369     if typetxt==recdblist.REC_FINAL_RESERVE:
370         typetxting=recdblist.REC_TS_RECORDING
371         typetxtdecque=recdblist.REC_TS_DECODE_QUE
372     tnow = datetime.datetime.now()
373     dtt = bt-tnow
374     dt = dtt.days * 24 * 60 * 60 + dtt.seconds
375     if dt < 6 * 60 and dt > 0:
376         newtitle=title
377         recdb.del_reckey(typetxtnow, title, chtxt, btime)
378         testpath=[os.path.join(recpath,title+".ts.b25")]
379         testpath.append(os.path.join(recpath,title+".ts"))
380         testpath.append(os.path.join(recpath,title+".avi"))
381         testpath.append(os.path.join(recpath,title+".mp4"))
382         testpath.append(os.path.join(recpath,title+".log"))
383         tcheck=0
384         for ti in testpath:
385             if os.path.exists(ti):
386                 tcheck=tcheck+1
387         if re.search("N", opt) or tcheck>0:
388             iff=""
389             try:
390                 iff=u"("+configreader.getenv("iff")+u")_"
391             except:
392                 iff=""
393             newtime=bt
394             newtitle=newtitle+u"_"+iff+newtime.strftime("%Y-%m-%dT%H-%M-%S")
395         recdb.rec_reckey(typetxting, newtitle, chtxt, btime, etime, opt)
396         recdblist.printutf8(u"録画開始 "+newtitle+" "+btime+" "+etime)
397         tv2avi.timetv2b25(recpath + "/" + newtitle + ".avi", chtxt, btime, etime, opt)
398         recdb.del_reckey(typetxting, newtitle, chtxt, btime)
399         if not re.search("R", opt):
400             tnow = datetime.datetime.now()
401             bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
402             et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
403             dt = tnow-bt
404             bt = tnow + datetime.timedelta(seconds=600)
405             et = et + dt + datetime.timedelta(seconds=600)
406             btime = bt.strftime("%Y-%m-%d %H:%M:%S")
407             etime = et.strftime("%Y-%m-%d %H:%M:%S")
408             recdb.rec_reckey(typetxtdecque, newtitle, chtxt, btime, etime, opt)
409         else:
410             try:
411                 shutil.copy(os.path.join(recpath,title+".ts.b25"), os.path.join(movepath,title+".ts.b25"))
412             except:
413                 ""
414             recdb.rec_reckey(recdblist.REC_MOVE_END, newtitle, chtxt, btime, etime, opt)
415         sys.exit()
416 def type_keyword(typetxt,chtxt,title,bt,et,opt,deltatime):
417     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
418     etime = et.strftime("%Y-%m-%d %H:%M:%S")
419     typetxtnow=typetxt
420     typetxtres=""
421     if typetxt==recdblist.REC_KEYWORD:
422         typetxtres=recdblist.REC_RESERVE
423     tnow = datetime.datetime.now()
424     dtt = bt-tnow
425     dt = dtt.days * 24 * 60 * 60 + dtt.seconds
426     print dt
427     if dt < 90 * 60:
428         recdata = epgdb.searchtime2(title, btime, deltatime, chtxt)
429         if recdata[1] != "":
430             chtxtt = recdata[0]
431             titlet = recdata[1]
432             btimet = recdata[2]
433             etimet = recdata[3]
434             exp = recdata[4]
435             longexp = recdata[5]
436             category=recdata[6]
437             bt = datetime.datetime.strptime(btimet, "%Y-%m-%d %H:%M:%S")
438             et = datetime.datetime.strptime(etimet, "%Y-%m-%d %H:%M:%S")
439             btimet = bt.strftime("%Y-%m-%d %H:%M:%S")
440             etimet = et.strftime("%Y-%m-%d %H:%M:%S")
441             #try:
442             recdb.del_reckey(typetxtnow, title, chtxt, btime)
443             recdb.rec_reckey(typetxtres, titlet, chtxtt, btimet, etimet, opt)
444             recdb.rec_reclog(titlet, chtxtt, btimet, etimet, opt, exp,longexp,category)
445             auto_rec.add_key(chtxt, titlet,exp+" "+longexp)
446             auto_rec.add_key("ALL", titlet,exp+" "+longexp)
447             recdblist.printutf8(u"key "+title+u" : "+titlet+u" "+btimet+u" "+etimet)
448             #except Exception, inst:
449             #    recdblist.printutf8("Error happened in REC_KEYWORD DB")
450             #    recdblist.printutf8(type(inst))
451             #    recdblist.printutf8(inst)
452         else:
453             recdblist.printutf8("nothing match")
454 def type_keyword_every_day(type,chtxt,title,bt,et,opt,deltatime,deltaday,counter):
455     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
456     etime = et.strftime("%Y-%m-%d %H:%M:%S")
457     tnow = datetime.datetime.now()
458     dtt = bt-tnow
459     dt = dtt.days * 24 * 60 * 60 + dtt.seconds
460     dd = datetime.timedelta(days=int(deltaday))
461     if dtt.days < 0:
462         recdb.del_reckey(recdblist.REC_KEYWORD_EVERY_SOME_DAYS, title, chtxt, btime)
463         bt = bt + dd
464         et = et + dd
465         btxt = bt.strftime("%Y-%m-%d %H:%M:%S")
466         etxt = et.strftime("%Y-%m-%d %H:%M:%S")
467         recdb.everyreserv(title, chtxt, btxt, etxt, deltatime, opt, deltaday)
468     elif dt < 120 * 60:
469         recdb.del_reckey(recdblist.REC_KEYWORD_EVERY_SOME_DAYS, title, chtxt, btime)
470         bt = bt + dd
471         et = et + dd
472         btxt = bt.strftime("%Y-%m-%d %H:%M:%S")
473         etxt = et.strftime("%Y-%m-%d %H:%M:%S")
474         if counter>0:
475             recdb.everyreserv(title, chtxt, btxt, etxt, deltatime, opt, deltaday,counter-1)
476         elif counter==-1:
477             recdb.everyreserv(title, chtxt, btxt, etxt, deltatime, opt, deltaday,-1)
478         recdata = epgdb.searchtime2(title, btime, deltatime, chtxt)
479         if recdata[1] != "":
480             chtxtt = recdata[0]
481             titlet = recdata[1]
482             btimet = recdata[2]
483             etimet = recdata[3]
484             exp = recdata[4]
485             longexp = recdata[5]
486             category=recdata[6]
487             bt = datetime.datetime.strptime(btimet, "%Y-%m-%d %H:%M:%S")
488             et = datetime.datetime.strptime(etimet, "%Y-%m-%d %H:%M:%S")
489             btimet = bt.strftime("%Y-%m-%d %H:%M:%S")
490             etimet = et.strftime("%Y-%m-%d %H:%M:%S")
491             #try:
492             recdb.keyreserv(titlet, chtxtt, btimet, etimet, deltatime, opt)
493         else:
494             recdb.keyreserv(title, chtxt, btime, etime, deltatime, opt)
495 def type_decode_que(typetxt,chtxt,title,bt,et,opt):
496     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
497     etime = et.strftime("%Y-%m-%d %H:%M:%S")
498     tnow = datetime.datetime.now()
499     dtt = bt-tnow
500     dt = dtt.days * 24 * 60 * 60 + dtt.seconds
501     typetxtnow=typetxt
502     typetxting=""
503     typetxtmiss=""
504     typetxtencque=""
505     if typetxt== recdblist.REC_TS_DECODE_QUE:
506         typetxting=recdblist.REC_TS_DECODING
507         typetxtmiss=recdblist.REC_MISS_DECODE
508         typetxtencque=recdblist.REC_ENCODE_QUE
509     tnow = datetime.datetime.now()
510     dtt = bt-tnow
511     dt = dtt.days * 24 * 60 * 60 + dtt.seconds
512     if dt < 10 * 60:
513         if status.getB25Decoding() < 2:
514             pin = recpath + "/" + title
515             recdb.del_reckey(typetxtnow, title, chtxt, btime)
516             recdb.rec_reckey(typetxting, title, chtxt, btime, etime, opt)
517             tv2avi.b252ts(pin, chtxt, btime, etime, opt)
518             recdb.del_reckey(typetxting, title, chtxt, btime)
519             if not os.access(recpath + "/" + title + ".ts", os.F_OK):
520                 recdb.del_reckey(typetxting, title, chtxt, btime)
521                 recdb.rec_reckey(typetxtmiss, title, chtxt, btime, etime, opt)
522             else:
523                 ch=checker.checkB25Decode(recpath + "/" + title + ".ts.b25", recpath + "/" + title + ".ts")
524                 #print ch
525                 if ch == 1:
526                     os.remove(recpath + "/" + title + ".ts.b25")
527             bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
528             et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
529             dt = et-bt
530             if not re.search("D", opt):
531                 tnow = datetime.datetime.now()
532                 bt = tnow + datetime.timedelta(seconds=600)
533                 et = bt + dt
534                 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
535                 etime = et.strftime("%Y-%m-%d %H:%M:%S")
536                 recdb.rec_reckey(typetxtencque, title, chtxt, btime, etime, opt)
537             else:
538                 try:
539                     shutil.copy(os.path.join(recpath,title+".ts"), os.path.join(movepath,title+".ts"))
540                 except Exception, inst:
541                     errtxt="Error happened in type_decode_que in timerec.\n"
542                     errtxt=errtxt+"move ts error.\n"
543                     errtxt=errtxt+str(type(inst))+"\n"
544                     errtxt=errtxt+str(inst)
545                     recdblist.addlog(os.path.join(recpath,title+".ts"), errtxt, "timerec-move(Decode) error")
546                 recdb.rec_reckey(recdblist.REC_MOVE_END, title, chtxt, btime, etime, opt)
547         else:
548             recdb.del_reckey(typetxtnow, title, chtxt, btime)
549             bt = bt + datetime.timedelta(seconds=600)
550             et = et + datetime.timedelta(seconds=600)
551             btime = bt.strftime("%Y-%m-%d %H:%M:%S")
552             etime = et.strftime("%Y-%m-%d %H:%M:%S")
553             recdb.rec_reckey(typetxtnow, title, chtxt, btime, etime, opt)
554     sys.exit()
555
556 def type_encode_que(typetxt,chtxt,title,bt,et,opt):
557     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
558     etime = et.strftime("%Y-%m-%d %H:%M:%S")
559     tnow = datetime.datetime.now()
560     dtt = bt-tnow
561     dt = dtt.days * 24 * 60 * 60 + dtt.seconds
562     typetxtnow=typetxt
563     typetxting=""
564     typetxtfin=""
565     if typetxt==recdblist.REC_ENCODE_QUE:
566         typetxting=recdblist.REC_ENCODE_LOCAL
567         typetxtfin=recdblist.REC_FIN_LOCAL
568     if dt < 10 * 60:
569         if status.getEncoding() < int(configreader.getenv("enc_max")):
570             recdb.del_reckey(typetxtnow, title, chtxt, btime)
571             recdb.rec_reckey(typetxting, title, chtxt, btime, etime, opt)
572             recdblist.printutf8(opt)
573             pin = recpath + "/" + title + ".ts"
574             if re.search("d", opt):
575                 pin = recpath + "/" + title + ".m2v"
576                 if not os.path.exists(pin) or os.path.getsize(pin)<100*1000:
577                     paac2 = recpath + "/" + title + "_2.aac"
578                     pmp32 = recpath + "/" + title + "_2.mp3"
579                     if not os.path.exists(paac2) and not os.path.exists(pmp32):
580                         tv2avi.dualaudio2sep(os.path.join(recpath, title+".ts"),"167")
581                 time.sleep(3)
582             elif re.search("5", opt):
583                 pin = recpath + "/" + title + ".m2v"
584                 if not os.path.exists(pin) or os.path.getsize(pin)<100*1000:
585                     paac2 = recpath + "/" + title + "_2.aac"
586                     pmp32 = recpath + "/" + title + "_2.mp3"
587                     if not os.path.exists(paac2) and not os.path.exists(pmp32):
588                         tv2avi.pentaaudio2sep(os.path.join(recpath, title+".ts"))
589                 time.sleep(3)
590             makeMP4=0
591             try:
592                 if configreader.getenv("make_mp4")=="1":
593                     makeMP4=1
594             except:
595                 ""
596             if re.search("m", opt):
597                 makeMP4=0
598             if re.search("4", opt):
599                 makeMP4=1
600             recdblist.printutf8(pin)
601             if not re.search("0", opt):
602                 if makeMP4==1:
603                     pout = recpath + "/" + title + ".mp4"
604                     tv2mp4.ts2mp4(pin, pout, opt)
605                 else:
606                     pout = recpath + "/" + title + ".mkv"
607                     tv2mkv.ts2mkv(pin, pout, opt)
608                 if re.search("8", opt) or re.search("9", opt):
609                     optt=opt
610                     optt=optt.replace("8","MW8")
611                     optt=optt.replace("9","MW9")
612                     poutt = recpath + "/" + "m_"+title + ".mp4"
613                     shutil.move(os.path.join(recpath, title+".ts"),os.path.join(recpath, "m_"+title+".ts"))
614                     tv2mp4.ts2mp4(os.path.join(recpath, "m_"+title+".ts"), poutt, optt)
615                     shutil.move(os.path.join(recpath, "m_"+title+".ts"),os.path.join(recpath, title+".ts"))
616             else:
617                 optt=opt
618                 optt=optt.replace("8","MW8")
619                 optt=optt.replace("9","MW9")
620                 poutt = recpath + "/" + "m_"+title + ".mp4"
621                 shutil.move(os.path.join(recpath, title+".ts"),os.path.join(recpath, "m_"+title+".ts"))
622                 tv2mp4.ts2mp4(os.path.join(recpath, "m_"+title+".ts"), poutt, optt)
623                 shutil.move(os.path.join(recpath, "m_"+title+".ts"),os.path.join(recpath, title+".ts"))
624             recdb.del_reckey(typetxting, title, chtxt, btime)
625             time.sleep(10)
626             if re.search("E", opt):
627                 try:
628                     if os.path.exists(os.path.join(recpath,title+".mp4")):
629                         shutil.copy(os.path.join(recpath,title+".mp4"), os.path.join(movepath,title+".mp4"))
630                     elif os.path.exists(os.path.join(recpath,title+".mkv")):
631                         shutil.copy(os.path.join(recpath,title+".mkv"), os.path.join(movepath,title+".mkv"))
632                 except Exception, inst:
633                     errtxt="Error happened in type_encode_que in timerec.\n"
634                     errtxt=errtxt+"move mkv/mp4 error.\n"
635                     errtxt=errtxt+str(type(inst))+"\n"
636                     errtxt=errtxt+str(inst)
637                     recdblist.addlog(os.path.join(recpath,title+".ts"), errtxt, "timerec-move(Encode) error")
638                 recdb.rec_reckey(recdblist.REC_MOVE_END, title, chtxt, btime, etime, opt)
639             recdb.rec_reckey(typetxtfin, title, chtxt, btime, etime, opt)
640             sys.exit()
641         else:
642             recdb.del_reckey(typetxtnow, title, chtxt, btime)
643             bt = bt + datetime.timedelta(seconds=600)
644             et = et + datetime.timedelta(seconds=600)
645             btime = bt.strftime("%Y-%m-%d %H:%M:%S")
646             etime = et.strftime("%Y-%m-%d %H:%M:%S")
647             recdb.rec_reckey(typetxtnow, title, chtxt, btime, etime, opt)
648             sys.exit()