繁体   English   中英

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

[英]How to modify incorrect mp3 song length in python

我正在使用 mutagen 修改文件的元数据:“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中存储的信息。 如何将此数据存储到文件中?

很久以前就有人问过这个问题,但我发现自己遇到了同样的问题。

经过一番大量的谷歌搜索,我找到了这个答案,它使用ffmpeg编码器来修复不正确的元数据。

这是一个解决方案,希望可以节省一些时间。


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

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

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

要检查您是否已经有ffmpegffmpeg -version在命令行上运行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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM