Streamlit: how to auto play audio, video including video size

Posted on February 22, 2024


I am currently working on the talking avatar project and use streamlit to build the demo. Streamlit audio and video function cannot support autoplay and change video window size when playing. Search and find a solution foor audio, i.e. encoding audio into base64 and use native HTML tag to control audio play. But for video, not find one. Using similar method, encoding video into base64 with HTML video tag, it work well. The following are sample function code and actual result:

def autoplay_audio(wav_file, is_auto = True):
with open(wav_file, "rb") as f:
data = f.read()
b64 = base64.b64encode(data).decode()
if is_auto:
md = f"""
<audio controls autoplay="true">
<source src="data:audio/mp3;base64,{b64}" type="audio/wav">
</audio>
"""
else:
md = f"""
<audio controls autoplay="false">
<source src="data:audio/mp3;base64,{b64}" type="audio/wav">
</audio>
"""

return md

def autoplay_video(video_file, is_auto = True):
with open(video_file, "rb") as f:
data = f.read()
b64 = base64.b64encode(data).decode()
if is_auto:
md = f"""
<video controls width=320 height=240 autoplay>
<source src="data:video/mp4;base64,{b64}">
</video>
"""
else:
md = f"""
<video controls width=320 height=240 autoplay muted>
<source src="data:video/mp4;base64,{b64}">
</video>
"""
return md