LiveKit: Stream audio and video using python sdk

47 Views Asked by At

I am able to stream video but not audio using python sdk . Here is my code to connect to a room and play video (unfortunately no audio). What should I do to also include audio from my video.mp4

I dont want to create a separate track for audio because that may lead to mismatch. is there a library that can do both for me?

async def main(room: rtc.Room):
    # get token and connect to room - not included
    # publish a track
    source = rtc.VideoSource(WIDTH, HEIGHT)
    track = rtc.LocalVideoTrack.create_video_track("hue", source)
    options = rtc.TrackPublishOptions()
    options.source = rtc.TrackSource.SOURCE_CAMERA
    publication = await room.local_participant.publish_track(track, options)
    video_path = 'video.mp4'
    await display_video(source, video_path)


async def display_video(source: rtc.VideoSource, video_path: str):
    cap = cv2.VideoCapture(video_path)

    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break

        # Resize frame if necessary
        frame = cv2.resize(frame, (WIDTH, HEIGHT))

        # Convert BGR frame to RGBA format
        rgba_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)

        # Create a VideoFrame and capture it
        frame_data = rgba_frame.tobytes()
        frame = rtc.VideoFrame(WIDTH, HEIGHT, rtc.VideoBufferType.RGBA, frame_data)
        source.capture_frame(frame)

        await asyncio.sleep(1 / 30)  # Adjust sleep time for desired frame rate

    cap.release()
0

There are 0 best solutions below