Skip to content

Commit 3ae2d70

Browse files
committed
Document MP4 timestamp alignment (fixes #2324)
1 parent 2a349d7 commit 3ae2d70

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

examples/numpy/generate_video_with_pts.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@
22

33
import colorsys
44
from fractions import Fraction
5+
from math import lcm
56

67
import numpy as np
78

89
import av
910

1011
(width, height) = (640, 360)
1112
total_frames = 20
12-
fps = 30
13-
14-
container = av.open("generate_video_with_pts.mp4", mode="w")
13+
fps = Fraction(30, 1)
14+
15+
# MP4 stores a nonzero starting offset in an edit list using the movie timescale,
16+
# which defaults to 1000. Choose a timescale that can represent frame-aligned
17+
# offsets exactly; otherwise, a starting PTS such as 1 at 30 fps is rounded.
18+
movie_timescale = lcm(1000, fps.numerator)
19+
container = av.open(
20+
"generate_video_with_pts.mp4",
21+
mode="w",
22+
container_options={"movie_timescale": str(movie_timescale)},
23+
)
1524

1625
stream = container.add_stream("mpeg4", rate=fps) # alibi frame rate
1726
stream.width = width

0 commit comments

Comments
 (0)