Debian Codegraph @

examples/numpy/generate_video_with_pts.py

raw
1 #!/usr/bin/env python3
2
3 import colorsys
4 from fractions import Fraction
5
6 import numpy as np
7
8 import av
9
10 (width, height) = (640, 360)
11 total_frames = 20
12 fps = 30
13
14 container = av.open("generate_video_with_pts.mp4", mode="w")
15
16 stream = container.add_stream("mpeg4", rate=fps) # alibi frame rate
17 stream.width = width
18 stream.height = height
19 stream.pix_fmt = "yuv420p"
20
21 # ffmpeg time is complicated
22 # more at https://github.com/PyAV-Org/PyAV/blob/main/docs/api/time.rst
23 # our situation is the "encoding" one
24
25 # this is independent of the "fps" you give above
26 # 1/1000 means milliseconds (and you can use that, no problem)
27 # 1/2 means half a second (would be okay for the delays we use below)
28 # 1/30 means ~33 milliseconds
29 # you should use the least fraction that makes sense for you
30 stream.codec_context.time_base = Fraction(1, fps)
31
32 # this says when to show the next frame
33 # (increment by how long the current frame will be shown)
34 my_pts = 0 # [seconds]
35 # below we'll calculate that into our chosen time base
36
37 # we'll keep this frame around to draw on this persistently
38 # you can also redraw into a new object every time but you needn't
39 the_canvas = np.zeros((height, width, 3), dtype=np.uint8)
40 the_canvas[:, :] = (32, 32, 32) # some dark gray background because why not
41 block_w2 = int(0.5 * width / total_frames * 0.75)
42 block_h2 = int(0.5 * height / 4)
43
44 for frame_i in range(total_frames):
45 # move around the color wheel (hue)
46 nice_color = colorsys.hsv_to_rgb(frame_i / total_frames, 1.0, 1.0)
47 nice_color = (np.array(nice_color) * 255).astype(np.uint8)
48
49 # draw blocks of a progress bar
50 cx = int(width / total_frames * (frame_i + 0.5))
51 cy = int(height / 2)
52 the_canvas[cy - block_h2 : cy + block_h2, cx - block_w2 : cx + block_w2] = (
53 nice_color
54 )
55
56 frame = av.VideoFrame.from_ndarray(the_canvas, format="rgb24")
57
58 # seconds -> counts of time_base
59 frame.pts = int(round(my_pts / stream.codec_context.time_base))
60
61 # increment by display time to pre-determine next frame's PTS
62 my_pts += 1.0 if ((frame_i // 3) % 2 == 0) else 0.5
63 # yes, the last frame has no "duration" because nothing follows it
64 # frames don't have duration, only a PTS
65
66 for packet in stream.encode(frame):
67 container.mux(packet)
68
69 # finish it with a blank frame, so the "last" frame actually gets shown for some time
70 # this black frame will probably be shown for 1/fps time
71 # at least, that is the analysis of ffprobe
72 the_canvas[:] = 0
73 frame = av.VideoFrame.from_ndarray(the_canvas, format="rgb24")
74 frame.pts = int(round(my_pts / stream.codec_context.time_base))
75 for packet in stream.encode(frame):
76 container.mux(packet)
77
78 # the time should now be 15.5 + 1/30 = 15.533
79
80 # without that last black frame, the real last frame gets shown for 1/30
81 # so that video would have been 14.5 + 1/30 = 14.533 seconds long
82
83 # Flush stream
84 for packet in stream.encode():
85 container.mux(packet)
86
87 # Close the file
88 container.close()