【问题标题】:Pygame plays a mp3 file faster than normal speedPygame 播放 mp3 文件的速度比正常速度快
【发布时间】:2017-09-17 07:37:05
【问题描述】:

我使用 Python 播放一个简短的 mp3 音频文件。但播放速度比其他音乐播放器快。

我的代码:

import pygame
import time
pygame.mixer.init(frequency=22050, size=16, channels=2, buffer=4096)
file=r'test.mp3'
try:
    pygame.mixer_music.load(file)
    pygame.mixer_music.play()
    while pygame.mixer_music.get_busy():
        time.sleep(0.1)
except Exception as err:
    print(err)

请帮我解决这个问题!

【问题讨论】:

标签: python pygame mp3


【解决方案1】:

这将起作用:

import time, os
from pygame import mixer
from pydub import AudioSegment
# original file: "slow.mp3"
# new fast file: "fast.mp3"
# speed: 1.3 (1.0 is actual speed)

def speed_swifter(sound, speed=1.0):
    sound_with_altered_frame_rate = sound._spawn(sound.raw_data, overrides={"frame_rate": int(sound.frame_rate * speed)})
    return sound_with_altered_frame_rate
newSpeed = 1.3
sound = AudioSegment.from_file("slow.mp3")    
speed_sound = speed_swifter(sound, newSpeed)
speed_sound.export(os.path.join("fast.mp3"), format="mp3")
mixer.init()
mixer.music.load("fast.mp3")
mixer.music.play()
while mixer.music.get_busy():
    time.sleep(0.1)

【讨论】:

    【解决方案2】:
    import pygame, mutagen.mp3
    
    song_file = "your_music.mp3"
    
    mp3 = mutagen.mp3.MP3(song_file)
    pygame.mixer.init(frequency=mp3.info.sample_rate)
    
    pygame.mixer.music.load(song_file)
    pygame.mixer.music.play()
    

    【讨论】:

      猜你喜欢
      • 2019-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多