我正在使用mutagen来修改文件的元数据:“temp.mp3”。
这首歌是3点长。
当我尝试:
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中存储的信息。如何将此数据存储到文件中?
答案 0 :(得分:0)
很久以前有人问过这个问题,但我发现自己也遇到了同样的问题。
经过大量Google搜索后,我发现doc使用 ffmpeg 编码器修复了错误的元数据。
这是一种希望可以节省某人时间的解决方案。
我们可以使用 ffmpeg 复制文件并使用以下命令自动修复错误的元数据:
ffmpeg -v quiet -i "sound.mp3" -acodec copy "fixed_sound.mp3"
-v quiet
修饰符可防止其将命令的详细信息打印到控制台。
要检查您是否已经拥有 ffmpeg ,请在命令行上运行ffmpeg -version
。 (否则,您可以从此处下载:this answer)
我在下面写了一个应该可以解决问题的函数!
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)