2

I am using mutagen to modify the metadata for a file: "temp.mp3".

The song is 3:00 long.

When I try:

from mutagen.mp3 import MP3
audio = MP3('temp.mp3')
print audio.info.length
audio.info.length = 180
print audio.info.length
audio.save()
audio = MP3('temp.mp3')
print audio.info.length

I get the following output:

424.791857143
180
424.791857143

So it appears that the save method of mp3 isn't recording the information I'm storing in info.length. How do I go about making this data store to the file?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Nezo
  • 567
  • 4
  • 18

1 Answers1

0

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)