【问题标题】:Playing MP3s in Python - Automatically handling varied sampling frequencies在 Python 中播放 MP3 - 自动处理不同的采样频率
【发布时间】:2023-03-12 07:25:02
【问题描述】:

我正在编写一个 python 程序来练习和测试我的普通话口语理解能力。它从我指定的目录中选择一个随机的 mp3 并播放它(然后做更多的事情)。我正在使用 pygame 播放这些 mp3,但我的问题是我当前的设置需要明确声明 mp3 的采样频率才能正常播放。但是,我混合了 48 kHz 和 44.1 kHz 的 mp3,希望能够在不失真的情况下播放它们。

import pygame
import random
import os

filenames = [x[:-4] for x in os.listdir(filepath) if x.endswith(suffix)]

pygame.mixer.init(48000, -16, 2, 4096)
selected_filename = random.choice(filenames)
selected_filename_full = filepath + selected_filename + suffix 
pygame.mixer.music.load(selected_filename_full)
pygame.mixer.music.set_volume(volume)
pygame.mixer.music.play()

有没有办法检测 mp3 的采样频率?或者以某种其他方式正确播放 mp3?这似乎是一个奇怪的问题,每当我双击一个 mp3 时,我的音乐播放器总是会正常播放,那么我该怎么做才能在我的 python 代码中获得相同的行为呢?

【问题讨论】:

    标签: python pygame mp3 frequency sampling


    【解决方案1】:

    你可以使用eyeD3:

    import eyed3
    e = eyed3.load(filename)
    print e.info.sample_freq
    

    【讨论】:

      猜你喜欢
      • 2013-04-05
      • 2017-07-24
      • 2017-03-04
      • 2015-10-05
      • 2013-04-05
      • 1970-01-01
      • 2011-07-05
      • 1970-01-01
      • 2014-12-04
      相关资源
      最近更新 更多