Skip to content

Commit

Permalink
Only allow 24fps worth of video into the queue
Browse files Browse the repository at this point in the history
  • Loading branch information
j0sh committed Feb 5, 2025
1 parent 8fe3cc8 commit 2722fc7
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions runner/app/live/trickle/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from .frame import InputFrame

MAX_FRAMERATE=24

def decode_av(pipe_input, frame_callback, put_metadata):
"""
Reads from a pipe (or file-like object).
Expand Down Expand Up @@ -63,6 +65,8 @@ def decode_av(pipe_input, frame_callback, put_metadata):
put_metadata(metadata)

reformatter = av.video.reformatter.VideoReformatter()
frame_interval = 1.0 / MAX_FRAMERATE
next_pts_time = 0.0
try:
for packet in container.demux():
if packet.dts is None:
Expand All @@ -84,6 +88,19 @@ def decode_av(pipe_input, frame_callback, put_metadata):
if frame.pts is None:
continue

# drop frames that come in too fast
# TODO also check timing relative to wall clock
pts_time = frame.time
if pts_time < next_pts_time:
# frame is too early, so drop it
continue
if pts_time > next_pts_time + frame_interval:
# frame is delayed, so reset based on frame pts
next_pts_time = pts_time + frame_interval
else:
# not delayed, so use prev pts to allow more jitter
next_pts_time = next_pts_time + frame_interval

w = 512
h = int((512 * frame.height / frame.width) / 2) * 2 # force divisible by 2
if frame.height > frame.width:
Expand Down

0 comments on commit 2722fc7

Please sign in to comment.