3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
7 __date__ ="$2009/09/14 17:38:25$"
20 recordedpath=unicode(configreader.getpath("recorded"),'utf-8')
21 recpath=unicode(configreader.getpath("recpath"),'utf-8')
24 録画フォルダを検索し、各QUEの実行状況を表示する
26 b25list = glob.glob(path + "/*.b25")
28 ##b25f is title.ts.b25 avi is title.avi
29 dir = os.path.split(b25f)[0]
30 title = os.path.split(b25f)[1]
31 title = title.replace(".ts.b25", "")
32 avipath = os.path.join(dir, title + ".avi")
33 mp4path = os.path.join(dir, title + ".mp4")
34 tspath = os.path.join(dir, title + ".ts")
35 b25f = b25f.replace(".ts.b25", ".avi")
37 if os.path.isfile(tspath):##tsファイルが存在している
38 dtime = time.time()-os.path.getmtime(tspath)
41 if os.path.getsize(tspath) > 1 * 1000 * 1000:##最終更新から22分以上経過かつ1MB以上
47 if os.path.isfile(mp4path):##tsファイルが存在している
48 dtime = time.time()-os.path.getmtime(mkvpath)
51 if os.path.getsize(avipath) > 1 * 1000 * 1000:##最終更新から22分以上経過かつ1MB以上
57 recdblist.printutf8(title + ":" + mode)
58 def search_avi(temppath,recpath):
62 avilist = glob.glob(temppath + "/*.avi")
65 ##b25f is title.ts.b25 avi is title.avi
66 dir = os.path.split(avif)[0]
67 title = os.path.split(avif)[1]
68 title = title.replace(".avi", "")
69 avipath = os.path.join(dir, title + ".avi")
70 dtime = time.time()-os.path.getmtime(avipath)
73 if veryfySize(avipath):
76 def search_mkv(temppath,recpath):
80 avilist = glob.glob(temppath + "/*.mkv")
83 ##b25f is title.ts.b25 avi is title.avi
84 dir = os.path.split(avif)[0]
85 title = os.path.split(avif)[1]
86 title = title.replace(".mkv", "")
87 avipath = os.path.join(dir, title + ".mkv")
88 dtime = time.time()-os.path.getmtime(avipath)
91 if veryfySize(avipath):
94 def search_mp4(temppath,recpath):
98 avilist = glob.glob(temppath + "/*.mp4")
101 ##b25f is title.ts.b25 avi is title.avi
102 dir = os.path.split(avif)[0]
103 title = os.path.split(avif)[1]
104 title = title.replace(".mp4", "")
105 avipath = os.path.join(dir, title + ".mp4")
106 dtime = time.time()-os.path.getmtime(avipath)
109 if veryfySize(avipath):
112 def veryfySize(path):
113 #vsize=[297,497,596,1196]#SD 30m 1h 1.5h 2h
114 vsize=[245,275,295,591,830]
115 vsize=vsize+[325,449,560,590,602,690,805,860,1014,1138,1237]
116 #vsize=vsize+[458,916]#HD 30m 1h
117 #vsize=vsize+[381,895,447]
120 if os.path.getsize(path)>(size-10)*1024*1024 and os.path.getsize(path)<(size+10)*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 rT=re.compile("(.+)_(\d*)\Z")
162 p=n_gram.trigram(ntitle,cmpp)
164 ngram.append((p,appp))
165 ngram=list(set(ngram))
169 #recdblist.printutf8(title + ngram[0][1] + " : "+str(ngram[0][0]))
176 def execMove(title,temppath,recpath):
177 srcpath=os.path.join(temppath,title+".mp4")
178 #desttitle=destName(title, temppath, recpath)
180 sf=searchFolder(title, recpath)
182 destpath=os.path.join(sf,destNameMP4(title, temppath, sf)+".mp4")
183 #os.path.join(sf,desttitle+".mkv")
184 #recdblist.printutf8(srcpath)
185 #recdblist.printutf8(destpath)
186 recdblist.printutf8("moving now..")
187 recdblist.printutf8(srcpath+" : "+destpath)
188 shutil.move(srcpath, destpath)
189 #shutil.copy(srcpath, destpath)
190 delpath=[os.path.join(temppath,title+".ts")]
191 delpath.append(os.path.join(temppath,title+".avi"))
192 delpath.append(os.path.join(temppath,title+".ts.b25"))
193 delpath.append(os.path.join(temppath,title+".ts.tsmix"))
194 delpath.append(os.path.join(temppath,title+".ts.log"))
195 delpath.append(os.path.join(temppath,title+".sa.avi"))
196 delpath.append(os.path.join(temppath,title+".sa.avi.log"))
197 delpath.append(os.path.join(temppath,title+".log"))
204 def destName(title,temppath,recpath):
206 titleから移動先に作るべきファイル名を作り出す。
208 dstpath=os.path.join(recpath,title+".avi")
209 srcpath=os.path.join(temppath,title+".avi")
210 if os.path.exists(dstpath):
211 gmtime=time.gmtime(os.path.getmtime(srcpath))
212 times=time.strftime("%y%m%d",gmtime)
213 title=title+"_"+times
215 def destNameMKV(title,temppath,recpath):
217 titleから移動先に作るべきファイル名を作り出す。
219 dstpath=os.path.join(recpath,title+".mkv")
220 srcpath=os.path.join(temppath,title+".mkv")
221 if os.path.exists(dstpath):
222 gmtime=time.gmtime(os.path.getmtime(srcpath))
223 times=time.strftime("%y%m%d",gmtime)
224 title=title+"_"+times
226 def destNameMP4(title,temppath,recpath):
228 titleから移動先に作るべきファイル名を作り出す。
230 dstpath=os.path.join(recpath,title+".mp4")
231 srcpath=os.path.join(temppath,title+".mp4")
232 if os.path.exists(dstpath):
233 gmtime=time.gmtime(os.path.getmtime(srcpath))
234 times=time.strftime("%y%m%d",gmtime)
235 title=title+"_"+times
237 def detName(path,title):
239 type A ---title#<number>
240 type B ---title#<number>subtitle
241 type C ---title subtitle
242 type D ---title(without number)
244 #if re.match("#\d\s[0,10]\z|#[0-9]\s[0,10]\z", title)
245 # recdblist.printutf8("typeA")
246 #elif re.match("#\d\s[0,10].|#[0-9]\s[0,10].", title)
247 # recdblist.printutf8("typeB")
249 def detNameType(title):
251 type A ---title#<number>
252 type B ---title#<number>subtitle
253 type C ---title subtitle
254 type D ---title(without number)
256 recdblist.printutf8(title)
257 #rA=re.compile(".+(?P<title>)#\d(?P<num>)\s[0,10]\z")
258 rA=re.compile("(.+)#(\d*)\s*\Z")
260 rB=re.compile("(.+)#(\d*)\s*(\D*)")
263 recdblist.printutf8("typeA")
264 recdblist.printutf8("title="+tA.group(1))
265 recdblist.printutf8("num="+tA.group(2))
267 recdblist.printutf8("typeB")
268 recdblist.printutf8("title="+tB.group(1))
269 recdblist.printutf8("num="+tB.group(2))
270 recdblist.printutf8("subtitle="+tB.group(3))
271 if __name__ == "__main__":
272 usage="usage: %prog read -h"
273 version="%prog 0.7.0beta1"
274 parser=optparse.OptionParser(usage=usage,version=version)
275 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)")
276 parser.add_option("-A","--Auto",action="store_true",dest="auto",default=False,help="auto classifying mode(not a test)")
277 parser.add_option("-e","--exec",action="store",type="string",dest="etitle",default="",metavar="TITLE",help="exec move(not a test)")
278 parser.add_option("-l","--list",action="store_true",dest="list",default=False,help="File listing mode(test for -a)")
279 (opts,args)=parser.parse_args(sys.argv)
282 ltitle=unicode(opts.ltitle,'utf-8')
283 sf=searchFolder(ltitle, recordedpath)
284 recdblist.printutf8(os.path.join(sf, destNameMP4(ltitle,recpath,sf)+".mp4"))
285 #print os.path.join(sf, destNameMP4(ltitle,recpath,sf)+".mp4")
287 sa=search_mp4(recpath, recordedpath)
289 recdblist.printutf8(u"自動推測実行中")
290 sf=searchFolder(t,recordedpath)
292 recdblist.printutf8(u"移動先")
293 recdblist.printutf8(t+" : "+os.path.join(sf, destNameMP4(t, recpath,sf)+".mp4"))
294 recdblist.printutf8(u"実行中")
295 execMove(t, recpath, recordedpath)
297 recdblist.printutf8(t+" can't find matching folder.")
299 sa=search_mp4(recpath, recordedpath)
301 sf=searchFolder(t,recordedpath)
303 recdblist.printutf8(t+u" : "+os.path.join(sf, destNameMP4(t, recpath,sf)+".mp4"))
304 elif opts.etitle!="":
305 etitle=unicode(opts.etitle,'utf-8')
306 execMove(etitle,recpath, recordedpath)