I'm using Flask for a web app. My frontend records some audio and sends it to a Flask route. In the route I am saving a wav audio file to an uploads folder:
# Save the audio file to a unique file
filename = f"audio_{datetime.now().strftime('%Y%m%d%H%M%S')}{extension}"
audio_path = os.path.join('uploads', filename)
audio_file.save(audio_path)
print(f"Saved audio file to {audio_path}.")
When I play the file in the uploads folder, it appears to have been saved correctly and the duration of the audio is 3 seconds long.
Then, I immediately verify the duration of the audio file using a function with ffprobe:
# Verify the duration of the original WAV file
if extension == '.wav':
print("Verifying the duration of the original WAV file...")
duration_before_conversion = get_audio_duration(audio_path)
print(f"Original WAV file duration: {duration_before_conversion} seconds")
And it prints that the file is just under 1 second long (it's always within 0.05 seconds of 1 second).
Wondering what could be causing it to say that the file is 1 second long when it's definitely 3 seconds in the uploads folder?
When I try to use the audio file after that (to convert it to a different sample rate), it is only 1 second long.
When I use ffprobe from the terminal to show the duration of the file, it says ~0.99 seconds, even though, when I download the file, it is definitely 3 seconds long.
EDIT: I have a theory that the frontend is passing the audio to the backend with incorrect metadata. Interestingly, it's only a problem on iOS and not on Windows and Android. Has anyone else come across this problem on iOS? This is the function that creates the AudioBlob and sends in to the backend:
EDIT: I tried downloading the audio file and then checking it's length locally with ffprobe. It gave the correct audio duration.
I then tried the same ffprobe command on the file on my production server and it gave the wrong audio duration.
I then compared the MD5 hashes of those two files and they are identical.
Turns out that my production server (Python Anywhere) is running a different version of ffprobe to my local server which might explain the difference? Will report back.