Search code examples
pythonpygamecodecyt-dlp

pygame Error : Unknown WAVE format & mpg123_seek: Invalid RVA mode


So i was making an Audio Player, it downloads audio using YT-DLP and then plays it using pygame.mixer.

the main problem, i am getting is that pygame Won't Run that Audio instead it would Just give Errors for Different Audio Formats like Wav, Ogg, and Mp3.

For Wav Format I get this Error : pygame.error: Unknown WAVE format

For MP3 I get this Error : pygame.error: mpg123_seek: Invalid RVA mode. (code 12)

For Ogg I get this Error : pygame.error: Not an Ogg Vorbis audio stream

After Wondering with these things For a While, I think there is Something Wrong with the Codecs. Attaching images of 2 Audio Files below (One which is playable with pygame, And one which Gives Errors with it)

Working Audio File And Not Working one

For Reference

Here is the yt-dlp Code that i used to download the desired Audio File.

from  yt_dlp import YoutubeDL
(option,C1,C2,C3)=[dict(extractaudio=True,outtmpl='src/backend/temp_audio/%(title)s.mp3'),'https://youtu.be','https://youtube.com','https://www.youtube.com',]

def download_song(link):
        try:
            with YoutubeDL(option) as ydl:
                ydl.download([link])
        except:
            print("Error in Downloading : " + link )
            pass
            

Command to install the prerequisite : python -m pip install yt-dlp

Also for a quick Review, here is the link to the test Audio File i wanted to play .


Solution

  • Problem Solved

    After Asking Tons of people on Discord Servers of Pygame and yt-dlp.

    what i found?

    • pygame won't be able to play an Audio file with that Specs.
    • we can use yt-dlp with ffmpeg to modify the Audio for making it compatible with pygame.

    So How can we install ffmpeg :

    1. Download ffmpeg : (https://github.com/yt-dlp/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip)

    2. Extract the files and find ffmpeg.exe and ffprobe.exe inside the bin folder.

    3. Copy those two files to the same folder as your Script.

    Now

    After installing ffmpeg, now we need to have some changes in our yt-dlp's code in order to make it run with ffmpeg. Below is the New Code :

    from  yt_dlp import YoutubeDL
    
    option={'final_ext': 'mp3',
     'format': 'bestaudio/best',
     'postprocessors': [{'key': 'FFmpegExtractAudio',
                         'nopostoverwrites': False,
                         'preferredcodec': 'mp3',
                         'preferredquality': '5'}],
     'outtmpl': 'src/backend/temp_audio/%(title)s.%(ext)s',
     'ffmpeg_location': 'YOUR-LOCATION/ffmpeg.exe'}
    
    def download_song(link):
            try:
                with YoutubeDL(option) as ydl:
                    ydl.download([link])
            except:
                print(Exception)
                pass
    

    Now your Audio Files would Work Perfectly With pygame!