OSDN Git Service

implement single audio using bontsdemux option(b).
[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                 cstch=-1
524                 try:
525                     cstch=int(chdb.chtxtsearch(chtxt)['csch'])
526                 except:
527                     cstch=-1
528                 if cstch>-1:
529                     ch=checker.checkB25Decode(recpath + "/" + title + ".ts.b25", recpath + "/" + title + ".ts",cstch)
530                 #print ch
531                 if ch == 1:
532                     os.remove(recpath + "/" + title + ".ts.b25")
533             bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
534             et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
535             dt = et-bt
536             if not re.search("D", opt):
537                 tnow = datetime.datetime.now()
538                 bt = tnow + datetime.timedelta(seconds=600)
539                 et = bt + dt
540                 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
541                 etime = et.strftime("%Y-%m-%d %H:%M:%S")
542                 recdb.rec_reckey(typetxtencque, title, chtxt, btime, etime, opt)
543             else:
544                 try:
545                     shutil.copy(os.path.join(recpath,title+".ts"), os.path.join(movepath,title+".ts"))
546                 except Exception, inst:
547                     errtxt="Error happened in type_decode_que in timerec.\n"
548                     errtxt=errtxt+"move ts error.\n"
549                     errtxt=errtxt+str(type(inst))+"\n"
550                     errtxt=errtxt+str(inst)
551                     recdblist.addlog(os.path.join(recpath,title+".ts"), errtxt, "timerec-move(Decode) error")
552                 recdb.rec_reckey(recdblist.REC_MOVE_END, title, chtxt, btime, etime, opt)
553         else:
554             recdb.del_reckey(typetxtnow, title, chtxt, btime)
555             bt = bt + datetime.timedelta(seconds=600)
556             et = et + datetime.timedelta(seconds=600)
557             btime = bt.strftime("%Y-%m-%d %H:%M:%S")
558             etime = et.strftime("%Y-%m-%d %H:%M:%S")
559             recdb.rec_reckey(typetxtnow, title, chtxt, btime, etime, opt)
560     sys.exit()
561
562 def type_encode_que(typetxt,chtxt,title,bt,et,opt):
563     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
564     etime = et.strftime("%Y-%m-%d %H:%M:%S")
565     tnow = datetime.datetime.now()
566     dtt = bt-tnow
567     dt = dtt.days * 24 * 60 * 60 + dtt.seconds
568     typetxtnow=typetxt
569     typetxting=""
570     typetxtfin=""
571     if typetxt==recdblist.REC_ENCODE_QUE:
572         typetxting=recdblist.REC_ENCODE_LOCAL
573         typetxtfin=recdblist.REC_FIN_LOCAL
574     if dt < 10 * 60:
575         if status.getEncoding() < int(configreader.getenv("enc_max")):
576             recdb.del_reckey(typetxtnow, title, chtxt, btime)
577             recdb.rec_reckey(typetxting, title, chtxt, btime, etime, opt)
578             recdblist.printutf8(opt)
579             pin = recpath + "/" + title + ".ts"
580             if re.search("d", opt):
581                 pin = recpath + "/" + title + ".m2v"
582                 if not os.path.exists(pin) or os.path.getsize(pin)<100*1000:
583                     paac2 = recpath + "/" + title + "_2.aac"
584                     pmp32 = recpath + "/" + title + "_2.mp3"
585                     if not os.path.exists(paac2) and not os.path.exists(pmp32):
586                         tv2avi.dualaudio2sep(os.path.join(recpath, title+".ts"),"167")
587                 time.sleep(3)
588             elif re.search("5", opt):
589                 pin = recpath + "/" + title + ".m2v"
590                 if not os.path.exists(pin) or os.path.getsize(pin)<100*1000:
591                     paac2 = recpath + "/" + title + "_2.aac"
592                     pmp32 = recpath + "/" + title + "_2.mp3"
593                     if not os.path.exists(paac2) and not os.path.exists(pmp32):
594                         tv2avi.pentaaudio2sep(os.path.join(recpath, title+".ts"))
595                 time.sleep(3)
596             elif re.search("b", opt):
597                 pin = recpath + "/" + title + ".m2v"
598                 if not os.path.exists(pin) or os.path.getsize(pin)<100*1000:
599                     paac2 = recpath + "/" + title + "_1.aac"
600                     pmp32 = recpath + "/" + title + "_1.mp3"
601                     if not os.path.exists(paac2) and not os.path.exists(pmp32):
602                         tv2avi.singleaudio2sep(os.path.join(recpath, title+".ts"))
603                 time.sleep(3)
604             makeMP4=0
605             try:
606                 if configreader.getenv("make_mp4")=="1":
607                     makeMP4=1
608             except:
609                 ""
610             if re.search("m", opt):
611                 makeMP4=0
612             if re.search("4", opt):
613                 makeMP4=1
614             recdblist.printutf8(pin)
615             if not re.search("0", opt):
616                 if makeMP4==1:
617                     pout = recpath + "/" + title + ".mp4"
618                     tv2mp4.ts2mp4(pin, pout, opt)
619                 else:
620                     pout = recpath + "/" + title + ".mkv"
621                     tv2mkv.ts2mkv(pin, pout, opt)
622                 if re.search("8", opt) or re.search("9", opt):
623                     optt=opt
624                     optt=optt.replace("8","MW8")
625                     optt=optt.replace("9","MW9")
626                     poutt = recpath + "/" + "m_"+title + ".mp4"
627                     if re.search("d", opt) or re.search("5", opt):
628                         if os.path.exists(os.path.join(recpath, "m_"+title+".m2v")):
629                             shutil.move(os.path.join(recpath, title+".m2v"),os.path.join(recpath, "m_"+title+".m2v"))
630                         if os.path.exists(os.path.join(recpath, "m_"+title+"_1.aac")):
631                             shutil.move(os.path.join(recpath, title+"_1.aac"),os.path.join(recpath, "m_"+title+"_1.aac"))
632                         if os.path.exists(os.path.join(recpath, "m_"+title+"_1.mp3")):
633                             shutil.move(os.path.join(recpath, title+"_1.mp3"),os.path.join(recpath, "m_"+title+"_1.mp3"))
634                         if os.path.exists(os.path.join(recpath, "m_"+title+"_2.aac")):
635                             shutil.move(os.path.join(recpath, title+"_2.aac"),os.path.join(recpath, "m_"+title+"_2.aac"))
636                         if os.path.exists(os.path.join(recpath, "m_"+title+"_2.mp3")):
637                             shutil.move(os.path.join(recpath, title+"_2.mp3"),os.path.join(recpath, "m_"+title+"_2.mp3"))
638                     shutil.move(os.path.join(recpath, title+".ts"),os.path.join(recpath, "m_"+title+".ts"))
639                     tv2mp4.ts2mp4(os.path.join(recpath, "m_"+title+".ts"), poutt, optt)
640                     shutil.move(os.path.join(recpath, "m_"+title+".ts"),os.path.join(recpath, title+".ts"))
641             else:
642                 optt=opt
643                 optt=optt.replace("8","MW8")
644                 optt=optt.replace("9","MW9")
645                 poutt = recpath + "/" + "m_"+title + ".mp4"
646                 shutil.move(os.path.join(recpath, title+".ts"),os.path.join(recpath, "m_"+title+".ts"))
647                 if re.search("d", opt) or re.search("5", opt):
648                     if os.path.exists(os.path.join(recpath, "m_"+title+".m2v")):
649                         shutil.move(os.path.join(recpath, title+".m2v"),os.path.join(recpath, "m_"+title+".m2v"))
650                     if os.path.exists(os.path.join(recpath, "m_"+title+"_1.aac")):
651                         shutil.move(os.path.join(recpath, title+"_1.aac"),os.path.join(recpath, "m_"+title+"_1.aac"))
652                     if os.path.exists(os.path.join(recpath, "m_"+title+"_1.mp3")):
653                         shutil.move(os.path.join(recpath, title+"_1.mp3"),os.path.join(recpath, "m_"+title+"_1.mp3"))
654                     if os.path.exists(os.path.join(recpath, "m_"+title+"_2.aac")):
655                         shutil.move(os.path.join(recpath, title+"_2.aac"),os.path.join(recpath, "m_"+title+"_2.aac"))
656                     if os.path.exists(os.path.join(recpath, "m_"+title+"_2.mp3")):
657                         shutil.move(os.path.join(recpath, title+"_2.mp3"),os.path.join(recpath, "m_"+title+"_2.mp3"))
658                 tv2mp4.ts2mp4(os.path.join(recpath, "m_"+title+".ts"), poutt, optt)
659                 shutil.move(os.path.join(recpath, "m_"+title+".ts"),os.path.join(recpath, title+".ts"))
660             recdb.del_reckey(typetxting, title, chtxt, btime)
661             time.sleep(10)
662             if re.search("E", opt):
663                 try:
664                     if os.path.exists(os.path.join(recpath,title+".mp4")):
665                         shutil.copy(os.path.join(recpath,title+".mp4"), os.path.join(movepath,title+".mp4"))
666                     elif os.path.exists(os.path.join(recpath,title+".mkv")):
667                         shutil.copy(os.path.join(recpath,title+".mkv"), os.path.join(movepath,title+".mkv"))
668                 except Exception, inst:
669                     errtxt="Error happened in type_encode_que in timerec.\n"
670                     errtxt=errtxt+"move mkv/mp4 error.\n"
671                     errtxt=errtxt+str(type(inst))+"\n"
672                     errtxt=errtxt+str(inst)
673                     recdblist.addlog(os.path.join(recpath,title+".ts"), errtxt, "timerec-move(Encode) error")
674                 recdb.rec_reckey(recdblist.REC_MOVE_END, title, chtxt, btime, etime, opt)
675             recdb.rec_reckey(typetxtfin, title, chtxt, btime, etime, opt)
676             sys.exit()
677         else:
678             recdb.del_reckey(typetxtnow, title, chtxt, btime)
679             bt = bt + datetime.timedelta(seconds=600)
680             et = et + datetime.timedelta(seconds=600)
681             btime = bt.strftime("%Y-%m-%d %H:%M:%S")
682             etime = et.strftime("%Y-%m-%d %H:%M:%S")
683             recdb.rec_reckey(typetxtnow, title, chtxt, btime, etime, opt)
684             sys.exit()