Skip to content

No Sound but video playing #2055

@zhanglifan1022nice

Description

@zhanglifan1022nice

I want to use webrtc as media player and input the frame and audio from local video file. But indeed, I only receive frame but no sound. Can you help me identify why?

import streamlit as st
from streamlit_webrtc import WebRtcMode, create_video_source_track, webrtc_streamer

# get frame
def get_next_video_frame()
  with av.open(self.video_path) as container:
      # 获取视频流(假设只有一个视频流)
      video_stream = next(s for s in container.streams if s.type == 'video')
  
      # 逐帧解码并返回
      for packet in container.demux(video_stream):
          for frame in packet.decode():
              yield frame

def video_source_callback(pts: int, time_base: fractions.Fraction) -> av.VideoFrame:
    import streamlit as st
    import warnings
    warnings.filterwarnings("ignore")
    if "video" not in st.session_state:
        vr = VideoReader("./sample.mp4")
        print("初始化完毕")
        st.session_state['video'] = vr
    buffer = st.session_state['video'].get_next_video_frame()
    if "last_frame" not in st.session_state:
        st.session_state['last_frame'] = buffer

    if buffer is None:
        buffer = st.session_state['last_frame']
    else:
        st.session_state['last_frame'] = buffer
    return buffer # av.VideoFrame.from_ndarray(buffer, format="bgr24")

video_source_track = create_video_source_track(
    video_source_callback, key="video_source_track", fps=25
)

webrtc_streamer(
    key="player",
    mode=WebRtcMode.RECVONLY,
    source_video_track=video_source_track,
    #source_audio_track=audio_source_track,
    media_stream_constraints={"video": True, "audio": True},
    on_change=on_change,
    # async_processing=True,
    # desired_playing_state=True
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions