3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
27 recpath = configreader.getpath('recpath')
28 movepath = configreader.getpath('move_destpath')
29 path = str(os.path.dirname(os.path.abspath(__file__))) + "/"
36 if rec10d.rec10db.select_installed_in_status()==0 or rec10d.rec10db.select_version_in_status()<recdblist.version:
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()) )
49 dnow=datetime.datetime.now()
55 for k in recdb.get_key():
64 auto_process.auto_check(recpath)
66 auto_process.kill_dead_encode(recpath)
68 update = chdb.update()
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
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
87 rec10d.rec10db.update_status_by_bctype_epg_ch(bctype, "0")
88 epgdb.updatebc(bctype)
89 update = chdb.update()
94 auto_process.update_all_timeline_epg()
101 auto_process.update_all_timeline_epg()
104 typetxt = task["type"]
106 chtxt = task['chtxt']
110 title = task['title']
114 btime = task['btime']
115 bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
118 bt = datetime.datetime.strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
120 etime = task['etime']
121 et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
124 et = datetime.datetime.strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
129 tnow = datetime.datetime.now()
131 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
132 if task["type"] == recdblist.REC_RESERVE:#"res,"+chtxt+","+title+","+btime+","+etime+","+opt
134 if pid != 0:#親プロセスの場合
136 else:#子プロセスの場合 アップデートを行って終了
137 type_reserve(task["type"],chtxt,title,bt,et,opt)
139 elif task["type"] == recdblist.REC_KEYWORD:#"key,"+chtxt+","+keyword+","+btime+","+deltatime+","+opt
140 deltatime = task['deltatime']
142 if pid != 0:#親プロセスの場合
144 else:#子プロセスの場合 アップデートを行って終了
145 type_keyword(task["type"],chtxt,title, bt, et, opt, deltatime)
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']
151 if pid != 0:#親プロセスの場合
153 else:#子プロセスの場合 アップデートを行って終了
154 type_keyword_every_day(task["type"],chtxt, title, bt, et, opt, deltatime, deltaday)
156 elif task["type"] == recdblist.REC_FINAL_RESERVE:#"rec,"+chtxt+","+title+","+btime+","+etime+","+opt
158 if pid != 0:#親プロセスの場合
160 else:#子プロセスの場合 アップデートを行って終了
161 type_final(task["type"],chtxt, title, bt, et, opt)
163 elif task["type"] == recdblist.REC_TS_DECODE_QUE:
166 if pid != 0:#親プロセスの場合
168 else:#子プロセスの場合 アップデートを行って終了
170 type_decode_que(task["type"],chtxt, title, bt, et, opt)
172 elif task["type"] == recdblist.REC_ENCODE_QUE:
173 encodenum=encodenum+1
175 if pid != 0:#親プロセスの場合
177 else:#子プロセスの場合 アップデートを行って終了
178 time.sleep(5*encodenum)
179 type_encode_que(task["type"],chtxt, title, bt, et, opt)
181 elif task["type"] == recdblist.REC_AVI_TO_MKV:
189 if configreader.getenv("make_mp4")==1:
193 if re.search("m", opt):
195 if re.search("4", opt):
197 recdb.del_reckey(recdblist.REC_AVI_TO_MKV, title, chtxt, btime)
198 recdb.rec_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime, etime, opt)
200 tv2mp4.raw2mp4(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mp4"),opt)
202 tv2mkv.raw2mkv(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mkv"),opt)
203 recdb.del_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime)
205 elif task["type"] == recdblist.REC_AVI_TO_MP4:
213 if configreader.getenv("make_mp4")==1:
217 if re.search("m", opt):
219 if re.search("4", opt):
221 recdb.del_reckey(recdblist.REC_AVI_TO_MP4, title, chtxt, btime)
222 recdb.rec_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime, etime, opt)
224 tv2mp4.ts2mp4(pin, pout, opt)
225 tv2mp4.raw2mp4(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mp4"),opt)
227 tv2mkv.raw2mkv(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mkv"),opt)
228 recdb.del_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime)
230 elif task["type"] == recdblist.REC_MKV_TO_MP4:
236 recdb.del_reckey(recdblist.REC_MKV_TO_MP4, title, chtxt, btime)
237 recdb.rec_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime, etime, opt)
238 tv2mp4.mkv2mp4(os.path.join(recpath,title+".mkv"),os.path.join(recpath,title+".mp4"))
239 recdb.del_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime)
242 def search_keyword(key):
243 tnow = datetime.datetime.now()
244 nows =tnow.strftime("%Y-%m-%d %H:%M:%S")
246 recdatum = epgdb.searchtime_auto(k,nows, "144")
247 for recdata in recdatum:
253 btt = datetime.datetime.strptime(btimet, "%Y-%m-%d %H:%M:%S")
254 ett = datetime.datetime.strptime(etimet, "%Y-%m-%d %H:%M:%S")
255 btimet = btt.strftime("%Y-%m-%d %H:%M:%S")
256 etimet = ett.strftime("%Y-%m-%d %H:%M:%S")
257 if status.getSettings_auto_jbk()==1:
258 topt=status.getSettings_auto_opt()
262 maxnum=epgdb.count_schedule_timeline(btimet, etimet)[1]
263 maxnum=int(configreader.getenv("bscs_max"))-maxnum
265 maxnum=epgdb.count_schedule_timeline(btimet, etimet)[0]
266 maxnum=int(configreader.getenv("te_max"))-maxnum
268 topt=status.getSettings_auto_opt()
269 recdb.recreserv(titlet,chtxtt, btimet, etimet,topt)
270 except Exception, inst:
271 print "error occures in search_keyword_auto_jbk"
272 print str(type(inst))
276 recdb.add_auto_keyword(chtxtt, titlet, btimet, etimet)
277 except Exception, inst:
278 print "error occures in search_keyword"
279 print str(type(inst))
281 def type_reserve(typetxt,chtxt,title,bt,et,opt):
282 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
283 etime = et.strftime("%Y-%m-%d %H:%M:%S")
286 if typetxt==recdblist.REC_RESERVE:
287 typetxtfinal=recdblist.REC_FINAL_RESERVE
288 tnow = datetime.datetime.now()
290 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
291 if (dt < 58 * 60 and dt > 30 * 60):
292 bctypet = chdb.chtxtsearch(chtxt)['bctype']
293 chdatat = rec10d.rec10db.select_by_bctype_epg_ch(bctypet)
294 dt1 = datetime.datetime.strptime(chdatat[0][5], "%Y-%m-%d %H:%M:%S")-datetime.datetime.now()
295 dt1 = dt1.days * 24 * 60 * 60 + dt1.seconds
297 recdata = epgdb.searchtime2(title, btime, "5", chtxt)
305 bt = datetime.datetime.strptime(btimen, "%Y-%m-%d %H:%M:%S")
306 et = datetime.datetime.strptime(etimen, "%Y-%m-%d %H:%M:%S")
307 btimen = bt.strftime("%Y-%m-%d %H:%M:%S")
308 etimen = et.strftime("%Y-%m-%d %H:%M:%S")
311 recdb.del_reckey(typetxtnow, title, chtxt, btime)
312 recdb.rec_reckey(typetxtfinal, titlen, chtxtn, btimen, etimen, opt)
313 recdb.rec_reclog(titlen, chtxtn, btimen, etimen, opt, exp, longexp, category)
314 auto_rec.add_key(chtxt, titlen,exp+" "+longexp)
315 auto_rec.add_key("ALL", titlen,exp+" "+longexp)
316 recdblist.printutf8(u"追いかけ機能実行中: "+title+" : "+titlen+" "+btimen+" "+etimen)
317 except Exception, inst:
318 print "Error happened in Oikake DB"
319 print str(type(inst))
322 recdblist.printutf8(u"追いかけ機能エラー:番組データが見付かりません。")
324 if rec10d.rec10db.select_by_bctype_epg_ch(bctypet)[0][6] != "0":
325 rec10d.rec10db.update_status_by_bctype_epg_ch(bctypet, "3")
327 elif (dt <= 30 * 60 and dt > 20 * 60):
328 recdata = epgdb.searchtime2(title, btime, "5", chtxt)
336 bt = datetime.datetime.strptime(btimen, "%Y-%m-%d %H:%M:%S")
337 et = datetime.datetime.strptime(etimen, "%Y-%m-%d %H:%M:%S")
338 btimen = bt.strftime("%Y-%m-%d %H:%M:%S")
339 etimen = et.strftime("%Y-%m-%d %H:%M:%S")
342 recdb.del_reckey(typetxtnow, title, chtxt, btime)
343 recdb.rec_reckey(typetxtfinal, titlen, chtxtn, btimen, etimen, opt)
344 recdb.rec_reclog(titlen, chtxtn, btimen, etimen, opt, exp, longexp, category)
345 auto_rec.add_key(chtxt, titlen,exp+" "+longexp)
346 auto_rec.add_key("ALL", titlen,exp+" "+longexp)
347 recdblist.printutf8(u"追いかけ機能実行中: "+title+" : "+titlen+" "+btimen+" "+etimen)
348 except Exception, inst:
349 print "Error happened in Oikake DB"
354 recdb.del_reckey(typetxtnow, title, chtxt, btime)
355 recdb.rec_reckey(typetxtfinal, title, chtxt, btime, etime, opt)
356 except Exception, inst:
359 def type_final(typetxt,chtxt,title,bt,et,opt):
360 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
361 etime = et.strftime("%Y-%m-%d %H:%M:%S")
365 if typetxt==recdblist.REC_FINAL_RESERVE:
366 typetxting=recdblist.REC_TS_RECORDING
367 typetxtdecque=recdblist.REC_TS_DECODE_QUE
368 tnow = datetime.datetime.now()
370 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
372 if dt < 6 * 60 and dt > 0:
374 recdb.del_reckey(typetxtnow, title, chtxt, btime)
375 testpath=[os.path.join(recpath,title+".ts.b25")]
376 testpath.append(os.path.join(recpath,title+".ts"))
377 testpath.append(os.path.join(recpath,title+".avi"))
378 testpath.append(os.path.join(recpath,title+".mp4"))
379 testpath.append(os.path.join(recpath,title+".log"))
382 if os.path.exists(ti):
384 if re.search("N", opt) or tcheck>0:
387 iff=u"("+configreader.getenv("iff")+u")_"
391 newtitle=newtitle+u"_"+iff+newtime.strftime("%Y-%m-%dT%H-%M-%S")
392 recdb.rec_reckey(typetxting, newtitle, chtxt, btime, etime, opt)
393 recdblist.printutf8(u"録画開始 "+newtitle+" "+btime+" "+etime)
394 tv2avi.timetv2b25(recpath + "/" + newtitle + ".avi", chtxt, btime, etime, opt)
395 recdb.del_reckey(typetxting, newtitle, chtxt, btime)
396 if not re.search("R", opt):
397 tnow = datetime.datetime.now()
398 bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
399 et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
401 bt = tnow + datetime.timedelta(seconds=600)
402 et = et + dt + datetime.timedelta(seconds=600)
403 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
404 etime = et.strftime("%Y-%m-%d %H:%M:%S")
405 recdb.rec_reckey(typetxtdecque, newtitle, chtxt, btime, etime, opt)
408 shutil.copy(os.path.join(recpath,title+".ts.b25"), os.path.join(movepath,title+".ts.b25"))
411 recdb.rec_reckey(recdblist.REC_MOVE_END, newtitle, chtxt, btime, etime, opt)
413 def type_keyword(typetxt,chtxt,title,bt,et,opt,deltatime):
414 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
415 etime = et.strftime("%Y-%m-%d %H:%M:%S")
418 if typetxt==recdblist.REC_KEYWORD:
419 typetxtres=recdblist.REC_RESERVE
420 tnow = datetime.datetime.now()
422 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
425 recdata = epgdb.searchtime2(title, btime, deltatime, chtxt)
434 bt = datetime.datetime.strptime(btimet, "%Y-%m-%d %H:%M:%S")
435 et = datetime.datetime.strptime(etimet, "%Y-%m-%d %H:%M:%S")
436 btimet = bt.strftime("%Y-%m-%d %H:%M:%S")
437 etimet = et.strftime("%Y-%m-%d %H:%M:%S")
439 recdb.del_reckey(typetxtnow, title, chtxt, btime)
440 recdb.rec_reckey(typetxtres, titlet, chtxtt, btimet, etimet, opt)
441 recdb.rec_reclog(titlet, chtxtt, btimet, etimet, opt, exp,longexp,category)
442 auto_rec.add_key(chtxt, titlet,exp+" "+longexp)
443 auto_rec.add_key("ALL", titlet,exp+" "+longexp)
444 recdblist.printutf8(u"key "+title+u" : "+titlet+u" "+btimet+u" "+etimet)
445 #except Exception, inst:
446 # recdblist.printutf8("Error happened in REC_KEYWORD DB")
447 # recdblist.printutf8(type(inst))
448 # recdblist.printutf8(inst)
450 recdblist.printutf8("nothing match")
452 def type_keyword_every_day(type,chtxt,title,bt,et,opt,deltatime,deltaday):
453 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
454 etime = et.strftime("%Y-%m-%d %H:%M:%S")
455 tnow = datetime.datetime.now()
457 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
458 dd = datetime.timedelta(days=int(deltaday))
460 recdb.del_reckey(recdblist.REC_KEYWORD_EVERY_SOME_DAYS, title, chtxt, btime)
463 btxt = bt.strftime("%Y-%m-%d %H:%M:%S")
464 etxt = et.strftime("%Y-%m-%d %H:%M:%S")
465 recdb.everyreserv(title, chtxt, btxt, etxt, deltatime, opt, deltaday)
467 recdb.del_reckey(recdblist.REC_KEYWORD_EVERY_SOME_DAYS, title, chtxt, btime)
470 btxt = bt.strftime("%Y-%m-%d %H:%M:%S")
471 etxt = et.strftime("%Y-%m-%d %H:%M:%S")
472 recdb.everyreserv(title, chtxt, btxt, etxt, deltatime, opt, deltaday)
474 recdata = epgdb.searchtime2(title, btime, deltatime, chtxt)
483 bt = datetime.datetime.strptime(btimet, "%Y-%m-%d %H:%M:%S")
484 et = datetime.datetime.strptime(etimet, "%Y-%m-%d %H:%M:%S")
485 btimet = bt.strftime("%Y-%m-%d %H:%M:%S")
486 etimet = et.strftime("%Y-%m-%d %H:%M:%S")
488 recdb.keyreserv(titlet, chtxtt, btimet, etimet, deltatime, opt)
490 recdb.keyreserv(title, chtxt, btime, etime, deltatime, opt)
491 def type_decode_que(typetxt,chtxt,title,bt,et,opt):
492 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
493 etime = et.strftime("%Y-%m-%d %H:%M:%S")
494 tnow = datetime.datetime.now()
496 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
501 if typetxt== recdblist.REC_TS_DECODE_QUE:
502 typetxting=recdblist.REC_TS_DECODING
503 typetxtmiss=recdblist.REC_MISS_DECODE
504 typetxtencque=recdblist.REC_ENCODE_QUE
505 tnow = datetime.datetime.now()
507 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
509 if status.getB25Decoding() < 2:
510 pin = recpath + "/" + title
511 recdb.del_reckey(typetxtnow, title, chtxt, btime)
512 recdb.rec_reckey(typetxting, title, chtxt, btime, etime, opt)
513 tv2avi.b252ts(pin, chtxt, btime, etime, opt)
514 recdb.del_reckey(typetxting, title, chtxt, btime)
515 if not os.access(recpath + "/" + title + ".ts", os.F_OK):
516 recdb.del_reckey(typetxting, title, chtxt, btime)
517 recdb.rec_reckey(typetxtmiss, title, chtxt, btime, etime, opt)
519 ch=checker.checkB25Decode(recpath + "/" + title + ".ts.b25", recpath + "/" + title + ".ts")
522 os.remove(recpath + "/" + title + ".ts.b25")
523 bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
524 et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
526 if not re.search("D", opt):
527 tnow = datetime.datetime.now()
528 bt = tnow + datetime.timedelta(seconds=600)
530 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
531 etime = et.strftime("%Y-%m-%d %H:%M:%S")
532 recdb.rec_reckey(typetxtencque, title, chtxt, btime, etime, opt)
535 shutil.copy(os.path.join(recpath,title+".ts"), os.path.join(movepath,title+".ts"))
536 except Exception, inst:
537 errtxt="Error happened in type_decode_que in timerec.\n"
538 errtxt=errtxt+"move ts error.\n"
539 errtxt=errtxt+str(type(inst))+"\n"
540 errtxt=errtxt+str(inst)
541 recdblist.addlog(os.path.join(recpath,title+".ts"), errtxt, "timerec-move(Decode) error")
542 recdb.rec_reckey(recdblist.REC_MOVE_END, title, chtxt, btime, etime, opt)
544 recdb.del_reckey(typetxtnow, title, chtxt, btime)
545 bt = bt + datetime.timedelta(seconds=600)
546 et = et + datetime.timedelta(seconds=600)
547 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
548 etime = et.strftime("%Y-%m-%d %H:%M:%S")
549 recdb.rec_reckey(typetxtnow, title, chtxt, btime, etime, opt)
552 def type_encode_que(typetxt,chtxt,title,bt,et,opt):
553 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
554 etime = et.strftime("%Y-%m-%d %H:%M:%S")
555 tnow = datetime.datetime.now()
557 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
561 if typetxt==recdblist.REC_ENCODE_QUE:
562 typetxting=recdblist.REC_ENCODE_LOCAL
563 typetxtfin=recdblist.REC_FIN_LOCAL
565 if status.getEncoding() < int(configreader.getenv("enc_max")):
566 recdb.del_reckey(typetxtnow, title, chtxt, btime)
567 recdb.rec_reckey(typetxting, title, chtxt, btime, etime, opt)
568 recdblist.printutf8(opt)
569 pin = recpath + "/" + title + ".ts"
570 if re.search("d", opt):
571 pin = recpath + "/" + title + ".m2v"
572 if not os.path.exists(pin) or os.path.getsize(pin)<100*1000:
573 paac2 = recpath + "/" + title + "_2.aac"
574 pmp32 = recpath + "/" + title + "_2.mp3"
575 if not os.path.exists(paac2) and not os.path.exists(pmp32):
576 tv2avi.dualaudio2sep(os.path.join(recpath, title+".ts"),"167")
578 elif re.search("5", opt):
579 pin = recpath + "/" + title + ".m2v"
580 if not os.path.exists(pin) or os.path.getsize(pin)<100*1000:
581 paac2 = recpath + "/" + title + "_2.aac"
582 pmp32 = recpath + "/" + title + "_2.mp3"
583 if not os.path.exists(paac2) and not os.path.exists(pmp32):
584 tv2avi.pentaaudio2sep(os.path.join(recpath, title+".ts"))
588 if configreader.getenv("make_mp4")=="1":
592 if re.search("m", opt):
594 if re.search("4", opt):
596 recdblist.printutf8(pin)
598 pout = recpath + "/" + title + ".mp4"
599 tv2mp4.ts2mp4(pin, pout, opt)
601 pout = recpath + "/" + title + ".mkv"
602 tv2mkv.ts2mkv(pin, pout, opt)
603 recdb.del_reckey(typetxting, title, chtxt, btime)
605 if re.search("E", opt):
607 if os.path.exists(os.path.join(recpath,title+".mp4")):
608 shutil.copy(os.path.join(recpath,title+".mp4"), os.path.join(movepath,title+".mp4"))
609 elif os.path.exists(os.path.join(recpath,title+".mkv")):
610 shutil.copy(os.path.join(recpath,title+".mkv"), os.path.join(movepath,title+".mkv"))
611 except Exception, inst:
612 errtxt="Error happened in type_encode_que in timerec.\n"
613 errtxt=errtxt+"move mkv/mp4 error.\n"
614 errtxt=errtxt+str(type(inst))+"\n"
615 errtxt=errtxt+str(inst)
616 recdblist.addlog(os.path.join(recpath,title+".ts"), errtxt, "timerec-move(Encode) error")
617 recdb.rec_reckey(recdblist.REC_MOVE_END, title, chtxt, btime, etime, opt)
618 recdb.rec_reckey(typetxtfin, title, chtxt, btime, etime, opt)
621 recdb.del_reckey(typetxtnow, title, chtxt, btime)
622 bt = bt + datetime.timedelta(seconds=600)
623 et = et + datetime.timedelta(seconds=600)
624 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
625 etime = et.strftime("%Y-%m-%d %H:%M:%S")
626 recdb.rec_reckey(typetxtnow, title, chtxt, btime, etime, opt)