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 update = chdb.update()
75 if bctype.find('te') > -1:
76 recnum = status.getTERecording() + recdb.countRecNow_minutes_TE("10")
77 recdblist.printutf8(u"放送種別:"+bctype + u"||該当チューナー実行中件数:" + str(status.getTERecording()) + u":直近予約件数:" + str(recdb.countRecNow_minutes_TE("10")))
78 recnum = int(configreader.getenv("te_max")) -recnum
80 recnum = status.getBSCSRecording() + recdb.countRecNow_minutes_BSCS("10")
81 recdblist.printutf8(u"放送種別:"+bctype + u"||該当チューナー実行中件数:" + str(status.getBSCSRecording()) + u":直近予約件数:" + str(recdb.countRecNow_minutes_BSCS("10")))
82 recnum = int(configreader.getenv("bscs_max")) -recnum
85 rec10d.rec10db.update_status_by_bctype_epg_ch(bctype, "0")
86 epgdb.updatebc(bctype)
87 update = chdb.update()
92 auto_process.update_all_timeline_epg()
99 auto_process.update_all_timeline_epg()
102 typetxt = task["type"]
104 chtxt = task['chtxt']
108 title = task['title']
112 btime = task['btime']
113 bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
116 bt = datetime.datetime.strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
118 etime = task['etime']
119 et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
122 et = datetime.datetime.strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
127 tnow = datetime.datetime.now()
129 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
130 if task["type"] == recdblist.REC_RESERVE:#"res,"+chtxt+","+title+","+btime+","+etime+","+opt
132 if pid != 0:#親プロセスの場合
134 else:#子プロセスの場合 アップデートを行って終了
135 type_reserve(task["type"],chtxt,title,bt,et,opt)
137 elif task["type"] == recdblist.REC_KEYWORD:#"key,"+chtxt+","+keyword+","+btime+","+deltatime+","+opt
138 deltatime = task['deltatime']
140 if pid != 0:#親プロセスの場合
142 else:#子プロセスの場合 アップデートを行って終了
143 type_keyword(task["type"],chtxt,title, bt, et, opt, deltatime)
145 elif task["type"] == recdblist.REC_KEYWORD_EVERY_SOME_DAYS:#"keyevery,"+chtxt+","+keyword+","+btime+","+deltatime+","+opt+","+deltaday
146 deltatime = task['deltatime']
147 deltaday = task['deltaday']
149 if pid != 0:#親プロセスの場合
151 else:#子プロセスの場合 アップデートを行って終了
152 type_keyword_every_day(task["type"],chtxt, title, bt, et, opt, deltatime, deltaday)
154 elif task["type"] == recdblist.REC_FINAL_RESERVE:#"rec,"+chtxt+","+title+","+btime+","+etime+","+opt
156 if pid != 0:#親プロセスの場合
158 else:#子プロセスの場合 アップデートを行って終了
159 type_final(task["type"],chtxt, title, bt, et, opt)
161 elif task["type"] == recdblist.REC_TS_DECODE_QUE:
164 if pid != 0:#親プロセスの場合
166 else:#子プロセスの場合 アップデートを行って終了
168 type_decode_que(task["type"],chtxt, title, bt, et, opt)
170 elif task["type"] == recdblist.REC_ENCODE_QUE:
171 encodenum=encodenum+1
173 if pid != 0:#親プロセスの場合
175 else:#子プロセスの場合 アップデートを行って終了
176 time.sleep(5*encodenum)
177 type_encode_que(task["type"],chtxt, title, bt, et, opt)
179 elif task["type"] == recdblist.REC_AVI_TO_MKV:
187 if configreader.getenv("make_mp4")==1:
191 if re.search("m", opt):
193 if re.search("4", opt):
195 recdb.del_reckey(recdblist.REC_AVI_TO_MKV, title, chtxt, btime)
196 recdb.rec_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime, etime, opt)
198 tv2mp4.raw2mp4(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mp4"),opt)
200 tv2mkv.raw2mkv(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mkv"),opt)
201 recdb.del_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime)
203 elif task["type"] == recdblist.REC_AVI_TO_MP4:
211 if configreader.getenv("make_mp4")==1:
215 if re.search("m", opt):
217 if re.search("4", opt):
219 recdb.del_reckey(recdblist.REC_AVI_TO_MP4, title, chtxt, btime)
220 recdb.rec_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime, etime, opt)
222 tv2mp4.ts2mp4(pin, pout, opt)
223 tv2mp4.raw2mp4(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mp4"),opt)
225 tv2mkv.raw2mkv(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mkv"),opt)
226 recdb.del_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime)
228 elif task["type"] == recdblist.REC_MKV_TO_MP4:
234 recdb.del_reckey(recdblist.REC_MKV_TO_MP4, title, chtxt, btime)
235 recdb.rec_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime, etime, opt)
236 tv2mp4.mkv2mp4(os.path.join(recpath,title+".mkv"),os.path.join(recpath,title+".mp4"))
237 recdb.del_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime)
240 def search_keyword(key):
241 tnow = datetime.datetime.now()
242 nows =tnow.strftime("%Y-%m-%d %H:%M:%S")
244 recdatum = epgdb.searchtime_auto(k,nows, "144")
245 for recdata in recdatum:
251 btt = datetime.datetime.strptime(btimet, "%Y-%m-%d %H:%M:%S")
252 ett = datetime.datetime.strptime(etimet, "%Y-%m-%d %H:%M:%S")
253 btimet = btt.strftime("%Y-%m-%d %H:%M:%S")
254 etimet = ett.strftime("%Y-%m-%d %H:%M:%S")
255 if status.getSettings_auto_jbk()==1:
256 topt=status.getSettings_auto_opt()
260 maxnum=epgdb.count_schedule_timeline(btimet, etimet)[1]
261 maxnum=int(configreader.getenv("bscs_max"))-maxnum
263 maxnum=epgdb.count_schedule_timeline(btimet, etimet)[0]
264 maxnum=int(configreader.getenv("te_max"))-maxnum
266 topt=status.getSettings_auto_opt()
267 recdb.recreserv(titlet,chtxtt, btimet, etimet,topt)
268 except Exception, inst:
269 print "error occures in search_keyword_auto_jbk"
270 print str(type(inst))
274 recdb.add_auto_keyword(chtxtt, titlet, btimet, etimet)
275 except Exception, inst:
276 print "error occures in search_keyword"
277 print str(type(inst))
279 def type_reserve(typetxt,chtxt,title,bt,et,opt):
280 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
281 etime = et.strftime("%Y-%m-%d %H:%M:%S")
284 if typetxt==recdblist.REC_RESERVE:
285 typetxtfinal=recdblist.REC_FINAL_RESERVE
286 tnow = datetime.datetime.now()
288 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
289 if (dt < 58 * 60 and dt > 30 * 60):
290 bctypet = chdb.chtxtsearch(chtxt)['bctype']
291 chdatat = rec10d.rec10db.select_by_bctype_epg_ch(bctypet)
292 dt1 = datetime.datetime.strptime(chdatat[0][5], "%Y-%m-%d %H:%M:%S")-datetime.datetime.now()
293 dt1 = dt1.days * 24 * 60 * 60 + dt1.seconds
295 recdata = epgdb.searchtime2(title, btime, "5", chtxt)
303 bt = datetime.datetime.strptime(btimen, "%Y-%m-%d %H:%M:%S")
304 et = datetime.datetime.strptime(etimen, "%Y-%m-%d %H:%M:%S")
305 btimen = bt.strftime("%Y-%m-%d %H:%M:%S")
306 etimen = et.strftime("%Y-%m-%d %H:%M:%S")
309 recdb.del_reckey(typetxtnow, title, chtxt, btime)
310 recdb.rec_reckey(typetxtfinal, titlen, chtxtn, btimen, etimen, opt)
311 recdb.rec_reclog(titlen, chtxtn, btimen, etimen, opt, exp, longexp, category)
312 auto_rec.add_key(chtxt, titlen,exp+" "+longexp)
313 auto_rec.add_key("ALL", titlen,exp+" "+longexp)
314 recdblist.printutf8(u"追いかけ機能実行中: "+title+" : "+titlen+" "+btimen+" "+etimen)
315 except Exception, inst:
316 print "Error happened in Oikake DB"
317 print str(type(inst))
320 recdblist.printutf8(u"追いかけ機能エラー:番組データが見付かりません。")
322 if rec10d.rec10db.select_by_bctype_epg_ch(bctypet)[0][6] != "0":
323 rec10d.rec10db.update_status_by_bctype_epg_ch(bctypet, "3")
325 elif (dt <= 30 * 60 and dt > 20 * 60):
326 recdata = epgdb.searchtime2(title, btime, "5", chtxt)
334 bt = datetime.datetime.strptime(btimen, "%Y-%m-%d %H:%M:%S")
335 et = datetime.datetime.strptime(etimen, "%Y-%m-%d %H:%M:%S")
336 btimen = bt.strftime("%Y-%m-%d %H:%M:%S")
337 etimen = et.strftime("%Y-%m-%d %H:%M:%S")
340 recdb.del_reckey(typetxtnow, title, chtxt, btime)
341 recdb.rec_reckey(typetxtfinal, titlen, chtxtn, btimen, etimen, opt)
342 recdb.rec_reclog(titlen, chtxtn, btimen, etimen, opt, exp, longexp, category)
343 auto_rec.add_key(chtxt, titlen,exp+" "+longexp)
344 auto_rec.add_key("ALL", titlen,exp+" "+longexp)
345 recdblist.printutf8(u"追いかけ機能実行中: "+title+" : "+titlen+" "+btimen+" "+etimen)
346 except Exception, inst:
347 print "Error happened in Oikake DB"
352 recdb.del_reckey(typetxtnow, title, chtxt, btime)
353 recdb.rec_reckey(typetxtfinal, title, chtxt, btime, etime, opt)
354 except Exception, inst:
357 def type_final(typetxt,chtxt,title,bt,et,opt):
358 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
359 etime = et.strftime("%Y-%m-%d %H:%M:%S")
363 if typetxt==recdblist.REC_FINAL_RESERVE:
364 typetxting=recdblist.REC_TS_RECORDING
365 typetxtdecque=recdblist.REC_TS_DECODE_QUE
366 tnow = datetime.datetime.now()
368 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
370 if dt < 6 * 60 and dt > 0:
372 recdb.del_reckey(typetxtnow, title, chtxt, btime)
373 testpath=[os.path.join(recpath,title+".ts.b25")]
374 testpath.append(os.path.join(recpath,title+".ts"))
375 testpath.append(os.path.join(recpath,title+".avi"))
376 testpath.append(os.path.join(recpath,title+".mp4"))
377 testpath.append(os.path.join(recpath,title+".log"))
380 if os.path.exists(ti):
382 if re.search("N", opt) or tcheck>0:
385 iff=u"("+configreader.getenv("iff")+u")_"
389 newtitle=newtitle+u"_"+iff+newtime.strftime("%Y-%m-%dT%H-%M-%S")
390 recdb.rec_reckey(typetxting, newtitle, chtxt, btime, etime, opt)
391 recdblist.printutf8(u"録画開始 "+newtitle+" "+btime+" "+etime)
392 tv2avi.timetv2b25(recpath + "/" + newtitle + ".avi", chtxt, btime, etime, opt)
393 recdb.del_reckey(typetxting, newtitle, chtxt, btime)
394 if not re.search("R", opt):
395 tnow = datetime.datetime.now()
396 bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
397 et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
399 bt = tnow + datetime.timedelta(seconds=600)
400 et = et + dt + datetime.timedelta(seconds=600)
401 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
402 etime = et.strftime("%Y-%m-%d %H:%M:%S")
403 recdb.rec_reckey(typetxtdecque, newtitle, chtxt, btime, etime, opt)
406 shutil.copy(os.path.join(recpath,title+".ts.b25"), os.path.join(movepath,title+".ts.b25"))
409 recdb.rec_reckey(recdblist.REC_MOVE_END, newtitle, chtxt, btime, etime, opt)
411 def type_keyword(typetxt,chtxt,title,bt,et,opt,deltatime):
412 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
413 etime = et.strftime("%Y-%m-%d %H:%M:%S")
416 if typetxt==recdblist.REC_KEYWORD:
417 typetxtres=recdblist.REC_RESERVE
418 tnow = datetime.datetime.now()
420 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
423 recdata = epgdb.searchtime2(title, btime, deltatime, chtxt)
432 bt = datetime.datetime.strptime(btimet, "%Y-%m-%d %H:%M:%S")
433 et = datetime.datetime.strptime(etimet, "%Y-%m-%d %H:%M:%S")
434 btimet = bt.strftime("%Y-%m-%d %H:%M:%S")
435 etimet = et.strftime("%Y-%m-%d %H:%M:%S")
437 recdb.del_reckey(typetxtnow, title, chtxt, btime)
438 recdb.rec_reckey(typetxtres, titlet, chtxtt, btimet, etimet, opt)
439 recdb.rec_reclog(titlet, chtxtt, btimet, etimet, opt, exp,longexp,category)
440 auto_rec.add_key(chtxt, titlet,exp+" "+longexp)
441 auto_rec.add_key("ALL", titlet,exp+" "+longexp)
442 recdblist.printutf8(u"key "+title+u" : "+titlet+u" "+btimet+u" "+etimet)
443 #except Exception, inst:
444 # recdblist.printutf8("Error happened in REC_KEYWORD DB")
445 # recdblist.printutf8(type(inst))
446 # recdblist.printutf8(inst)
448 recdblist.printutf8("nothing match")
450 def type_keyword_every_day(type,chtxt,title,bt,et,opt,deltatime,deltaday):
451 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
452 etime = et.strftime("%Y-%m-%d %H:%M:%S")
453 tnow = datetime.datetime.now()
455 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
456 dd = datetime.timedelta(days=int(deltaday))
458 recdb.del_reckey(recdblist.REC_KEYWORD_EVERY_SOME_DAYS, title, chtxt, btime)
461 btxt = bt.strftime("%Y-%m-%d %H:%M:%S")
462 etxt = et.strftime("%Y-%m-%d %H:%M:%S")
463 recdb.everyreserv(title, chtxt, btxt, etxt, deltatime, opt, deltaday)
465 recdb.del_reckey(recdblist.REC_KEYWORD_EVERY_SOME_DAYS, title, chtxt, btime)
468 btxt = bt.strftime("%Y-%m-%d %H:%M:%S")
469 etxt = et.strftime("%Y-%m-%d %H:%M:%S")
470 recdb.everyreserv(title, chtxt, btxt, etxt, deltatime, opt, deltaday)
472 recdata = epgdb.searchtime2(title, btime, deltatime, chtxt)
481 bt = datetime.datetime.strptime(btimet, "%Y-%m-%d %H:%M:%S")
482 et = datetime.datetime.strptime(etimet, "%Y-%m-%d %H:%M:%S")
483 btimet = bt.strftime("%Y-%m-%d %H:%M:%S")
484 etimet = et.strftime("%Y-%m-%d %H:%M:%S")
486 recdb.keyreserv(titlet, chtxtt, btimet, etimet, deltatime, opt)
488 recdb.keyreserv(title, chtxt, btime, etime, deltatime, opt)
489 def type_decode_que(typetxt,chtxt,title,bt,et,opt):
490 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
491 etime = et.strftime("%Y-%m-%d %H:%M:%S")
492 tnow = datetime.datetime.now()
494 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
499 if typetxt== recdblist.REC_TS_DECODE_QUE:
500 typetxting=recdblist.REC_TS_DECODING
501 typetxtmiss=recdblist.REC_MISS_DECODE
502 typetxtencque=recdblist.REC_ENCODE_QUE
503 tnow = datetime.datetime.now()
505 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
507 if status.getB25Decoding() < 2:
508 pin = recpath + "/" + title
509 recdb.del_reckey(typetxtnow, title, chtxt, btime)
510 recdb.rec_reckey(typetxting, title, chtxt, btime, etime, opt)
511 tv2avi.b252ts(pin, chtxt, btime, etime, opt)
512 recdb.del_reckey(typetxting, title, chtxt, btime)
513 if not os.access(recpath + "/" + title + ".ts", os.F_OK):
514 recdb.del_reckey(typetxting, title, chtxt, btime)
515 recdb.rec_reckey(typetxtmiss, title, chtxt, btime, etime, opt)
517 ch=checker.checkB25Decode(recpath + "/" + title + ".ts.b25", recpath + "/" + title + ".ts")
520 os.remove(recpath + "/" + title + ".ts.b25")
521 bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
522 et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
524 if not re.search("D", opt):
525 tnow = datetime.datetime.now()
526 bt = tnow + datetime.timedelta(seconds=600)
528 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
529 etime = et.strftime("%Y-%m-%d %H:%M:%S")
530 recdb.rec_reckey(typetxtencque, title, chtxt, btime, etime, opt)
533 shutil.copy(os.path.join(recpath,title+".ts"), os.path.join(movepath,title+".ts"))
534 except Exception, inst:
535 errtxt="Error happened in type_decode_que in timerec.\n"
536 errtxt=errtxt+"move ts error.\n"
537 errtxt=errtxt+str(type(inst))+"\n"
538 errtxt=errtxt+str(inst)
539 recdblist.addlog(os.path.join(recpath,title+".ts"), errtxt, "timerec-move(Decode) error")
540 recdb.rec_reckey(recdblist.REC_MOVE_END, title, chtxt, btime, etime, opt)
542 recdb.del_reckey(typetxtnow, title, chtxt, btime)
543 bt = bt + datetime.timedelta(seconds=600)
544 et = et + datetime.timedelta(seconds=600)
545 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
546 etime = et.strftime("%Y-%m-%d %H:%M:%S")
547 recdb.rec_reckey(typetxtnow, title, chtxt, btime, etime, opt)
550 def type_encode_que(typetxt,chtxt,title,bt,et,opt):
551 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
552 etime = et.strftime("%Y-%m-%d %H:%M:%S")
553 tnow = datetime.datetime.now()
555 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
559 if typetxt==recdblist.REC_ENCODE_QUE:
560 typetxting=recdblist.REC_ENCODE_LOCAL
561 typetxtfin=recdblist.REC_FIN_LOCAL
563 if status.getEncoding() < int(configreader.getenv("enc_max")):
564 recdb.del_reckey(typetxtnow, title, chtxt, btime)
565 recdb.rec_reckey(typetxting, title, chtxt, btime, etime, opt)
566 recdblist.printutf8(opt)
567 pin = recpath + "/" + title + ".ts"
568 if re.search("d", opt):
569 pin = recpath + "/" + title + ".m2v"
570 if not os.path.exists(pin) or os.path.getsize(pin)<100*1000:
571 paac2 = recpath + "/" + title + "_2.aac"
572 pmp32 = recpath + "/" + title + "_2.mp3"
573 if not os.path.exists(paac2) and not os.path.exists(pmp32):
574 tv2avi.dualaudio2sep(os.path.join(recpath, title+".ts"),"167")
576 elif re.search("5", opt):
577 pin = recpath + "/" + title + ".m2v"
578 if not os.path.exists(pin) or os.path.getsize(pin)<100*1000:
579 paac2 = recpath + "/" + title + "_2.aac"
580 pmp32 = recpath + "/" + title + "_2.mp3"
581 if not os.path.exists(paac2) and not os.path.exists(pmp32):
582 tv2avi.pentaaudio2sep(os.path.join(recpath, title+".ts"))
586 if configreader.getenv("make_mp4")==1:
590 if re.search("m", opt):
592 if re.search("4", opt):
594 recdblist.printutf8(pin)
596 pout = recpath + "/" + title + ".mp4"
597 tv2mp4.ts2mp4(pin, pout, opt)
599 pout = recpath + "/" + title + ".mkv"
600 tv2mkv.ts2mkv(pin, pout, opt)
601 recdb.del_reckey(typetxting, title, chtxt, btime)
603 if re.search("E", opt):
605 if os.path.exists(os.path.join(recpath,title+".mp4")):
606 shutil.copy(os.path.join(recpath,title+".mp4"), os.path.join(movepath,title+".mp4"))
607 elif os.path.exists(os.path.join(recpath,title+".mkv")):
608 shutil.copy(os.path.join(recpath,title+".mkv"), os.path.join(movepath,title+".mkv"))
609 except Exception, inst:
610 errtxt="Error happened in type_encode_que in timerec.\n"
611 errtxt=errtxt+"move mkv/mp4 error.\n"
612 errtxt=errtxt+str(type(inst))+"\n"
613 errtxt=errtxt+str(inst)
614 recdblist.addlog(os.path.join(recpath,title+".ts"), errtxt, "timerec-move(Encode) error")
615 recdb.rec_reckey(recdblist.REC_MOVE_END, title, chtxt, btime, etime, opt)
616 recdb.rec_reckey(typetxtfin, title, chtxt, btime, etime, opt)
619 recdb.del_reckey(typetxtnow, title, chtxt, btime)
620 bt = bt + datetime.timedelta(seconds=600)
621 et = et + datetime.timedelta(seconds=600)
622 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
623 etime = et.strftime("%Y-%m-%d %H:%M:%S")
624 recdb.rec_reckey(typetxtnow, title, chtxt, btime, etime, opt)