3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
7 __date__ ="$2009/09/14 17:38:25$"
20 recordedpath=configreader.getpath("recorded")
21 recpath=configreader.getpath("recpath")
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 print 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,560,590,602,690,860]
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, ft)
132 if os.path.isdir(fullpath):
133 lfpath.append(fullpath)
134 ftt=os.listdir(fullpath)
137 folderpath.append(os.path.join(fullpath, ft2))
139 lfpath.append(fullpath)
143 if os.path.isdir(dirp):
144 cmpp=os.path.dirname(dirp)
147 cmpp=os.path.basename(dirp)
148 appp=os.path.dirname(dirp)
149 p=n_gram.trigram(title.decode("utf-8"),cmpp.decode("utf-8"))
151 ngram.append((p,appp))
152 ngram=list(set(ngram))
156 #print title + ngram[0][1] + " : "+str(ngram[0][0])
163 def execMove(title,temppath,recpath):
164 srcpath=os.path.join(temppath,title+".mp4")
165 #desttitle=destName(title, temppath, recpath)
167 sf=searchFolder(title, recpath)
169 destpath=os.path.join(sf,destNameMP4(title, temppath, sf)+".mp4")
170 #os.path.join(sf,desttitle+".mkv")
174 print srcpath+" : "+destpath
175 shutil.move(srcpath, destpath)
176 #shutil.copy(srcpath, destpath)
177 delpath=[os.path.join(temppath,title+".ts")]
178 delpath.append(os.path.join(temppath,title+".avi"))
179 delpath.append(os.path.join(temppath,title+".ts.b25"))
180 delpath.append(os.path.join(temppath,title+".ts.tsmix"))
181 delpath.append(os.path.join(temppath,title+".ts.log"))
182 delpath.append(os.path.join(temppath,title+".sa.avi"))
183 delpath.append(os.path.join(temppath,title+".sa.avi.log"))
184 delpath.append(os.path.join(temppath,title+".log"))
191 def destName(title,temppath,recpath):
193 titleから移動先に作るべきファイル名を作り出す。
195 dstpath=os.path.join(recpath,title+".avi")
196 srcpath=os.path.join(temppath,title+".avi")
197 if os.path.exists(dstpath):
198 gmtime=time.gmtime(os.path.getmtime(srcpath))
199 times=time.strftime("%y%m%d",gmtime)
200 title=title+"_"+times
202 def destNameMKV(title,temppath,recpath):
204 titleから移動先に作るべきファイル名を作り出す。
206 dstpath=os.path.join(recpath,title+".mkv")
207 srcpath=os.path.join(temppath,title+".mkv")
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 destNameMP4(title,temppath,recpath):
215 titleから移動先に作るべきファイル名を作り出す。
217 dstpath=os.path.join(recpath,title+".mp4")
218 srcpath=os.path.join(temppath,title+".mp4")
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 detName(path,title):
226 type A ---title#<number>
227 type B ---title#<number>subtitle
228 type C ---title subtitle
229 type D ---title(without number)
231 #if re.match("#\d\s[0,10]\z|#[0-9]\s[0,10]\z", title)
233 #elif re.match("#\d\s[0,10].|#[0-9]\s[0,10].", title)
236 def detNameType(title):
238 type A ---title#<number>
239 type B ---title#<number>subtitle
240 type C ---title subtitle
241 type D ---title(without number)
244 #rA=re.compile(".+(?P<title>)#\d(?P<num>)\s[0,10]\z")
245 rA=re.compile("(.+)#(\d*)\s*\Z")
247 rB=re.compile("(.+)#(\d*)\s*(\D*)")
251 print "title="+tA.group(1)
252 print "num="+tA.group(2)
255 print "title="+tB.group(1)
256 print "num="+tB.group(2)
257 print "subtitle="+tB.group(3)
258 if __name__ == "__main__":
259 usage="usage: %prog read -h"
260 version="%prog 0.7.0beta1"
261 parser=optparse.OptionParser(usage=usage,version=version)
262 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)")
263 parser.add_option("-A","--Auto",action="store_true",dest="auto",default=False,help="auto classifying mode(not a test)")
264 parser.add_option("-e","--exec",action="store",type="string",dest="etitle",default="",metavar="TITLE",help="exec move(not a test)")
265 parser.add_option("-l","--list",action="store_true",dest="list",default=False,help="File listing mode(test for -a)")
266 (opts,args)=parser.parse_args(sys.argv)
268 sf=searchFolder(opts.ltitle, recordedpath)
269 print os.path.join(sf, destNameMKV(opts.ltitle,recpath,sf)+".mp4")
271 sa=search_mp4(recpath, recordedpath)
274 sf=searchFolder(t,recordedpath)
277 print t+" : "+os.path.join(sf, destNameMP4(t, recpath,sf)+".mp4")
279 execMove(t, recpath, recordedpath)
281 print t+" can't find matching folder."
283 sa=search_mp4(recpath, recordedpath)
285 sf=searchFolder(t,recordedpath)
287 print t+" : "+os.path.join(sf, destNameMP4(t, recpath,sf)+".mp4")
288 elif opts.etitle!="":
289 execMove(opts.etitle,recpath, recordedpath)