I have a playlist for some MP3 Files.
Would it be possible to load them by an M3U file and play it like this:
st.audio(bytes, format=“audio/m3u”)
Hi @aro,
At present, st.audio
doesn’t allow this for a couple of different reasons. Mainly it’s just that the audio widget on the front-end isn’t set up to understand what to do with a list of files, only one file at a time.
What you could do is load your m3u file into a list that could populate a multiselect for an audio player. Something like this, for example (untested code):
import streamlit as st
def m3u_to_list(filename):
out = []
with open(filename) as fh:
for line in fh.read().split("\n"):
line = line.strip()
if not line.startswith("#")
out.append(line)
return out
playlist = m3u_to_list("songs.m3u")
song = st.selectbox("Pick an MP3 to play", playlist, 0)
st.audio(song)
Or you could approximate a random-picker for the playlist by using a random function that chooses a song to play from that generated list every time the script runs. So instead of the st.selectbox
you’d have something like this:
import random
st.audio(random.choice(playlist))
Let me know if any of these suggestions help!
Thank you very much @nthmost . That helps me a lot indeed.
I did not know that m3u is so easy
I’m thinking about merging all the files together, but then it becomes very big.
Just for the sake of completeness:
...
audio_file = open(song, 'rb')
audio_bytes = audio_file.read()
st.audio(audio_bytes)
Thanks a lot!
Nice, thanks for completing the thought.
And just FYI, I’m currently implementing a change in the way we process A/V files where we’re going to dump audiovisual bytes into a virtual file and serve it via HTTP anyway. This means we’ll be able to support running st.audio
from a local file instead of having to load it into bytes first. And it means that even if you load st.audio
with bytes, Streamlit will then turn it back into a file.
Of course if your streamlit app is manipulating your audio files and it makes more sense to do st.audio(some_bytes)
, you still can.