New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how could I save audio to wavfile? #357
Comments
Hi, please try below. The reason is that your app script is re-executed every time component state (e.g. playing or not) changes then import queue
import pydub
import streamlit as st
from streamlit_webrtc import webrtc_streamer, WebRtcMode, ClientSettings
def main():
webrtc_ctx = webrtc_streamer(
key="sendonly-audio",
mode=WebRtcMode.SENDONLY,
audio_receiver_size=256,
client_settings=ClientSettings(
rtc_configuration={
"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
},
media_stream_constraints={
"audio": True,
},
),
)
if "audio_buffer" not in st.session_state:
st.session_state["audio_buffer"] = pydub.AudioSegment.empty()
status_indicator = st.empty()
while True:
if webrtc_ctx.audio_receiver:
try:
audio_frames = webrtc_ctx.audio_receiver.get_frames(timeout=1)
except queue.Empty:
status_indicator.write("No frame arrived.")
continue
status_indicator.write("Running. Say something!")
sound_chunk = pydub.AudioSegment.empty()
for audio_frame in audio_frames:
sound = pydub.AudioSegment(
data=audio_frame.to_ndarray().tobytes(),
sample_width=audio_frame.format.bytes,
frame_rate=audio_frame.sample_rate,
channels=len(audio_frame.layout.channels),
)
sound_chunk += sound
if len(sound_chunk) > 0:
st.session_state["audio_buffer"] += sound_chunk
else:
status_indicator.write("AudioReciver is not set. Abort.")
break
audio_buffer = st.session_state["audio_buffer"]
if not webrtc_ctx.state.playing and len(audio_buffer) > 0:
st.info("Writing wav to disk")
audio_buffer.export("temp.wav", format="wav")
# Reset
st.session_state["audio_buffer"] = pydub.AudioSegment.empty()
if __name__ == "__main__":
main() |
If your purpose is only to record the sound to a file without any processing with from streamlit_webrtc import webrtc_streamer, WebRtcMode, ClientSettings
from aiortc.contrib.media import MediaRecorder
def main():
def recorder_factory():
return MediaRecorder("record.wav")
webrtc_streamer(
key="sendonly-audio",
mode=WebRtcMode.SENDONLY,
in_recorder_factory=recorder_factory,
client_settings=ClientSettings(
rtc_configuration={
"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
},
media_stream_constraints={
"audio": True,
"video": False,
},
),
)
if __name__ == "__main__":
main() |
thx very much for your help, I will dive into webRTC and streamlit for details, thxs |
I use sample code as above, but it doesn't work as expected if you play it (seems it missing half of audio frame), it used to work before. |
if you put wavfile in audition, you get ( by the way, record wav using |
mm, while I'm not familiar with sound processing and cannot understand what the figures mean, |
set options doesn't work, but if I change mode to I write a gist here for reproduce |
@whitphx @Jackiexiao I just did in your @Jackiexiao's gist. I played it right after I recorded it, and it seems the speed of the |
@hihunjin use this function to record https://gist.github.com/Jackiexiao/9c45d5ad9caf524471b5ed966c57b5b9#file-streamlit_recorder-py-L131 |
@Jackiexiao thank you for your investigation and problem-solving. |
Hi! Im using the code that you post there https://gist.github.com/Jackiexiao/9c45d5ad9caf524471b5ed966c57b5b9#file-streamlit_recorder-py-L131. When i click the START button it works just fine but when i stop it the audio file doesn't save it anywhere. I don't know if a need to change some parameter or something like that. Sorry im new coding with streamlit :) |
Actually im getting this log #552 |
Hi @whitphx and @Jackiexiao, this code is very helpful. Thanks a lot! |
You can control the HTML attributes of the webrtc_ctx: WebRtcStreamerContext = webrtc_streamer(
key="sendonly-audio",
mode=WebRtcMode.SENDRECV,
in_recorder_factory=recorder_factory,
media_stream_constraints=MEDIA_STREAM_CONSTRAINTS,
audio_html_attrs={"muted": True} # THIS!
) When you set only audio_html_attrs={"muted": True, "controls": True} You can find other available attributes: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio |
I want to use streamlit-webrtc to record wav and save to disk (like
temp.wav'), but I have some problem, after I click
stopbutton, audio_buffer become
None` instead of audio from microphonemy code
The text was updated successfully, but these errors were encountered: