For files returned by st.file_uploader, how can I get the "modified date"?

I am uploading several audio recording files every day into my test app. (bird sounds)
Not all recorders include the time of the recording in the name.
This is useful because bird activities depend on time!

Therefore, I hoped to retrieve the “modified datetime” of the files for this purpose.
Unfortunately, I was not able to do that.
And I guess the file metadata are probably not included in the upload.

Are these metadata, especially the “modified date” completely out of reach within streamlit?
Or would they be available somehow / somewhere?

Thanks for your suggestions

Michel

Hi @maajdl,

Thanks for posting! Streamlit itself does not have a feature to extract the timestamps but you can find workarounds using Python to preprocess the audio files before uploading them. You can basically run all your audio files through this script as part of your pipeline to make sure they’re all timestamped correctly.

You can use the Mutagen module to handle the audio metadata before uploading them. Here’s example code to get you started:

import os
import mutagen
from mutagen.mp3 import MP3
import time
import streamlit as st

# Folder containing audio files
audio_dir = 'path/to/audio/files' 

for filename in os.listdir(audio_dir):
    if filename.endswith('.mp3'):
        
        # Open audio file with Mutagen
        audio = MP3(os.path.join(audio_dir, filename))
        
        # Extract metadata
        mod_time = os.path.getmtime(os.path.join(audio_dir, filename))
        mod_time_str = time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime(mod_time))
        
        # Construct new file name
        name, ext = os.path.splitext(filename)
        new_name = f'{name}_{mod_time_str}{ext}'
        
        # Rename file
        new_path = os.path.join(audio_dir, new_name)
        os.rename(os.path.join(audio_dir, filename), new_path)

Let me know if this is helpful.

2 Likes

I did the same when I also faced the same problem related to the timestamp of the uploaded file

1 Like

Did it work for you?

Yes, but i didn’t use the mutagen. Just use the time module for timestamp

2 Likes

Awesome! Thanks for sharing your approach.

Thanks for the suggestions.
I always use wav files.
However, I have two types of recordes.
One specifies the date and time in the name of the files, but has no metadata.
The other has a counter-type filenae, but contains metadata.
I could use wavinfo or some other package to read these metadata.
But I am left with two different methods of doing the job.
An in addition, I fear that metadata could have different formats dependong on the recorder.
Therefore, indeed, it is much better that I assume a filename contains the date and time in a stabdard way. And that I write a small program to name the files with the method suited for the different recorders.
Thanks again

Could you sketch how you did?
Thanks