0

I am using the python vlc library to make a music player using pygame. I download all of the songs into mp3 format and then run some vlc code to play it. Here it is:

import pygame, sys
from pygame import mixer
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((300, 300))

import vlc
current = vlc.MediaPlayer('audio files/1.mp3')

while True:
    screen.fill((0, 0, 0))

    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_SPACE:
                current.pause()
            if event.key == K_a:
                current.play()

    pygame.display.update()

The problem is that vlc only seems to play songs 1 and 0(shown in the image of the file where I keep the songs) and doesn't play the others. Song 1 or 2 aren't the one with the lowest size and were downloaded in exactly the same way as all of the others.

so doing current = vlc.MediaPlayer('audio files/1.mp3') will work and play the song, but doing current = vlc.MediaPlayer('audio files/2.mp3') won't play the song.

Could anybody possibly explain why it is doing this and help me out? Thanks in advance.

Here's the directory where I keeep all of the songs enter image description here

And here is the code that I used to download everything:

import pafy, sys
import pandas as pd 
import requests
import urllib.request
import csv
from PIL import Image
import youtube_dl


links =  ["https://www.youtube.com/watch?v=HnIdtbV_TDU&list=PLjT6ePOFLFf3gHO_fXXmikcipOV3ZLYB0&index=1",
          "https://www.youtube.com/watch?v=zkggvNaUArQ&list=PLjT6ePOFLFf3gHO_fXXmikcipOV3ZLYB0&index=4",
          "https://www.youtube.com/watch?v=CY8_Q5Wd5Sw&list=PLjT6ePOFLFf3gHO_fXXmikcipOV3ZLYB0&index=5",
          "https://www.youtube.com/watch?v=AzEx8VFMu_s&list=PLjT6ePOFLFf3gHO_fXXmikcipOV3ZLYB0&index=7",
          "https://www.youtube.com/watch?v=XDpoBc8t6gE&list=PLLD1frN1SkrxbVhtho-xMIPSzWSNz53Zc&index=2"
          ]


def cut_playlist(link):
    cut_off = link.find("list")-1
    return link[0:cut_off]

links = [cut_playlist(x) for x in links]

def get_mp3(url, name):
    video_info = youtube_dl.YoutubeDL().extract_info(
        url=url,
        download=False
        )

    options = {
    'format':'bestaudio/best',
    'keepvideo':False,
    'outtmpl':f"audio files/{name}.mp3",
    'noplaylist':True,
    'ignoreerrors':True}

    with youtube_dl.YoutubeDL(options) as ydl:
        ydl.download([video_info['webpage_url']])


with open("data.csv", "w") as f:
    writer = csv.writer(f)

    writer.writerow(["thumbnail index", "audio index", "title"])

    for link in links:
        pafy_object  = pafy.new(link)

        get_mp3(link, str(links.index(link))) # DOWNLOADS THE MP3

        tn = pafy_object.bigthumbhd
        title = list(pafy_object.title)
        title = ''.join([x for x in title if (x.isdigit() or x.isalpha() or x==' ')])

        urllib.request.urlretrieve(tn, f"thumbnails/image{links.index(link)}.png")
        im = Image.open(f"thumbnails/image{links.index(link)}.png")

        #will be in the form of [thumbnail index, audio index, name]
        writer.writerow([f"thumbnails/image{links.index(link)}.png", links.index(link), title])
        width, height = im.size
        left, top, right, bottom = 0, 50, width, height-50
        im1 = im.crop((left, top, right, bottom))
        im1.save(f"thumbnails/image{links.index(link)}.png")


  • 1
    did you test these files directly in VLC? Maybe it can't play it even if you use VLC directly. Maybe they are broken. – furas May 28 '22 at 08:18
  • to get index of element on list you can use `for index, link in enumerate(links):` instead of `links.index(link)` – furas May 28 '22 at 08:25
  • 1
    thanks for the help. Testing it in vlc made me realise what the actual problem was :) – Percy Lorello May 28 '22 at 09:40

1 Answers1

2

Ok so I figured it out. Basically I had the ignoreerrors variable set to true in the options dict which I then fed into the youtube-dl downloader. It ignored the download errors that were recieved for videos 2, 3, 4 therefore downloaded faulty audio which wouldn't play. The solution is just to properly download all of the audio and not have ignoreerrors set to true.