Search code examples
pythonmp3vlctaglib

Python VLC module not working with no tags


I am using the VLC module in Python to play some music downloaded with a YouTube to MP3 converter for a project. The problem is, I have discovered that these files don't like to cooperate and Python ends up throwing a load of red at me. I believe this is because the MP3 file has no tags, so when the module checks for them and they don't exist it throws an error at me. Here is my code:

import vlc
import os
import time

def playtrack(fileid, songname):
    filename = fileid + ".mp3"
    print('Now playing: ' + songname)
    music = vlc.MediaPlayer(filename)
    music.play()
    print(music.get_state())
    time.sleep(60)
    print("Finished.")
    #os.remove(filename)

playtrack("tVj0ZTS4WF4", "foo")

Here is the output "Very long...":

Now playing: foo
State.NothingSpecial
Warning: option --plugin-path no longer exists.
Warning: option --plugin-path no longer exists.
TagLib: MPEG::Header::parse() -- The next frame was not consistent with this frame.
TagLib: MPEG::Header::parse() -- The next frame was not consistent with this frame.

There is WAY more errors than this, but Stack Overflow limits me from putting them all. They're all the same, anyway.

TagLib: MPEG::Header::parse() -- The next frame was not consistent with this 
TagLib: MPEG::Header::parse() -- Invalid MPEG version bits.
TagLib: MPEG::Header::parse() -- Invalid MPEG layer bits.
TagLib: MPEG::Header::parse() -- Invalid MPEG version bits.
TagLib: MPEG::Header::parse() -- Invalid bit rate.
TagLib: MPEG::Header::parse() -- Invalid MPEG version bits.
TagLib: MPEG::Header::parse() -- The next frame was not consistent with this frame.
TagLib: MPEG::Header::parse() -- Invalid bit rate.
TagLib: MPEG::Header::parse() -- Could not read the next frame header.
TagLib: MPEG::Header::parse() -- The next frame was not consistent with this frame.
TagLib: MPEG::Properties::read() -- Could not find a valid first MPEG frame in the stream.
Finished.

Process finished with exit code 0

Here's the downloader, if it's necessary:

import youtube_dl
import os


class InvalidURL(Exception):
    pass


class SongExists(Exception):
    pass


def download(url):
    try:
        options = {
            'format': 'bestaudio/best',
            'extractaudio': True,  # only keep the audio
            'audioformat': "mp3",  # convert to wav
            'outtmpl': '%(id)s.mp3',  # name the file the ID of the video
            'noplaylist': True,  # only download single song, not playlist
        }
        with youtube_dl.YoutubeDL(options) as ydl:
            r = ydl.extract_info(url, download=False)
            if os.path.isfile(str(r["id"])):
                raise SongExists('This song has already been requested.')
            print("Downloading" + str(r["title"]))
            ydl.download([url])
            print("Downloaded" + str(r["title"]))
            return r["title"], r["id"]
    except youtube_dl.utils.DownloadError:
        raise InvalidURL('This URL is invalid.')

if __name__ == "__main__":
    download("https://www.youtube.com/watch?v=tVj0ZTS4WF4")

Solution

  • I have tried your code on my machine and it works. I think you are using outdated version or outdated library. So try code at below, and let me know if you figure out a more reliable solution....

    sudo pip uninstall vlc
    sudo pip uninstall python-vlc
    sudo pip install python-vlc