Here’s my use case:
I want a user to be able to upload files in Streamlit, I will perform some edits on the files, then the user should be able to download the result file.
The only file-format I’m having troubles with, is mp3.
I am able to get the uploaded file, strips the tags with Mutagen, and end up with an object of class mutagen.mp3.MP3.
In the end, I want to be able to send this modified mp3 file to a ZipFile (since the user could upload multiple files and then also needs to be able to download multiple files), but I can’t get it to work. Neither to play it directly in the browser, download it ‘as is’, etc. Any ideas would be greatly appreciated!
The idea is like in the next section (can’t share the complete code unfortunately) and I either get the message ‘mutagen.mp3.MP3 is Invalid binary data format’ or I get a ZipFile with an mp3 (not in the code below), but the mp3 has 0 bytes after conversion to byte-like object with io.BytesIO.
PS the complete code works perfectly on local files, where files can be read from and written back to disk. Unfortunately I can’t provide that for the users of this app.
Tried something in the line of this:
def remove_metadata_from_mp3(self, mp3_path):
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
tmp_file.write(mp3_path.getvalue())
#added this, because mutagen doesn't seem to work with a file-like object, if I just do audio =
mutagen.File(mp3_path), it breaks on audio.delete()
audio = mutagen.File(tmp_file.name)
audio.delete() #to delete tags
audio.save()
return audio
input_files = st.file_uploader("Upload file(s)", accept_multiple_files=True)
if input_files:
process_files(input_files)
def process_files(file_paths):
for file_path in file_paths:
resultfile = remove_metadata_from_mp3(file_path)
st.download_button(label='Download mp3', data=resultfile, file_name = mp3_path.name)
#with other files (like docx / xlsx) I added buffer=io.BytesIO() and resultfile.save(buffer) to get a Byteslike object