Passing an uploaded video file into an FFmpeg function

Summary

I have a working function that embeds a subtitle file into a video file using Python and FFmpeg. I would like to use that function within a streamlit app with files uploaded via st.file_uploader. When I try to run the process via an st.button, I get an error. I am new to streamlit. I was expecting to just pass the uploaded files from a variable to my function, but I must be missing a step in between.

Code snippet:

import ffmpeg
import streamlit as st

st.title("Caption Embedder")
st.markdown("-----")

video_filename = st.sidebar.file_uploader("Choose a video file", type=["mov", "mp4"])
if video_filename is not None:
    st.video(video_filename)
        
sub_filename = st.sidebar.file_uploader("Choose a caption file", type=["srt", "txt"])
if sub_filename is not None:
    st.write(sub_filename)
        
start_button = st.sidebar.button("Start")

def embed_srt(video_filename, sub_filename):
    video = ffmpeg.input(video_filename)
    audio = video.audio
    ffmpeg.concat(video.filter("subtitles", sub_filename), audio, v=1, a=1).output(video_filename + "output.mp4").run()

if start_button:
    embed_srt(video_filename, sub_filename)

Expected behavior:
A new video file is output that has the subtitles embedded in the the original file.

Actual behavior:
TypeError: unsupported operand type(s) for +: β€˜UploadedFile’ and β€˜str’

Debug info

  • Streamlit version: 1.17.0
  • Python version: 3.11.1
  • OS version: macOS 11.7
  • Browser version: Safari

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.