Spaces:
Running
Running
Create streamlit-audiorecorder_An_audio_Recorder_for_streamlit.txt
Browse files
streamlit-audiorecorder_An_audio_Recorder_for_streamlit.txt
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit-audiorecorder_An_audio_Recorder_for_streamlit.txt
|
2 |
+
|
3 |
+
streamlit-audiorecorder
|
4 |
+
An audio Recorder for streamlit
|
5 |
+
Description
|
6 |
+
Audio recorder component for streamlit.
|
7 |
+
It creates a button to start the recording and takes three arguments: the start button text, the stop button text, and the pause button text.
|
8 |
+
If the pause button text is not specified, the pause button is not displayed.
|
9 |
+
|
10 |
+
Parameters
|
11 |
+
The signature of the component is:
|
12 |
+
|
13 |
+
audiorecorder(start_prompt="Start recording", stop_prompt="Stop recording", pause_prompt="", key=None):
|
14 |
+
The prompt parameters are self-explanatory, and the optionnal key parameter is used internally by streamlit to properly distinguish multiple audiorecorders on the page.
|
15 |
+
|
16 |
+
Return value
|
17 |
+
The component's return value is a pydub AudioSegment.
|
18 |
+
All AudioSegment methods are available, in particular you can:
|
19 |
+
|
20 |
+
Play the audio in the frontend with st.audio(audio.export().read())
|
21 |
+
Save the audio to a file with audio.export("audio.wav", format="wav")
|
22 |
+
Installation:
|
23 |
+
pip install streamlit-audiorecorder
|
24 |
+
Note: This package uses ffmpeg, so it should be installed for this audiorecorder to work properly.
|
25 |
+
|
26 |
+
On ubuntu/debian: sudo apt update && sudo apt install ffmpeg
|
27 |
+
On mac: brew install ffmpeg
|
28 |
+
|
29 |
+
Usage:
|
30 |
+
import streamlit as st
|
31 |
+
from audiorecorder import audiorecorder
|
32 |
+
|
33 |
+
st.title("Audio Recorder")
|
34 |
+
audio = audiorecorder("Click to record", "Click to stop recording")
|
35 |
+
|
36 |
+
if len(audio) > 0:
|
37 |
+
# To play audio in frontend:
|
38 |
+
st.audio(audio.export().read())
|
39 |
+
|
40 |
+
# To save audio to a file, use pydub export method:
|
41 |
+
audio.export("audio.wav", format="wav")
|
42 |
+
|
43 |
+
# To get audio properties, use pydub AudioSegment properties:
|
44 |
+
st.write(f"Frame rate: {audio.frame_rate}, Frame width: {audio.frame_width}, Duration: {audio.duration_seconds} seconds")
|
45 |
+
Troubleshooting:
|
46 |
+
Error: No record button is shown and you get the following error message in the console:
|
47 |
+
|
48 |
+
Component Error
|
49 |
+
Cannot read properties of undefined (reading 'getUserMedia')
|
50 |
+
Reason: To record the audio, this component is using the MediaDevices interface.
|
51 |
+
For security reasons, the getUserMedia() method is available only in secure contexts (HTTPS), as explained in the MDM documentation :
|
52 |
+
|
53 |
+
As an API that may involve significant privacy concerns, getUserMedia()'s specification lays out a wide array of privacy and security requirements that browsers are obligated to meet.
|
54 |
+
|
55 |
+
getUserMedia() is a powerful feature that can only be used in secure contexts; in insecure contexts, navigator.mediaDevices is undefined, preventing access to getUserMedia(). A secure context is, in short, a page loaded using HTTPS or the file:/// URL scheme, or a page loaded from localhost.
|
56 |
+
|
57 |
+
Solution: Serve your website using HTTPS. If you are serving your website locally, make sure to access it using localhost, not an IP address.
|