3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
19 recordedpath=unicode(configreader.getpath("recorded"),'utf-8')
20 recpath=unicode(configreader.getpath("recpath"),'utf-8')
23 録画フォルダを検索し、各QUEの実行状況を表示する
25 b25list = glob.glob(path + "/*.b25")
27 ##b25f is title.ts.b25 avi is title.avi
28 dir = os.path.split(b25f)[0]
29 title = os.path.split(b25f)[1]
30 title = title.replace(".ts.b25", "")
31 avipath = os.path.join(dir, title + ".avi")
32 mp4path = os.path.join(dir, title + ".mp4")
33 tspath = os.path.join(dir, title + ".ts")
34 b25f = b25f.replace(".ts.b25", ".avi")
36 if os.path.isfile(tspath):##tsファイルが存在している
37 dtime = time.time()-os.path.getmtime(tspath)
40 if os.path.getsize(tspath) > 1 * 1000 * 1000:##最終更新から22分以上経過かつ1MB以上
46 if os.path.isfile(mp4path):##tsファイルが存在している
47 dtime = time.time()-os.path.getmtime(mkvpath)
50 if os.path.getsize(avipath) > 1 * 1000 * 1000:##最終更新から22分以上経過かつ1MB以上
56 recdblist.printutf8(title + ":" + mode)
57 def search_avi(temppath,recpath):
61 avilist = glob.glob(temppath + "/*.avi")
64 ##b25f is title.ts.b25 avi is title.avi
65 dir = os.path.split(avif)[0]
66 title = os.path.split(avif)[1]
67 title = title.replace(".avi", "")
68 avipath = os.path.join(dir, title + ".avi")
69 dtime = time.time()-os.path.getmtime(avipath)
72 if veryfySize(avipath):
75 def search_mkv(temppath,recpath):
79 avilist = glob.glob(temppath + "/*.mkv")
82 ##b25f is title.ts.b25 avi is title.avi
83 dir = os.path.split(avif)[0]
84 title = os.path.split(avif)[1]
85 title = title.replace(".mkv", "")
86 avipath = os.path.join(dir, title + ".mkv")
87 dtime = time.time()-os.path.getmtime(avipath)
90 if veryfySize(avipath):
93 def search_mp4(temppath,recpath):
97 avilist = glob.glob(temppath + "/*.mp4")
100 ##b25f is title.ts.b25 avi is title.avi
101 dir = os.path.split(avif)[0]
102 title = os.path.split(avif)[1]
103 title = title.replace(".mp4", "")
104 avipath = os.path.join(dir, title + ".mp4")
105 dtime = time.time()-os.path.getmtime(avipath)
108 if veryfySize(avipath):
111 def veryfySize(path):
112 #vsize=[297,497,596,1196]#SD 30m 1h 1.5h 2h
113 vsize=[245,275,295,591,830]
114 vsize=vsize+[325,449,560,590,602,690,805,860,1014,1138,1237]
115 vsize=vsize+[261,535,540,616,740]#HD 30m 1h
116 #vsize=vsize+[381,895,447]
119 if os.path.getsize(path)>(size-10)*1024*1024 and os.path.getsize(path)<(size+10)*1024*1024:
121 if os.path.getsize(path)>270*1024*1024:
124 def searchFolder(title,path):
126 titleにマッチするフォルダを探し出す。
128 folderpath=os.listdir(path)
131 for ft in folderpath:
132 fullpath=os.path.join(path.encode('utf-8'), ft)
133 if os.path.isdir(fullpath):
134 lfpath.append(fullpath)
135 ftt=os.listdir(fullpath)
139 folderpath.append(os.path.join(fullpath, ft2))
140 except Exception, inst:
145 lfpath.append(fullpath)
150 if os.path.isdir(dirp):
151 cmpp=os.path.dirname(dirp)
154 cmpp=os.path.basename(dirp)
155 appp=os.path.dirname(dirp)
156 ntitle=getTitle(title)
157 #recdblist.printutf8(cmpp)
158 cmpp=getTitle(os.path.splitext(cmpp)[0])
159 #recdblist.printutf8(cmpp+"\n")
160 p=n_gram.trigram(ntitle,cmpp)
162 ngram.append((p,appp))
163 ngram=list(set(ngram))
167 #recdblist.printutf8(title + ngram[0][1] + " : "+str(ngram[0][0]))
175 rT=re.compile("(.+)_(\d+)\Z")
177 rT2=re.compile("(.+)_(.+)_(\d*)\Z")#_(aichi)_2010-02-06T01:59:00.mkv
179 rT3=re.compile("(.+)_(.+)_\d+-\d+-\d+T\d+:\d+:\d+\Z")#_(aichi)_2010-02-06T01:59:00.mkv
181 rT4=re.compile("(.+)_(.+)_\d+-\d+-\d+T\d+-\d+-\d+\Z")#_(aichi)_2010-02-06T01-59-00.mkv
183 rT5=re.compile("(.+)_(.+)_(.+)\Z")#_(aichi)_2010-02-06T01-59-00.mkv
197 def execMove(title,temppath,recpath):
198 srcpath=os.path.join(temppath,title+".mkv")
199 #desttitle=destName(title, temppath, recpath)
201 sf=searchFolder(title, recpath)
203 destpath=os.path.join(sf,destNameMKV(title, temppath, sf)+".mkv")
204 #os.path.join(sf,desttitle+".mkv")
205 #recdblist.printutf8(srcpath)
206 #recdblist.printutf8(destpath)
207 recdblist.printutf8("moving now..")
208 recdblist.printutf8(srcpath+" : "+destpath)
210 shutil.move(srcpath, destpath)
211 #shutil.copy(srcpath, destpath)
212 delpath=[os.path.join(temppath,title+".ts")]
213 delpath.append(os.path.join(temppath,title+".avi"))
214 delpath.append(os.path.join(temppath,title+".120.avi"))
215 delpath.append(os.path.join(temppath,title+".timecode.txt"))
216 delpath.append(os.path.join(temppath,title+".aac"))
217 delpath.append(os.path.join(temppath,title+".ts.b25"))
218 delpath.append(os.path.join(temppath,title+".ts.tsmix"))
219 delpath.append(os.path.join(temppath,title+".ts.log"))
220 delpath.append(os.path.join(temppath,title+".sa.avi"))
221 delpath.append(os.path.join(temppath,title+".sa.avi.log"))
222 delpath.append(os.path.join(temppath,title+".log"))
229 def execMove_MKV(title,temppath,recpath):
230 srcpath=os.path.join(temppath,title+".mkv")
231 #desttitle=destName(title, temppath, recpath)
233 sf=searchFolder(title, recpath)
235 destpath=os.path.join(sf,destNameMKV(title, temppath, sf)+".mkv")
236 #os.path.join(sf,desttitle+".mkv")
237 #recdblist.printutf8(srcpath)
238 #recdblist.printutf8(destpath)
239 recdblist.printutf8("moving now..")
240 recdblist.printutf8(srcpath+" : "+destpath)
242 shutil.move(srcpath, destpath)
243 #shutil.copy(srcpath, destpath)
244 delpath=[os.path.join(temppath,title+".ts")]
245 delpath.append(os.path.join(temppath,title+".avi"))
246 delpath.append(os.path.join(temppath,title+".120.avi"))
247 delpath.append(os.path.join(temppath,title+".timecode.txt"))
248 delpath.append(os.path.join(temppath,title+".aac"))
249 delpath.append(os.path.join(temppath,title+".ts.b25"))
250 delpath.append(os.path.join(temppath,title+".ts.tsmix"))
251 delpath.append(os.path.join(temppath,title+".ts.log"))
252 delpath.append(os.path.join(temppath,title+".sa.avi"))
253 delpath.append(os.path.join(temppath,title+".sa.avi.log"))
254 delpath.append(os.path.join(temppath,title+".log"))
261 def execMove_MP4(title,temppath,recpath):
262 srcpath=os.path.join(temppath,title+".mp4")
263 #desttitle=destName(title, temppath, recpath)
265 sf=searchFolder(title, recpath)
267 destpath=os.path.join(sf,destNameMP4(title, temppath, sf)+".mp4")
269 #os.path.join(sf,desttitle+".mkv")
270 #recdblist.printutf8(srcpath)
271 #recdblist.printutf8(destpath)
272 recdblist.printutf8("moving now..")
273 recdblist.printutf8(srcpath+" : "+destpath)
275 shutil.move(srcpath, destpath)
276 #shutil.copy(srcpath, destpath)
277 delpath=[os.path.join(temppath,title+".ts")]
278 delpath.append(os.path.join(temppath,title+".avi"))
279 delpath.append(os.path.join(temppath,title+".120.avi"))
280 delpath.append(os.path.join(temppath,title+".timecode.txt"))
281 delpath.append(os.path.join(temppath,title+".aac"))
282 delpath.append(os.path.join(temppath,title+".ts.b25"))
283 delpath.append(os.path.join(temppath,title+".ts.tsmix"))
284 delpath.append(os.path.join(temppath,title+".ts.log"))
285 delpath.append(os.path.join(temppath,title+".sa.avi"))
286 delpath.append(os.path.join(temppath,title+".sa.avi.log"))
287 delpath.append(os.path.join(temppath,title+".log"))
294 def destName(title,temppath,recpath):
296 titleから移動先に作るべきファイル名を作り出す。
298 dstpath=os.path.join(recpath,title+".avi")
299 srcpath=os.path.join(temppath,title+".avi")
300 if os.path.exists(dstpath):
301 gmtime=time.gmtime(os.path.getmtime(srcpath))
302 times=time.strftime("%y%m%d",gmtime)
303 title=title+"_"+times
305 def destNameMKV(title,temppath,recpath):
307 titleから移動先に作るべきファイル名を作り出す。
309 dstpath=os.path.join(recpath,title+".mkv")
310 srcpath=os.path.join(temppath,title+".mkv")
311 if os.path.exists(dstpath):
312 gmtime=time.gmtime(os.path.getmtime(srcpath))
313 times=time.strftime("%y%m%d",gmtime)
314 title=title+"_"+times
316 def destNameMP4(title,temppath,recpath):
318 titleから移動先に作るべきファイル名を作り出す。
320 dstpath=os.path.join(recpath,title+".mp4")
321 srcpath=os.path.join(temppath,title+".mp4")
322 if os.path.exists(dstpath):
323 gmtime=time.gmtime(os.path.getmtime(srcpath))
324 times=time.strftime("%y%m%d",gmtime)
325 title=title+"_"+times
327 def detName(path,title):
329 type A ---title#<number>
330 type B ---title#<number>subtitle
331 type C ---title subtitle
332 type D ---title(without number)
334 #if re.match("#\d\s[0,10]\z|#[0-9]\s[0,10]\z", title)
335 # recdblist.printutf8("typeA")
336 #elif re.match("#\d\s[0,10].|#[0-9]\s[0,10].", title)
337 # recdblist.printutf8("typeB")
339 def detNameType(title):
341 type A ---title#<number>
342 type B ---title#<number>subtitle
343 type C ---title subtitle
344 type D ---title(without number)
346 recdblist.printutf8(title)
347 #rA=re.compile(".+(?P<title>)#\d(?P<num>)\s[0,10]\z")
348 rA=re.compile("(.+)#(\d*)\s*\Z")
350 rB=re.compile("(.+)#(\d*)\s*(\D*)")
353 recdblist.printutf8("typeA")
354 recdblist.printutf8("title="+tA.group(1))
355 recdblist.printutf8("num="+tA.group(2))
357 recdblist.printutf8("typeB")
358 recdblist.printutf8("title="+tB.group(1))
359 recdblist.printutf8("num="+tB.group(2))
360 recdblist.printutf8("subtitle="+tB.group(3))
361 if __name__ == "__main__":
362 usage="usage: %prog read -h"
363 version="%prog 0.9.4"
364 parser=optparse.OptionParser(usage=usage,version=version)
365 parser.add_option("-s","--search",action="store",type="string",dest="ltitle",default="",metavar="TITLE",help="test to search where the title should be contained(test for -e)")
366 parser.add_option("-A","--Auto",action="store_true",dest="auto",default=False,help="auto classifying mode(not a test)")
367 parser.add_option("-e","--exec",action="store",type="string",dest="etitle",default="",metavar="TITLE",help="exec move(not a test)")
368 parser.add_option("-l","--list",action="store_true",dest="list",default=False,help="File listing mode(test for -A)")
369 parser.add_option("-t","--tssearch",action="store_true",dest="tssearch",default=False,help="Ts move auto search")
370 parser.add_option("-T","--Tsmove",action="store_true",dest="tsmove",default=False,help="Ts auto move.")
371 (opts,args)=parser.parse_args(sys.argv)
373 ltitle=unicode(opts.ltitle,'utf-8')
374 sf=searchFolder(ltitle, recordedpath)
376 recdblist.printutf8(os.path.join(sf, auto_move.get_move_dest_path(ltitle,recpath,sf,".mkv")+".mkv"))
378 recdblist.printutf8(os.path.join(sf, auto_move.get_move_dest_path(ltitle,recpath,sf,".mp4")+".mp4"))
380 sa=auto_move.search_file(recpath, recordedpath, ".mkv")
382 recdblist.printutf8(u"自動推測実行中-MKV")
383 sf=searchFolder(t,recordedpath)
385 recdblist.printutf8(u"移動先")
386 recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mkv")+".mkv"))
387 recdblist.printutf8(u"実行中")
388 auto_move.execMove(t, recpath, recordedpath,".mkv",1)
390 recdblist.printutf8(t+" can't find matching folder")
391 sa=auto_move.search_file(recpath, recordedpath, ".mp4")
393 recdblist.printutf8(u"自動推測実行中-MP4")
394 sf=searchFolder(t,recordedpath)
396 recdblist.printutf8(u"移動先")
397 recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mp4")+".mp4"))
398 recdblist.printutf8(u"実行中")
399 auto_move.execMove(t, recpath, recordedpath,".mp4",1)
401 recdblist.printutf8(t+" can't find matching folder")
403 sa=auto_move.search_file(recpath, recordedpath,".mkv")
405 sf=searchFolder(t,recordedpath)
407 recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mkv")+".mkv"))
408 sa=auto_move.search_file(recpath, recordedpath,".mp4")
410 sf=searchFolder(t,recordedpath)
412 recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mp4")+".mp4"))
413 elif opts.etitle!="":
414 if os.path.exists(os.path.join(recpath, etitle+".mkv")):
415 auto_move.execMove(etitle,recpath, recordedpath,".mkv",1)
416 elif os.path.exists(os.path.join(recpath, etitle+".mp4")):
417 auto_move.execMove(etitle,recpath, recordedpath,".mp4",1)
418 srcpath=os.path.join(temppath,title+".mkv")
419 etitle=unicode(opts.etitle,'utf-8')
421 execMove_MKV(etitle,recpath, recordedpath)
423 execMove_MP4(etitle,recpath, recordedpath)
427 tsmovepath=unicode(configreader.getpath("ts_movepath"),'utf-8',700)
431 sa=auto_move.search_file(recpath, recordedpath,".ts")
433 sf=searchFolder(t,recordedpath)
435 recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".ts")+".ts"))
436 sa=auto_move.search_file(recpath, recordedpath,".ts")
438 recdblist.printutf8(u"ts_movepathが設定されていません。/etc/rec10.confを設定してください。")
442 tsmovepath=unicode(configreader.getpath("ts_movepath"),'utf-8',700)
446 sa=auto_move.search_file(recpath, recordedpath,".ts")
448 sf=searchFolder(t,recordedpath)
450 recdblist.printutf8(u"移動先")
451 recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".ts")+".ts"))
452 recdblist.printutf8(u"実行中")
453 auto_move.execMove(t, recpath, recordedpath,".ts",0)
454 sa=auto_move.search_file(recpath, recordedpath,".ts")
456 recdblist.printutf8(u"ts_movepathが設定されていません。/etc/rec10.confを設定してください。")