This was asked a long time ago but I found myself having the same problem.
After some heavy Google searching, I found this answer which uses the ffmpeg encoder to fix the incorrect metadata.
Here's a solution that hopefully saves someone some time.
We can use ffmpeg to copy the file and automatically fix the faulty metadata using this command:
ffmpeg -v quiet -i "sound.mp3" -acodec copy "fixed_sound.mp3"
The -v quiet
modifier prevents it from printing the details of the command to the console.
To check if you already have ffmpeg, run ffmpeg -version
on your command line. (If not, you can download it from here: https://ffmpeg.org/)
I wrote a function below that should do the trick!
import os
def fix_duration(filepath):
## Create a temporary name for the current file.
## i.e: 'sound.mp3' -> 'sound_temp.mp3'
temp_filepath = filepath[ :len(filepath) - len('.mp3')] + '_temp' + '.mp3'
## Rename the file to the temporary name.
os.rename(filepath, temp_filepath)
## Run the ffmpeg command to copy this file.
## This fixes the duration and creates a new file with the original name.
command = 'ffmpeg -v quiet -i "' + temp_filepath + '" -acodec copy "' + filepath + '"'
os.system(command)
## Remove the temporary file that had the wrong duration in its metadata.
os.remove(temp_filepath)