2017-09-10 7 views
1

J'utilise le module VLC en python pour lire de la musique téléchargée avec un convertisseur youtube en mp3 pour un projet. Le problème est, j'ai découvert que ces fichiers n'aiment pas coopérer et Python finit par jeter une charge de rouge sur moi. Je crois que c'est parce que le fichier mp3 n'a pas de balises, donc quand le module les vérifie et qu'elles n'existent pas, il me renvoie une erreur. Voici mon code:Module VLC Python ne fonctionnant pas avec aucune balise

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") 

Voici la sortie "Très 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. 

Il est beaucoup plus d'erreurs que cela, mais StackOverflow me limite de les mettre tous. Ils sont tous pareils, de toute façon.

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 

est ici le téléchargeur, si elle est nécessaire:

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") 

Répondre

0

J'ai essayé votre code sur ma machine et il fonctionne. Je pense que vous utilisez une version obsolète ou une bibliothèque obsolète. Alors essayez le code ci-dessous, et laissez-moi savoir si vous trouvez une solution plus fiable ....

sudo pip uninstall vlc 
sudo pip uninstall python-vlc 
sudo pip install python-vlc 
+1

J'ai résolu le problème en utilisant un fichier WAV au lieu d'un fichier MP3. Tout va bien maintenant, mais merci quand même. –

+0

@GamingWithAltitude vous êtes les bienvenus. –