View Full Version : Trimming Video and Audio and keeping it in sync


ENunn
28th July 2026, 06:59
i've been trying vapoursynth off and on after being an avisynth main for years. other than certain plugins, the main thing holding me back from switching is trimming. i know that it's a different scripting language, but i can't get it right at all.

when i edit commercial compilations for youtube, i do all the editing in avisynth. it's really easy in avspmod. it involves multiple trims.

example avisynth script:
SetFilterMTMode("DEFAULT_MT_MODE", 2)
AVISource("F:\king5 nbc - news and days of our lives feb 2009 d-vhs.avi", pixel_type="yv12")
Trim(3839, 82862)
Trim(0, 4625) ++ Trim(14964, 79023)
Trim(0, 9281) ++ Trim(13820, 68685)
Trim(0, 15277) ++ Trim(25747, 64147)
Trim(0, 20674) ++ Trim(30971, 53678)
Trim(0, 25159) ++ Trim(31951, 43382)
Trim(0, 29645) ++ Trim(36206, 36591)
normalize(0.8912)
assumetff()
par=getparity()
SeparateFields().PointResize(width,height)
Deblock_QED()
AssumeFrameBased()
SeparateFields()
Merge(SelectEven(),SelectOdd())
par ? AssumeTFF() : AssumeBFF()
Weave()
QTGMC(Preset="slow", ezdenoise=0.0, tuning="dv-hd", sourcematch=3, MatchEnhance=1, NoiseProcess=1, NoiseRestore=1.0, Sigma=0.0 , sharpness=0, border=false)
Trim(0, 18563) ++ Trim(18571, 60063)
SuperRes(matrixin="rec709", 2, .43, 0, """nnedi3_rpow2(2, nns=4, cshift="Spline36Resize")""", matrixout="rec709")
prefetch(8)


is there not an easy way to do all this in vapoursynth?

Adub
12th August 2026, 00:33
While improved audio/video trimming is planned (https://github.com/vapoursynth/vapoursynth/issues/1248), it's entirely possible to trim video and audio together with VS.

It's important to note that video uses *frames* and audio uses *samples* as the smallest "unit". This unit is what you specify when you pass values to std.Trim and std.AudioTrim, respectively.

Thankfully, you can convert from one to the other pretty easily using some basic math.

Here's an example:


from vapoursynth import core

def frame_to_sample(frame, fps: Fraction, sample_rate: int):
"""
Converts a given frame number to a corresponding audio sample number
based on the video fps and the audio sample rate.
"""
return (sample_rate / fps) * frame

video = core.bs.VideoSource('foo.mkv')
audio = core.bs.AudioSource('foo.mkv')

trimmed_video = video.std.Trim(100, 200)
trimmed_audio = audio.std.AudioTrim(frame_to_sample(100, video.fps, audio.sample_rate), frame_to_sample(200, video.fps, audio.sample_rate))

trimmed_video.set_output()
trimmed_audio.set_output(1)


Then you can combine things together with the "+" operator:


all = video.std.Trim(100, 200) + video.std.Trim(300,400) + video.std.Trim(900, 950)

ENunn
12th August 2026, 03:29
While improved audio/video trimming is planned (https://github.com/vapoursynth/vapoursynth/issues/1248), it's entirely possible to trim video and audio together with VS.

It's important to note that video uses *frames* and audio uses *samples* as the smallest "unit". This unit is what you specify when you pass values to std.Trim and std.AudioTrim, respectively.

Thankfully, you can convert from one to the other pretty easily using some basic math.

Here's an example:


from vapoursynth import core

def frame_to_sample(frame, fps: Fraction, sample_rate: int):
"""
Converts a given frame number to a corresponding audio sample number
based on the video fps and the audio sample rate.
"""
return (sample_rate / fps) * frame

video = core.bs.VideoSource('foo.mkv')
audio = core.bs.AudioSource('foo.mkv')

trimmed_video = video.std.Trim(100, 200)
trimmed_audio = audio.std.AudioTrim(frame_to_sample(100, video.fps, audio.sample_rate), frame_to_sample(200, video.fps, audio.sample_rate))

trimmed_video.set_output()
trimmed_audio.set_output(1)


Then you can combine things together with the "+" operator:


all = video.std.Trim(100, 200) + video.std.Trim(300,400) + video.std.Trim(900, 950)


ahhh so basically for multiple trims, I just copy this a few times until I'm done with trimming?

Selur
12th August 2026, 20:39
I posted an example with a wrapper over at https://forum.videohelp.com/threads/420929-Vapoursynth-Trimming-Video-and-Audio-and-keeping-it-in-sync