3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 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+[458,916]#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:
121 def searchFolder(title,path):
123 titleにマッチするフォルダを探し出す。
125 folderpath=os.listdir(path)
128 for ft in folderpath:
129 fullpath=os.path.join(path.encode('utf-8'), ft)
130 if os.path.isdir(fullpath):
131 lfpath.append(fullpath)
132 ftt=os.listdir(fullpath)
136 folderpath.append(os.path.join(fullpath, ft2))
137 except Exception, inst:
142 lfpath.append(fullpath)
147 if os.path.isdir(dirp):
148 cmpp=os.path.dirname(dirp)
151 cmpp=os.path.basename(dirp)
152 appp=os.path.dirname(dirp)
153 rT=re.compile("(.+)_(\d*)\Z")
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]))
174 def execMove(title,temppath,recpath):
175 srcpath=os.path.join(temppath,title+".mp4")
176 #desttitle=destName(title, temppath, recpath)
178 sf=searchFolder(title, recpath)
180 destpath=os.path.join(sf,destNameMP4(title, temppath, sf)+".mp4")
181 #os.path.join(sf,desttitle+".mkv")
182 #recdblist.printutf8(srcpath)
183 #recdblist.printutf8(destpath)
184 recdblist.printutf8("moving now..")
185 recdblist.printutf8(srcpath+" : "+destpath)
186 shutil.move(srcpath, destpath)
187 #shutil.copy(srcpath, destpath)
188 delpath=[os.path.join(temppath,title+".ts")]
189 delpath.append(os.path.join(temppath,title+".avi"))
190 delpath.append(os.path.join(temppath,title+".ts.b25"))
191 delpath.append(os.path.join(temppath,title+".ts.tsmix"))
192 delpath.append(os.path.join(temppath,title+".ts.log"))
193 delpath.append(os.path.join(temppath,title+".sa.avi"))
194 delpath.append(os.path.join(temppath,title+".sa.avi.log"))
195 delpath.append(os.path.join(temppath,title+".log"))
202 def destName(title,temppath,recpath):
204 titleから移動先に作るべきファイル名を作り出す。
206 dstpath=os.path.join(recpath,title+".avi")
207 srcpath=os.path.join(temppath,title+".avi")
208 if os.path.exists(dstpath):
209 gmtime=time.gmtime(os.path.getmtime(srcpath))
210 times=time.strftime("%y%m%d",gmtime)
211 title=title+"_"+times
213 def destNameMKV(title,temppath,recpath):
215 titleから移動先に作るべきファイル名を作り出す。
217 dstpath=os.path.join(recpath,title+".mkv")
218 srcpath=os.path.join(temppath,title+".mkv")
219 if os.path.exists(dstpath):
220 gmtime=time.gmtime(os.path.getmtime(srcpath))
221 times=time.strftime("%y%m%d",gmtime)
222 title=title+"_"+times
224 def destNameMP4(title,temppath,recpath):
226 titleから移動先に作るべきファイル名を作り出す。
228 dstpath=os.path.join(recpath,title+".mp4")
229 srcpath=os.path.join(temppath,title+".mp4")
230 if os.path.exists(dstpath):
231 gmtime=time.gmtime(os.path.getmtime(srcpath))
232 times=time.strftime("%y%m%d",gmtime)
233 title=title+"_"+times
235 def detName(path,title):
237 type A ---title#<number>
238 type B ---title#<number>subtitle
239 type C ---title subtitle
240 type D ---title(without number)
242 #if re.match("#\d\s[0,10]\z|#[0-9]\s[0,10]\z", title)
243 # recdblist.printutf8("typeA")
244 #elif re.match("#\d\s[0,10].|#[0-9]\s[0,10].", title)
245 # recdblist.printutf8("typeB")
247 def detNameType(title):
249 type A ---title#<number>
250 type B ---title#<number>subtitle
251 type C ---title subtitle
252 type D ---title(without number)
254 recdblist.printutf8(title)
255 #rA=re.compile(".+(?P<title>)#\d(?P<num>)\s[0,10]\z")
256 rA=re.compile("(.+)#(\d*)\s*\Z")
258 rB=re.compile("(.+)#(\d*)\s*(\D*)")
261 recdblist.printutf8("typeA")
262 recdblist.printutf8("title="+tA.group(1))
263 recdblist.printutf8("num="+tA.group(2))
265 recdblist.printutf8("typeB")
266 recdblist.printutf8("title="+tB.group(1))
267 recdblist.printutf8("num="+tB.group(2))
268 recdblist.printutf8("subtitle="+tB.group(3))
269 if __name__ == "__main__":
270 usage="usage: %prog read -h"
271 version="%prog 0.7.0beta1"
272 parser=optparse.OptionParser(usage=usage,version=version)
273 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)")
274 parser.add_option("-A","--Auto",action="store_true",dest="auto",default=False,help="auto classifying mode(not a test)")
275 parser.add_option("-e","--exec",action="store",type="string",dest="etitle",default="",metavar="TITLE",help="exec move(not a test)")
276 parser.add_option("-l","--list",action="store_true",dest="list",default=False,help="File listing mode(test for -a)")
277 (opts,args)=parser.parse_args(sys.argv)
280 ltitle=unicode(opts.ltitle,'utf-8')
281 sf=searchFolder(ltitle, recordedpath)
282 recdblist.printutf8(os.path.join(sf, destNameMP4(ltitle,recpath,sf)+".mp4"))
283 #print os.path.join(sf, destNameMP4(ltitle,recpath,sf)+".mp4")
285 sa=search_mp4(recpath, recordedpath)
287 recdblist.printutf8(u"自動推測実行中")
288 sf=searchFolder(t,recordedpath)
290 recdblist.printutf8(u"移動先")
291 recdblist.printutf8(t+" : "+os.path.join(sf, destNameMP4(t, recpath,sf)+".mp4"))
292 recdblist.printutf8(u"実行中")
293 execMove(t, recpath, recordedpath)
295 recdblist.printutf8(t+" can't find matching folder.")
297 sa=search_mp4(recpath, recordedpath)
299 sf=searchFolder(t,recordedpath)
301 recdblist.printutf8(t+u" : "+os.path.join(sf, destNameMP4(t, recpath,sf)+".mp4"))
302 elif opts.etitle!="":
303 etitle=unicode(opts.etitle,'utf-8')
304 execMove(etitle,recpath, recordedpath)