forked from vdcrim/AvsP-macros
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Matroska chapters from Trims.py
382 lines (334 loc) · 13.1 KB
/
Matroska chapters from Trims.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# -*- coding: utf-8 -*-
"""
Generate Matroska chapters from Avisynth Trims.
Additionally cut audio and text subtitles to match the trimmed video.
Requirements:
- vfr.py (for chapters, qpfile, and audio)
http://forum.doom9.org/showthread.php?t=154535
- MKVToolNix (for vfr.py, only required for templates and audio)
http://www.bunkus.org/videotools/mkvtoolnix/
- TrimSubs.py (for trimmed timecodes and subtitles)
http://forum.doom9.org/showthread.php?t=163653
- PySubs 0.1.1 (for TrimSubs.py, if not using it as an executable)
http://pypi.python.org/pypi/pysubs
- Python 3.2 (if not using both vfr and TrimSubs as executables)
http://python.org/
Description:
This macro is a wrapper for vfr.py and TrimSubs.py. It parses the
Avisynth script in the current tab for a line with uncommented Trims,
and based on them generates a Matroska chapter file and a QP file to
be used with x264. The chapter file can be customized through the use
of a template (see vfr.py documentation). By using a template, the
chapters can be ordered (ordered chapters, ordered editions).
There are three ways of specifying the line of the avs used:
- Parse the avs from top to bottom or vice versa, and use the first
line with Trims found.
- Use a line with a specific comment at the end, e.g:
Trim(0,99)++Trim(200,499) # cuts
- Directly specifying the Trims line number, starting with 1.
A frame rate or timecode file (v1 or v2) is required, except for MicroDVD
subtitles. All other fields are optional. The macro attempts to find
valid default values for all parameters. If a timecode is supplied, a
new trimmed timecode v2 file is also generated.
Audio and text subtitle files can be cut to match the trimmed video.
The audio cutting is lossless.
Supported formats:
- Audio: as MKVToolNix does (hint: a lot).
- Subtitles: ASS, SSA, SRT, SUB (MicroDVD).
Ouput filenames (ext: original extension of the input file):
Chapters: avs_name.xml (mkv) or avs_name.chapters.txt (ogm)
QP file: avs_name.qpfile
Timecode: input_timecode_name.otc.ext
Audio: input_audio_name.cut.mka
Subtitles: input_subtitle_name.cut.ext
Installation instructions:
Place this script in your "AvsPmod\macros" directory. By default,
vfr.py and TrimSubs.py are expected to be found on "AvsPmod\tools",
subdirectories also valid. Set a custom path in the "preferences"
section of this file otherwise.
If any of these two Python scripts isn't in stand-alone executable
form, you will need to get installed Python 3.2 as well.
Some of the features in vfr.py require MKVToolNix. You must do one
of the following:
- Install or extract MKVToolNix to a directory in your PATH.
- Install or extract MKVToolNix to any directory and set a custom
path in the first lines of vfr.py.
If your copy of TrimSubs.py isn't a stand-alone executable, you'll
also neeed PySubs. Do one of the following:
- Install PySubs with the Windows installer.
- Extract the contents of the source package to any directory, and put
TrimSubs.py inside. You can then move the directory to "AvsPmod\tools"
if you want.
Date: 2012-11-27
Mod Date: 2023-07-08
Latest version: https://github.com/vdcrim/avsp-macros
Doom9 Forum thread: http://forum.doom9.org/showthread.php?t=163653
Changelog:
- support chapter names from file
- add custom parsing order and label feature. Needs vfr.py 0.8.6.1+
- update prompt dialog
- allow specifying directly the line used. Needs vfr.py-d024de920a
- *nix compatibility
Copyright (C) 2011, 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>.
"""
# PREFERENCES
# Save the avs script before starting
save_avs = True
# Set custom paths, e.g. ur"D:\vfr.py". Python is not required if using
# executables. vfr and TrimSubs path must include the right extension
python_path = ur"python3"
vfr_path = ur""
trimsubs_path = ur""
# Avisynth script default parsing order
parse_avs_top2bottom = False
# Only match the Trims line with 'label' as commentary
# e.g: Trim(0,99)++Trim(200,499) # cuts
use_a_label_as_default = False
default_label = 'cuts'
# Set to False to use a default fps in the dialog box
# (avoids refreshing the avs script)
fps_from_avs = True
fps_default = '24000/1001'
# Suffix list for automatic timecode file search
tc_suffix = ['.tc.txt', '.timecode.txt', '.timecodes.txt', 'timecode',
'timecodes', '.txt']
# Filename list for automatic template file search
template_list = ['template.txt', 'template']
# Filename list for automatic name file search
name_list = ['chnames.txt', 'names']
# Set the subtitle file encoding. Only needed when it's neither a
# Unicode encoding nor the system's locale encoding.
# See http://docs.python.org/library/codecs.html#standard-encodings
encoding = ''
# Generate OGM chapters instead of Matroska
ogm_chapters = False
# If this is set to True, show the log when the tasks are finished
show_log = False
#-------------------------------------------------------------------------------
# run in thread
from os import name, walk
from os.path import isfile, splitext, dirname, join
from sys import getfilesystemencoding
import subprocess, shlex
# Check paths
if vfr_path:
if isfile(vfr_path):
if vfr_path.endswith('.py'):
vfr_path = python_path + '" "' + vfr_path
else:
avsp.MsgBox(_('Custom vfr.py path is invalid:\n') + vfr_path, _('Error'))
return
if trimsubs_path:
if isfile(trimsubs_path):
if trimsubs_path.endswith('.py'):
trimsubs_path = python_path + '" "' + trimsubs_path
else:
avsp.MsgBox(_('Custom TrimSubs.py path is invalid:\n') + trimsubs_path,
_('Error'))
return
if not vfr_path or not trimsubs_path:
for parent, dirs, files in walk(avsp.GetWindow().toolsfolder):
for file in files:
if not vfr_path:
if file == 'vfr.py':
vfr_path = python_path + '" "' + join(parent, file)
if trimsubs_path:
break
else:
continue
if file == 'vfr.exe':
vfr_path = join(parent, file)
if trimsubs_path:
break
else:
continue
if not trimsubs_path:
if file == 'TrimSubs.py':
trimsubs_path = python_path + '" "' + join(parent, file)
if vfr_path:
break
else:
continue
if file == 'TrimSubs.exe':
trimsubs_path = join(parent, file)
if vfr_path:
break
else:
continue
else:
continue
break
else:
if not vfr_path:
avsp.MsgBox(_('vfr.py not found'), _('Error'))
if not trimsubs_path:
avsp.MsgBox(_('TrimSubs.py not found'), _('Error'))
return
# Popen doesn't search PATH on Windows if shell=False
shell = name == 'nt'
# Set the prompt default values
if not avsp.GetScriptFilename():
if not avsp.SaveScript():
return
if save_avs and not avsp.IsScriptSaved():
avsp.SaveScript()
avs = avsp.GetScriptFilename()
if not avs:
return
# Python 2.x doesn't support unicode args in subprocess.Popen()
# http://bugs.python.org/issue1759845
# Encoding to system's locale encoding
code = getfilesystemencoding()
avs_no_ext = splitext(avs)[0]
parsing_order = _('Top to bottom') if parse_avs_top2bottom else _('Bottom to top')
label = default_label if use_a_label_as_default else ''
for path in (avs_no_ext + ext for ext in tc_suffix):
if isfile(path):
fps = path
break
else:
if fps_from_avs:
fps = str(avsp.GetVideoFramerate())
else:
fps = fps_default
if not ogm_chapters:
for path in (join(dirname(avs),name) for name in template_list):
if isfile(path):
template = path
break
else:
template = ''
else:
template = ''
for path in (join(dirname(avs),name) for name in name_list):
if isfile(path):
names = path
break
else:
names = ''
def suffix_generator(list, suffix, max=4):
for item in list:
yield item
for n in range(max):
for item in list:
yield suffix + str(n) + item
audio_ext = ['.flac', '.dts', '.ac3', '.ogg', '.m4a', '.aac', '.mp3', '.mp2',
'.wav', '.mka', '.webma', '.thd', '.eac3', '.truehd', '']
for path in (avs_no_ext + suffix for suffix in suffix_generator(audio_ext, '_Track')):
if isfile(path):
audio = path
break
else:
audio = ''
sub_ext = ['.ass', '.ssa', '.srt', '.sub']
for path in (avs_no_ext + suffix for suffix in suffix_generator(sub_ext, '_Track')):
if isfile(path):
subtitles = path
break
else:
subtitles = ''
timecode_filter = (_('Text files') + ' (*.txt)|*.txt|' + _('All files') + '|*.*')
template_filter = (_('Text files') + ' (*.txt)|*.txt|' + _('All files') + '|*.*')
name_filter = (_('Text files') + ' (*.txt)|*.txt|' + _('All files') + '|*.*')
subs_filter = (_('Subtitle files') + ' (*.ass;*.ssa;*.srt;*.sub)|*.ass;*.ssa;*.srt;*.sub|' +
_('All files') + '|*.*')
# Prompt for parameters
options = avsp.GetTextEntry(
title=_('Matroska chapters from Trims - parameters for vfr and TrimSubs'),
message=[[_('Avisynth script parsing order for\nsetting the line with Trims used'),
_('Only consider the lines with Trims\nthat end with this commentary...'),
_('Use directly this line, starting\nwith 1')],
_('Frame rate or timecode file (v1 or v2)'),
_('Template file'), _('Chapters Names file'), _('Audio file'), _('Subtitle file'),
_('Additional parameters for vfr.py')],
default=[[(_('Top to bottom'), _('Bottom to top'), _(parsing_order)), label, (0, 0)],
(fps, timecode_filter), (template, template_filter), (names, name_filter), audio,
(subtitles, subs_filter), ''],
types=[['list_read_only', '', 'spin'], 'file_save', 'file_open', 'file_open', 'file_open', 'file_open'],
width=300)
if not options:
return
# Set parameters
if options[2]:
line = ' --line ' + str(options[2])
order = label = ''
else:
line = ''
order = '' if options[0] == _('Top to bottom') else ' --reverse'
label = ' --label ' + options[1].strip() if options[1] else ''
fps = options[3]
if fps:
if isfile(fps):
fps = ' --fps "' + fps + '"'
otc = ' --otc'
else:
fps = ' --fps ' + fps
otc = ''
else:
avsp.MsgBox(_('A frame rate value or timecode file is required'), _('Error'))
return
template = ' --template "' + options[4] + '"' if options[4] else ''
names = ' -n "' + options[5] + '"' if options[5] else ''
audio = ' --input "' + options[6] + '" --merge --remove' if options[6] else ''
subtitles = ' --input "' + options[7] + '"' if options[7] else ''
if encoding:
encoding = ' --encoding ' + encoding
chapters = ' --chapters "' + avs_no_ext
chapters += '.chapters.txt"' if ogm_chapters else '.xml"'
# Start processes
vfr_args = ('"' + vfr_path + '"'
+ ' --verbose'
+ ' "' + avs + '"'
+ line
+ order
+ label
+ chapters
+ template
+ fps
+ names
+ audio
+ ' ' + options[8]).encode(code)
log = '\nVFR.PY\n\n' + vfr_args
p = subprocess.Popen(shlex.split(vfr_args), shell=shell,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = p.communicate()[0]
if p.returncode:
error_message = stdout.partition('Traceback (most recent call last):')
error_message = error_message[2] if error_message[1] else stdout
avsp.MsgBox(stdout, _('vfr.py rrror'))
return
log = log + '\n\n' + stdout
if subtitles or otc:
order = ' --reversed' if order else ''
trimsubs_args = ('"' + trimsubs_path + '"'
+ ' --verbose'
+ ' "' + avs + '"'
+ line
+ order
+ label
+ subtitles
+ encoding
+ fps
+ otc).encode(code)
log = log + '\n\nTRIMSUBS.PY\n\n' + trimsubs_args
p = subprocess.Popen(shlex.split(trimsubs_args), shell=shell,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = p.communicate()[0]
if p.returncode:
error_message = stdout.partition('Traceback (most recent call last):')
error_message = error_message[2] if error_message[1] else stdout
avsp.MsgBox(error_message, _('TrimSubs error'))
return
log = log + '\n\n' + stdout
if show_log:
print log