首页 > 解决方案 > 从 M4A 转换时 MP3 的显示长度加倍

问题描述

我编写了一个 Python 脚本,它收集 .m4a 文件(艺术家、标题等)的元数据,将 .m4a 转换为 .mp3,然后将元数据保存到新的 .mp3。

当我在 iTunes 中播放新的 MP3 时,曲目的显示长度是 M4A 版本的两倍。(正常轨道长度为 3:37)。见下图:

iTunes 音轨长度裁剪

有趣的是,当 7:14 长的曲目到达 3:37 时,歌曲正常结束,跳过剩余显示时间,转到下一首歌曲。但是,当我使用下图所示的小部件单击曲目中的各个位置(包括超过 3:37 的时间戳)时,它似乎在歌曲中的相对点播放,而不是什么都不播放。(例如,拖到 7:00 并点击“播放”播放歌曲的正常结尾,与原始 M4A 的 ~3:23 时间戳播放的内容相同)。很难描述……希望这是有道理的。

我的代码在这里:

from pydub import AudioSegment
from tinytag import TinyTag
from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3
import mutagen

song_directory = "/Users/mgermaine93/Desktop/Test-Music/"
song_name = "03 Dylan Thomas.m4a"
song_name_minus_file_type = song_name[0:-4]
song_file_type = song_name[-4:]
song_full_name = f"{song_directory}{song_name}"

# This grabs the track information from the existing file before it is converted
mp3 = TinyTag.get(f"{song_full_name}")

album = mp3.album
# album_artist = mp3.albumartist
title = mp3.title
artist = mp3.artist
composer = mp3.composer
genre = mp3.genre
year = mp3.year
track_number = mp3.track
track_total = mp3.track_total
disc_number = mp3.disc
disc_total = mp3.disc_total
duration = mp3.duration
print(f"This is the duration of the m4a: {duration}")

# This part does the conversion from m4a to mp3
m4a_audio = AudioSegment.from_file(
    f"{song_full_name}", format="m4a")
m4a_audio.export(
    f"{song_directory}{song_name_minus_file_type}.mp3", format="mp3")

# This assigns the m4a track field values to the new mp3
mp3 = EasyID3(f"{song_directory}{song_name_minus_file_type}.mp3")

mp3['album'] = album
mp3['title'] = title
mp3['artist'] = artist
mp3['composer'] = composer
mp3['genre'] = genre
mp3['date'] = year
EasyID3.RegisterTextKey('track total', 'TRCK')
mp3['track total'] = f"{track_number}/{track_total}"
EasyID3.RegisterTextKey('disc total', 'TPOS')
mp3['disc total'] = f"{disc_number}/{disc_total}"
mp3.save(v2_version=3)

# Get the properties of the mp3
new_song = TinyTag.get(f"{song_directory}{song_name_minus_file_type}.mp3")

new_duration = new_song.duration
print(f"This is the duration of the mp3: {new_duration}")

脚本的当前输出是:

This is the duration of the m4a: 216.85115646258504
This is the duration of the mp3: 216.94245058379644

据我所知,这个问题似乎不会影响任何有意义的事情。而且我确信有更有效的方法来执行文件转换,但我所写的内容可以满足我的所有需求。(除了上面输出中的持续时间值略有不同之外,两个文件的元数据是相同的。)我只是对为什么显示长度与元数据所说的持续时间不同感到困惑。

预先感谢您的任何帮助。

标签: pythonmp3id3m4amutagen

解决方案


推荐阅读