-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDivide script.py
248 lines (219 loc) · 8.73 KB
/
Divide script.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# -*- coding: utf-8 -*-
"""
Split an AviSynth script into multiple trimmed avs
There's various dividing choices:
- Use the current AvsP bookmarks to delimit the Trims. The first and
last frame are automatically added to the bookmarks if not already
present.
- Specify a frame step
- Specify a time step
- Specify a number of intervals
If 'split at the current cursor position' is set, the script is only
evaluated until the line the cursor is on, and the Trims are inserted
before the next one. If this option and 'using the last evaluated
expression' are checked, the clip that is used as the script's return
is the one returned on the last evaluated expression, even if it's
assigned to a variable (doesn't set 'last').
Date: 2012-11-13
Latest version: https://github.com/vdcrim/avsp-macros
Changelog:
- fix Python 2.6 compatibility
- move all settings to the prompt
- add splitting options (bookmarks are not longer necessary)
- add 'split at current position' option
- fix file encodings and some cosmetics
- add 'using the last evaluated expression' option
- fix text evaluated when the cursor is on a multiline comment
- fix generated scripts not being correct in some cases
- strip tags and sliders from the script
Copyright (C) 2012 Diego Fernández Gosende <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
"""
# ------------------------------------------------------------------------------
# run in thread
import sys
import os
import os.path
import re
import pyavs
def parse_time(time):
''''Parse time (string) to ms
>>> parse_time('01:23:45.678')
5025678
>>> parse_time('01:23:45')
'''
splits = re.split(r'[:.]', time)
time_list = []
try:
for split in splits:
time_list.append(int(split))
except ValueError:
return
if len(time_list) != 4: return
h, m, s, ms = time_list
ms = ms + 1000 * (s + 60 * (m + 60 * h))
return ms
def float_range_list(start, stop, step):
'''float range (list) with stop included'''
ret = []
while start < stop:
ret.append(int(round(start)))
start += step
ret.append(stop)
return ret
self = avsp.GetWindow()
# Load default options
election = avsp.Options.get('election', _('using the current boomarks'))
frame_step = avsp.Options.get('frame_step', 10000)
time_step = avsp.Options.get('time_step', '0:01:00.000')
intervals = avsp.Options.get('intervals', 10)
use_current_position = avsp.Options.get('use_current_position', False)
use_last_expression = avsp.Options.get('use_last_expression', True)
use_dir = avsp.Options.get('use_dir', False)
use_base = avsp.Options.get('use_base', False)
# Get the default filename
filename = avsp.GetScriptFilename()
if filename:
dirname, basename = os.path.split(filename)
elif self.version > '2.3.1':
dirname, basename = os.path.split(avsp.GetScriptFilename(propose='general'))
else:
dirname, basename = (self.options['recentdir'], self.scriptNotebook.GetPageText(
self.scriptNotebook.GetSelection()).lstrip('* '))
if use_dir:
dirname = avsp.Options.get('dirname', '')
if use_base:
basename = avsp.Options.get('basename', '')
basename2, ext = os.path.splitext(basename)
if ext in ('.avs', '.avsi'):
basename = basename2
filename = os.path.join(dirname, basename)
# Ask for options
while True:
election_list = (_('using the current boomarks'), _('specifying a frame step'),
_('specifying a time step'), _('specifying a number of intervals'),
election)
options = avsp.GetTextEntry(title=_('Divide script'),
message=[_('Split script by...'),
[_('Frame step'), _('Time step'), _('Number of intervals')],
[_('Split at the current cursor position'),
_('... using the last evaluated expression')],
_('Choose a directory and basename'),
[_('Use always this directory'), _('Use always this basename')]],
default=[election_list,
[(frame_step, 1, None, 0, max(1, 10 ** (len(str(frame_step)) - 2))),
time_step, (intervals, 1)],
[use_current_position, use_last_expression],
filename, [use_dir, use_base]],
types=['list_read_only', ['spin', '', 'spin'], ['check', 'check'],
'file_save', ['check', 'check']],
width=400)
if not options:
return
(election, frame_step, time_step, intervals, use_current_position,
use_last_expression, filename, use_dir, use_base) = options
if election == _('specifying a time step'):
time_step_ms = parse_time(time_step)
if not time_step_ms:
avsp.MsgBox(_('Malformed time: '+ time_step), _('Error'))
continue
if filename:
filename = filename.lstrip()
break
elif not avsp.MsgBox(_('An output path is needed'), _('Error'), True):
return
# Save default options
avsp.Options['election'] = election
avsp.Options['frame_step'] = frame_step
avsp.Options['time_step'] = time_step
avsp.Options['intervals'] = intervals
avsp.Options['use_current_position'] = use_current_position
avsp.Options['use_last_expression'] = use_last_expression
avsp.Options['use_dir'] = use_dir
avsp.Options['use_base'] = use_base
if use_dir:
avsp.Options['dirname'] = os.path.dirname(filename)
if use_base:
avsp.Options['basename'] = os.path.basename(filename)
# Eval script
encoding = sys.getfilesystemencoding()
if self.version > '2.3.1':
text = avsp.GetText(clean=True)
else:
text = self.getCleanText(avsp.GetText())
var = 'last'
assign = ''
if use_current_position:
text = text.encode('utf-8') # StyledTextCtrl uses utf-8 internally
script = self.currentScript
re_assign = re.compile(r'\s*(\w+)\s*=')
for line in range(script.GetCurrentLine(), -1 , -1):
line_text = script.GetLine(line).strip()
line_pos = script.PositionFromLine(line)
if (line_text and script.GetStyleAt(line_pos) not in script.commentStyle):
if use_last_expression:
match = re_assign.match(line_text)
if match:
var = match.group(1)
assign = '{0}={0}.'.format(var)
script.SetCurrentPos(line_pos)
script.SetAnchor(line_pos)
break
index = script.GetLineEndPosition(script.GetCurrentLine())
text_enc = text[:index], text[index:]
text = text_enc[0].decode('utf-8')
if encoding != 'utf8': # AviSynth expects mbcs
text_enc = (text.encode(encoding).rstrip(),
text_enc[1].decode('utf-8').encode(encoding))
else:
text_enc = text.encode(encoding).rstrip(), ''
clip = pyavs.AvsClip(u'{0}\nreturn {1}'.format(text, var))
if not clip.initialized or clip.IsErrorClip():
avsp.MsgBox(_('Error loading the script'), _('Error'))
return
frame_count = clip.Framecount
fps = clip.Framerate
del clip
# Get the list of frames
if election == _('using the current boomarks'):
frame_list = avsp.GetBookmarkList()
if not frame_list:
avsp.MsgBox(_('There is not bookmarks'), _('Error'))
return
frame_list.sort()
if frame_list[0] != 0:
frame_list[:0] = [0]
if frame_list[-1] == frame_count - 1:
frame_list[-1] = frame_count
else:
frame_list.append(frame_count)
elif election == _('specifying a frame step'):
frame_list = float_range_list(0, frame_count, frame_step)
elif election == _('specifying a time step'):
frame_list = float_range_list(0, frame_count, fps * time_step_ms / 1000)
elif election == _('specifying a number of intervals'):
frame_list = float_range_list(0, frame_count, frame_count / float(intervals))
# Save scripts
global avs_list
avs_list = []
filename2, ext = os.path.splitext(filename)
if ext in ('.avs', '.avsi'):
filename = filename2
digits = len(str(len(frame_list) - 1))
for i, frame in enumerate(frame_list[:-1]):
avs_path = u'{0}_{1:0{2}}.avs'.format(filename, i+1, digits)
avs_list.append(avs_path)
with open(avs_path, 'w') as f:
f.write(text_enc[0] +
'\n{0}Trim({1},{2})'.format(assign, frame, frame_list[i+1] - 1).encode(encoding) +
text_enc[1])
return avs_list