1

I use Python to play a short mp3 audio file. But the playing speed is faster than other music players.

My Code:

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)

Please help me to solve this issue!

MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
yi wei
  • 11
  • 3

2 Answers2

0

This will work:

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)
-1
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()
Christopher Graf
  • 1,929
  • 1
  • 17
  • 34