From d10a188de4cf19c5419f255dd98a8685ada5e3db Mon Sep 17 00:00:00 2001 From: WyattBlue Date: Wed, 22 Jul 2026 18:34:38 -0400 Subject: [PATCH] Document MP4 timestamp alignment (fixes #2324) --- examples/numpy/generate_video_with_pts.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/examples/numpy/generate_video_with_pts.py b/examples/numpy/generate_video_with_pts.py index d84b34817..8de95b63f 100644 --- a/examples/numpy/generate_video_with_pts.py +++ b/examples/numpy/generate_video_with_pts.py @@ -2,6 +2,7 @@ import colorsys from fractions import Fraction +from math import lcm import numpy as np @@ -9,9 +10,17 @@ (width, height) = (640, 360) total_frames = 20 -fps = 30 - -container = av.open("generate_video_with_pts.mp4", mode="w") +fps = Fraction(30, 1) + +# MP4 stores a nonzero starting offset in an edit list using the movie timescale, +# which defaults to 1000. Choose a timescale that can represent frame-aligned +# offsets exactly; otherwise, a starting PTS such as 1 at 30 fps is rounded. +movie_timescale = lcm(1000, fps.numerator) +container = av.open( + "generate_video_with_pts.mp4", + mode="w", + container_options={"movie_timescale": str(movie_timescale)}, +) stream = container.add_stream("mpeg4", rate=fps) # alibi frame rate stream.width = width