如何在python中修改不正确的mp3歌曲长度

问题描述 投票:2回答:1

我正在使用诱变剂来修改文件的元数据:“ temp.mp3”。

歌曲长3:00。

当我尝试:

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

我得到以下输出:

424.791857143
180
424.791857143

所以看来mp3的save方法没有记录我存储在info.length中的信息。如何将该数据存储到文件中?

python mp3 mutagen
1个回答
0
投票

这是很久以前问过的,但我发现自己也遇到了同样的问题。

经过大量Google搜索之后,我发现this answer使用ffmpeg编码器修复了错误的元数据。

这是一种希望可以节省某人时间的解决方案。


我们可以使用ffmpeg复制文件并使用以下命令自动修复错误的元数据:

ffmpeg -v quiet -i "sound.mp3" -acodec copy "fixed_sound.mp3"

-v quiet修饰符防止它将命令的详细信息打印到控制台。

要检查您是否已有ffmpeg,请在命令行上运行ffmpeg -version。 (如果没有,您可以从此处下载:https://ffmpeg.org/


我在下面写了一个应该可以解决问题的函数!

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)

© www.soinside.com 2019 - 2024. All rights reserved.