Skip to content

Instantly share code, notes, and snippets.

@bmsan
Created October 26, 2021 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmsan/62e96ec6b50e87ecb7eb18d8828c4448 to your computer and use it in GitHub Desktop.
Save bmsan/62e96ec6b50e87ecb7eb18d8828c4448 to your computer and use it in GitHub Desktop.
import fractions
from aiortc.contrib.media import MediaRecorder
from aiortc.mediastreams import MediaStreamError
class MediaRecorderFixed30FPS(MediaRecorder):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.video_frame_ctr = {}
def addTrack(self, track):
super().addTrack(track)
if track.kind == "video":
self.video_frame_ctr[track.id] = 0
async def __run_track(self, track, context):
while True:
try:
frame = await track.recv()
if track.kind == 'video':
frame.pts = self.video_frame_ctr[track.id]
frame.time_base = fractions.Fraction(1, 30)
self.frame_ctr[track.id] += 1
except MediaStreamError:
return
for packet in context.stream.encode(frame):
self.__container.mux(packet)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment