forked from vdcrim/AvsP-macros
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Import bookmarks from file (Extend).py
171 lines (154 loc) · 7.17 KB
/
Import bookmarks from file (Extend).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import re
import cPickle
filename = avsp.GetFilename(_('Select a file'), filefilter=
_('All supported files') + '|*.txt;*.xml;*.ses;*.log;*.qp;*.qpf|' +
_('Chapters Text files') + ' (*.txt)|*.txt|'+
_('Matroska XML files') + ' (*.xml)|*.xml|' +
_('Celltimes files') + ' (*.txt)|*.txt|' +
_('AvsP Session files') + ' (*.ses)|*.ses|' +
_('TFM log files') + ' (*.log)|*.log|' +
_('XviD log files') + ' (*.log)|*.log|' +
_('QP files') + ' (*.qp/*.qpf)|*.qp;*.qpf|' +
_('Timecode format v1 files') + ' (*.txt)|*.txt|' +
_('All files') + ' (*.*)|*.*')
if not filename:
return
lines = avsp.GetWindow().GetTextFromFile(filename)[0]
bookmarkDict = {}
# parsing QP-file
if not bookmarkDict:
try:
for index in lines.strip().split('\n'):
s=index.strip()
if s!='':
bm = s.split(' ')
bookmarkDict[int(bm[0])] = '' if len(bm) == 1 else ' '.join(bm[1:])
except:
bookmarkDict = {}
# parsing Timecode format v1: place a bookmark on every starting frame
if not bookmarkDict:
if lines.startswith('# timecode format v1'):
match = re.search(r'^\s*assume\s*(\d*\.*\d+\.*\d*)', lines, re.M|re.I)
base_fps = (match.group(1) if match else 'unknown') + ' fps'
bookmarkDict[0] = base_fps
for line in lines.splitlines():
if line and line[0].isdigit():
start, end, fps = line.split(',')
bookmarkDict[int(start)] = fps + ' fps'
bookmarkDict[int(end)+1] = base_fps
# parsing SCXviD log
if not bookmarkDict:
try:
if lines.startswith('# XviD 2pass stat file'):
bookmarkDict=dict((i-3,'') for i,v in enumerate(lines.split('\n')) if v.startswith('i'))
except:
bookmarkDict = {}
# parsing TFM output
if not bookmarkDict:
if lines.startswith('#TFM '):
try:
stats = lines.split('# FORMAT:')
if len(stats)==5:
sectionslice = (0,(2,-2),(2,-4),(2,-4),(2,-1))
section = lambda sectionidx: stats[sectionidx].strip().split('\n')[sectionslice[sectionidx][0]:sectionslice[sectionidx][1]]
sectionisempty = lambda sectionidx: 'none detected' in stats[sectionidx]
frameindent = 4
frametitle = lambda line: line[line.find(' ',frameindent+1)+1:]
framenum = lambda line: int(line[1:line.find(' ',frameindent)])
dCombed = dict( (framenum(L), frametitle(L)) for L in section(1) ) if not sectionisempty(1) else {}
dGrouped = dict( (int(F), frametitle(L)) for L in section(2) for F in re.split('[\s,]',L[frameindent:])[:-2] ) if not sectionisempty(2) else {}
dPossible = dict( (framenum(L), frametitle(L)) for L in section(3) ) if not sectionisempty(3) else {}
dUBmatch = dict( (int(F),L[-1]) for L in section(4) for F in re.split('[\s,]',L[frameindent:-2]) ) if not sectionisempty(4) else {}
maxframe = max([max(d.keys()) if d.keys() else -1 for d in (dCombed, dPossible, dUBmatch)])
if maxframe == -1:
avsp.MsgBox(_('Not combed or out of order frames'), _('Bookmarks from TFM file'))
return
s=avsp.GetTextEntry( \
[_('Combed') + ' (%d)' % len(dCombed),\
_('Possible') + ' (%d)' % len(dPossible),\
_('u,b,out-of-order') + ' (%d)' % len(dUBmatch),\
'',\
_('Min frame:'),\
_('Max frame:')],\
[True,True,True,'','0',str(maxframe)],\
_('TFM log parser'),\
['check','check','check','sep','text','text'],\
250 )
if not s: return
if s[0]: bookmarkDict.update(dCombed)
if s[1]: bookmarkDict.update(dPossible)
if s[2]: bookmarkDict.update(dUBmatch)
try:
f1,f2=int(s[3]),int(s[4])
if f1!=0 or f2!=maxframe:
bookmarkDict=dict( (f,t) for (f,t) in bookmarkDict.items() if f1<=f<=f2 )
except:
pass
avsp.GetWindow().GetStatusBar().SetStatusText( _('%d frames imported') % len(bookmarkDict) )
except:
raise
avsp.MsgBox(_('[COMBED FRAMES] section could not be parsed'))
return
# parsing chapters text files
if not bookmarkDict:
timeList = re.findall(r'(\d+)=(\d+):(\d+):(\d+\.\d+)', lines)
if timeList:
fps = avsp.GetVideoFramerate()
titleDict = {}
for index, title in re.findall(r'(\d+)NAME=(.*)', lines, re.I):
titleDict[index] = title
for index, hr, min, sec in timeList:
sec = int(hr)*3600 + int(min)*60 + float(sec)
bookmark = int(round(sec*fps))
bookmarkDict[bookmark] = titleDict.get(index, '')
# parsing matroska xml files
if not bookmarkDict:
sections = re.findall(r'<ChapterAtom>(.*?)</ChapterAtom>', lines, re.I|re.S)
fps = avsp.GetVideoFramerate()
for text in sections:
timecode = re.search(r'<ChapterTimeStart>(\d+):(\d+):(\d+\.\d+)</ChapterTimeStart>', text)
if not timecode:
continue
title = re.search(r'<ChapterString>(.*?)</ChapterString>', text)
hr, min, sec = timecode.groups()
sec = int(hr)*3600 + int(min)*60 + float(sec)
bookmark = int(round(sec*fps))
bookmarkDict[bookmark] = title.group(1) if title else ''
# parsing celltime format - frame count content
if not bookmarkDict:
try:
for index in lines.strip().split():
bookmarkDict[int(index)] = ''
except:
bookmarkDict = {}
# parsing AvsP ssesion files
if not bookmarkDict:
try:
f = open(filename, 'rb')
session = cPickle.load(f)
except:
pass
f.close()
try:
if 'bookmarks' in session:
if 'bookMarkDict' in session:
for bookmark, btype in session['bookmarks']:
bookmarkDict[bookmark] = session['bookMarkDict'].get(bookmark, '')
else:
for bookmark, btype in session['bookmarks']:
bookmarkDict[bookmark] = ''
except:
pass
if bookmarkDict:
bookmarkList = bookmarkDict.items()
# Don't delete current bookmarks, update its title if supplied
oldBookmarks = avsp.GetBookmarkList()
for bookmark, title in bookmarkDict.items():
if bookmark in oldBookmarks:
if title:
bookmarkList.append((bookmark, title))
else:
bookmarkList.remove((bookmark, title))
avsp.SetBookmark(bookmarkList)
else:
avsp.MsgBox(_('Bookmark file unrecognized!'), _('Error'))