3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
18 recordedpath=unicode(configreader.getpath("recorded"),'utf-8')
19 recpath=unicode(configreader.getpath("recpath"),'utf-8')
22 録画フォルダを検索し、各QUEの実行状況を表示する
24 b25list = glob.glob(path + "/*.b25")
26 ##b25f is title.ts.b25 avi is title.avi
27 dir = os.path.split(b25f)[0]
28 title = os.path.split(b25f)[1]
29 title = title.replace(".ts.b25", "")
30 avipath = os.path.join(dir, title + ".avi")
31 mp4path = os.path.join(dir, title + ".mp4")
32 tspath = os.path.join(dir, title + ".ts")
33 b25f = b25f.replace(".ts.b25", ".avi")
35 if os.path.isfile(tspath):##tsファイルが存在している
36 dtime = time.time()-os.path.getmtime(tspath)
39 if os.path.getsize(tspath) > 1 * 1000 * 1000:##最終更新から22分以上経過かつ1MB以上
45 if os.path.isfile(mp4path):##tsファイルが存在している
46 dtime = time.time()-os.path.getmtime(mkvpath)
49 if os.path.getsize(avipath) > 1 * 1000 * 1000:##最終更新から22分以上経過かつ1MB以上
55 recdblist.printutf8(title + ":" + mode)
56 def search_avi(temppath,recpath):
60 avilist = glob.glob(temppath + "/*.avi")
63 ##b25f is title.ts.b25 avi is title.avi
64 dir = os.path.split(avif)[0]
65 title = os.path.split(avif)[1]
66 title = title.replace(".avi", "")
67 avipath = os.path.join(dir, title + ".avi")
68 dtime = time.time()-os.path.getmtime(avipath)
71 if veryfySize(avipath):
74 def search_mkv(temppath,recpath):
78 avilist = glob.glob(temppath + "/*.mkv")
81 ##b25f is title.ts.b25 avi is title.avi
82 dir = os.path.split(avif)[0]
83 title = os.path.split(avif)[1]
84 title = title.replace(".mkv", "")
85 avipath = os.path.join(dir, title + ".mkv")
86 dtime = time.time()-os.path.getmtime(avipath)
89 if veryfySize(avipath):
92 def search_mp4(temppath,recpath):
96 avilist = glob.glob(temppath + "/*.mp4")
99 ##b25f is title.ts.b25 avi is title.avi
100 dir = os.path.split(avif)[0]
101 title = os.path.split(avif)[1]
102 title = title.replace(".mp4", "")
103 avipath = os.path.join(dir, title + ".mp4")
104 dtime = time.time()-os.path.getmtime(avipath)
107 if veryfySize(avipath):
110 def veryfySize(path):
111 #vsize=[297,497,596,1196]#SD 30m 1h 1.5h 2h
112 vsize=[245,275,295,591,830]
113 vsize=vsize+[325,449,560,590,602,690,805,860,1014,1138,1237]
114 vsize=vsize+[261,535,540,616,740]#HD 30m 1h
115 #vsize=vsize+[381,895,447]
118 if os.path.getsize(path)>(size-10)*1024*1024 and os.path.getsize(path)<(size+10)*1024*1024:
120 if os.path.getsize(path)>270*1024*1024:
123 def searchFolder(title,path):
125 titleにマッチするフォルダを探し出す。
127 folderpath=os.listdir(path)
130 for ft in folderpath:
131 fullpath=os.path.join(path.encode('utf-8'), ft)
132 if os.path.isdir(fullpath):
133 lfpath.append(fullpath)
134 ftt=os.listdir(fullpath)
138 folderpath.append(os.path.join(fullpath, ft2))
139 except Exception, inst:
144 lfpath.append(fullpath)
149 if os.path.isdir(dirp):
150 cmpp=os.path.dirname(dirp)
153 cmpp=os.path.basename(dirp)
154 appp=os.path.dirname(dirp)
155 ntitle=getTitle(title)
156 #recdblist.printutf8(cmpp)
157 cmpp=getTitle(os.path.splitext(cmpp)[0])
158 #recdblist.printutf8(cmpp+"\n")
159 p=n_gram.trigram(ntitle,cmpp)
161 ngram.append((p,appp))
162 ngram=list(set(ngram))
166 #recdblist.printutf8(title + ngram[0][1] + " : "+str(ngram[0][0]))
174 rT=re.compile("(.+)_(\d+)\Z")
176 rT2=re.compile("(.+)_(.+)_(\d*)\Z")#_(aichi)_2010-02-06T01:59:00.mkv
178 rT3=re.compile("(.+)_(.+)_\d+-\d+-\d+T\d+:\d+:\d+\Z")#_(aichi)_2010-02-06T01:59:00.mkv
180 rT4=re.compile("(.+)_(.+)_\d+-\d+-\d+T\d+-\d+-\d+\Z")#_(aichi)_2010-02-06T01-59-00.mkv
182 rT5=re.compile("(.+)_(.+)_(.+)\Z")#_(aichi)_2010-02-06T01-59-00.mkv
196 def execMove(title,temppath,recpath):
197 srcpath=os.path.join(temppath,title+".mkv")
198 #desttitle=destName(title, temppath, recpath)
200 sf=searchFolder(title, recpath)
202 destpath=os.path.join(sf,destNameMKV(title, temppath, sf)+".mkv")
203 #os.path.join(sf,desttitle+".mkv")
204 #recdblist.printutf8(srcpath)
205 #recdblist.printutf8(destpath)
206 recdblist.printutf8("moving now..")
207 recdblist.printutf8(srcpath+" : "+destpath)
209 shutil.move(srcpath, destpath)
210 #shutil.copy(srcpath, destpath)
211 delpath=[os.path.join(temppath,title+".ts")]
212 delpath.append(os.path.join(temppath,title+".avi"))
213 delpath.append(os.path.join(temppath,title+".120.avi"))
214 delpath.append(os.path.join(temppath,title+".timecode.txt"))
215 delpath.append(os.path.join(temppath,title+".aac"))
216 delpath.append(os.path.join(temppath,title+".ts.b25"))
217 delpath.append(os.path.join(temppath,title+".ts.tsmix"))
218 delpath.append(os.path.join(temppath,title+".ts.log"))
219 delpath.append(os.path.join(temppath,title+".sa.avi"))
220 delpath.append(os.path.join(temppath,title+".sa.avi.log"))
221 delpath.append(os.path.join(temppath,title+".log"))
228 def execMove_MKV(title,temppath,recpath):
229 srcpath=os.path.join(temppath,title+".mkv")
230 #desttitle=destName(title, temppath, recpath)
232 sf=searchFolder(title, recpath)
234 destpath=os.path.join(sf,destNameMKV(title, temppath, sf)+".mkv")
235 #os.path.join(sf,desttitle+".mkv")
236 #recdblist.printutf8(srcpath)
237 #recdblist.printutf8(destpath)
238 recdblist.printutf8("moving now..")
239 recdblist.printutf8(srcpath+" : "+destpath)
241 shutil.move(srcpath, destpath)
242 #shutil.copy(srcpath, destpath)
243 delpath=[os.path.join(temppath,title+".ts")]
244 delpath.append(os.path.join(temppath,title+".avi"))
245 delpath.append(os.path.join(temppath,title+".120.avi"))
246 delpath.append(os.path.join(temppath,title+".timecode.txt"))
247 delpath.append(os.path.join(temppath,title+".aac"))
248 delpath.append(os.path.join(temppath,title+".ts.b25"))
249 delpath.append(os.path.join(temppath,title+".ts.tsmix"))
250 delpath.append(os.path.join(temppath,title+".ts.log"))
251 delpath.append(os.path.join(temppath,title+".sa.avi"))
252 delpath.append(os.path.join(temppath,title+".sa.avi.log"))
253 delpath.append(os.path.join(temppath,title+".log"))
260 def execMove_MP4(title,temppath,recpath):
261 srcpath=os.path.join(temppath,title+".mp4")
262 #desttitle=destName(title, temppath, recpath)
264 sf=searchFolder(title, recpath)
266 destpath=os.path.join(sf,destNameMP4(title, temppath, sf)+".mp4")
268 #os.path.join(sf,desttitle+".mkv")
269 #recdblist.printutf8(srcpath)
270 #recdblist.printutf8(destpath)
271 recdblist.printutf8("moving now..")
272 recdblist.printutf8(srcpath+" : "+destpath)
274 shutil.move(srcpath, destpath)
275 #shutil.copy(srcpath, destpath)
276 delpath=[os.path.join(temppath,title+".ts")]
277 delpath.append(os.path.join(temppath,title+".avi"))
278 delpath.append(os.path.join(temppath,title+".120.avi"))
279 delpath.append(os.path.join(temppath,title+".timecode.txt"))
280 delpath.append(os.path.join(temppath,title+".aac"))
281 delpath.append(os.path.join(temppath,title+".ts.b25"))
282 delpath.append(os.path.join(temppath,title+".ts.tsmix"))
283 delpath.append(os.path.join(temppath,title+".ts.log"))
284 delpath.append(os.path.join(temppath,title+".sa.avi"))
285 delpath.append(os.path.join(temppath,title+".sa.avi.log"))
286 delpath.append(os.path.join(temppath,title+".log"))
293 def destName(title,temppath,recpath):
295 titleから移動先に作るべきファイル名を作り出す。
297 dstpath=os.path.join(recpath,title+".avi")
298 srcpath=os.path.join(temppath,title+".avi")
299 if os.path.exists(dstpath):
300 gmtime=time.gmtime(os.path.getmtime(srcpath))
301 times=time.strftime("%y%m%d",gmtime)
302 title=title+"_"+times
304 def destNameMKV(title,temppath,recpath):
306 titleから移動先に作るべきファイル名を作り出す。
308 dstpath=os.path.join(recpath,title+".mkv")
309 srcpath=os.path.join(temppath,title+".mkv")
310 if os.path.exists(dstpath):
311 gmtime=time.gmtime(os.path.getmtime(srcpath))
312 times=time.strftime("%y%m%d",gmtime)
313 title=title+"_"+times
315 def destNameMP4(title,temppath,recpath):
317 titleから移動先に作るべきファイル名を作り出す。
319 dstpath=os.path.join(recpath,title+".mp4")
320 srcpath=os.path.join(temppath,title+".mp4")
321 if os.path.exists(dstpath):
322 gmtime=time.gmtime(os.path.getmtime(srcpath))
323 times=time.strftime("%y%m%d",gmtime)
324 title=title+"_"+times
326 def detName(path,title):
328 type A ---title#<number>
329 type B ---title#<number>subtitle
330 type C ---title subtitle
331 type D ---title(without number)
333 #if re.match("#\d\s[0,10]\z|#[0-9]\s[0,10]\z", title)
334 # recdblist.printutf8("typeA")
335 #elif re.match("#\d\s[0,10].|#[0-9]\s[0,10].", title)
336 # recdblist.printutf8("typeB")
338 def detNameType(title):
340 type A ---title#<number>
341 type B ---title#<number>subtitle
342 type C ---title subtitle
343 type D ---title(without number)
345 recdblist.printutf8(title)
346 #rA=re.compile(".+(?P<title>)#\d(?P<num>)\s[0,10]\z")
347 rA=re.compile("(.+)#(\d*)\s*\Z")
349 rB=re.compile("(.+)#(\d*)\s*(\D*)")
352 recdblist.printutf8("typeA")
353 recdblist.printutf8("title="+tA.group(1))
354 recdblist.printutf8("num="+tA.group(2))
356 recdblist.printutf8("typeB")
357 recdblist.printutf8("title="+tB.group(1))
358 recdblist.printutf8("num="+tB.group(2))
359 recdblist.printutf8("subtitle="+tB.group(3))
360 if __name__ == "__main__":
361 usage="usage: %prog read -h"
362 version="%prog 0.9.3"
363 parser=optparse.OptionParser(usage=usage,version=version)
364 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)")
365 parser.add_option("-A","--Auto",action="store_true",dest="auto",default=False,help="auto classifying mode(not a test)")
366 parser.add_option("-e","--exec",action="store",type="string",dest="etitle",default="",metavar="TITLE",help="exec move(not a test)")
367 parser.add_option("-l","--list",action="store_true",dest="list",default=False,help="File listing mode(test for -a)")
368 (opts,args)=parser.parse_args(sys.argv)
371 ltitle=unicode(opts.ltitle,'utf-8')
372 sf=searchFolder(ltitle, recordedpath)
373 print "###MKV List###"
374 recdblist.printutf8(os.path.join(sf, destNameMKV(ltitle,recpath,sf)+".mkv"))
375 print "###MP4 List###"
376 recdblist.printutf8(os.path.join(sf, destNameMP4(ltitle,recpath,sf)+".mp4"))
377 #print os.path.join(sf, destNameMP4(ltitle,recpath,sf)+".mp4")
379 sa=search_mkv(recpath, recordedpath)
381 recdblist.printutf8(u"自動推測実行中")
382 sf=searchFolder(t,recordedpath)
384 recdblist.printutf8(u"移動先")
385 recdblist.printutf8(t+" : "+os.path.join(sf, destNameMKV(t, recpath,sf)+".mkv"))
386 recdblist.printutf8(u"実行中")
387 execMove_MKV(t, recpath, recordedpath)
389 recdblist.printutf8(t+" can't find matching folder")
390 sa=search_mp4(recpath, recordedpath)
392 recdblist.printutf8(u"自動推測実行中")
393 sf=searchFolder(t,recordedpath)
395 recdblist.printutf8(u"移動先")
396 recdblist.printutf8(t+" : "+os.path.join(sf, destNameMP4(t, recpath,sf)+".mp4"))
397 recdblist.printutf8(u"実行中")
398 execMove_MP4(t, recpath, recordedpath)
400 recdblist.printutf8(t+" can't find matching folder.")
402 sa=search_mkv(recpath, recordedpath)
404 sf=searchFolder(t,recordedpath)
406 recdblist.printutf8(t+u" : "+os.path.join(sf, destNameMKV(t, recpath,sf)+".mkv"))
407 sa=search_mp4(recpath, recordedpath)
409 sf=searchFolder(t,recordedpath)
411 recdblist.printutf8(t+u" : "+os.path.join(sf, destNameMP4(t, recpath,sf)+".mp4"))
412 elif opts.etitle!="":
413 etitle=unicode(opts.etitle,'utf-8')
415 execMove_MKV(etitle,recpath, recordedpath)
417 execMove_MP4(etitle,recpath, recordedpath)