DEV Community

Cover image for Imagine using Spotify - Nerding Out Episode 1
AkIonSight
AkIonSight

Posted on

Imagine using Spotify - Nerding Out Episode 1

Well yeah why pay for spotify if can use VLC media player, a text file, a few youtube links and a small python script to listen to your playlist. Why? cuz you know thats the stuff I love to do and if this doesnt interest you, hey youre getting a Ad-Free Music Player without anyone collecting your data. And yeah all of this stuff is open-source, even the libraries i am using too

Skip to the end if you just want the code

Getting everything

You will need some stuff to get this project done, but I guess you probably have some of it or all of it already on your machine. Ok so this is the stuff you will need

  1. VLC Media Player (aka VideoLAN)
  2. Python (I made this on python 3.9 but this is pretty small and should work in anything above python2)
  3. The Youtube links of all your playlist songs (but you can use any link that works with VLC)

Wait a sec, why VLC? I mean there are a lot of options?

Yeah lemme tell you. Imagine you know you use a custom music player that you are using to play songs at a party and then your non-existent friend asks, "hey wheres the equiliser" or "hey i wanna see the music video" or even "hey can you add subtitles" and then you don't have any answer because you didn't code that part out. VLC will help you in those situations, its pretty lightweight but it will help you get low-level customization done

Also im lazy, let VLC do the hard part

Lets go

First I assume you already have VLC on your computer. Also I assume you are on Windows. Now the thing is that VLC ships with command line tools too. To enable these you need to add the path to these command line tools in the PATH environment variable.

To do so:

  1. Open Program files or Program Files(x86) and find the vlc folder or just use these locations

C:\Program Files\VideoLAN\VLC\
or
C:\Program Files (x86)\VideoLAN\VLC\

if these folders exist, you can move ahead

  1. Open windows search and type "edit environment variables". Something like this should pop up, click on it

Environment Variables

Or alternatively, you can open it from the command line

  1. Now select PATH and click edit. Something like this

Edit it lol

  1. Now click on new and then add the Program Files or Program Files(x86) for VLC. something like this. then press "OK"

🚀🚀

Then close the window

  1. Test it out. to test this, open a new command prompt window and type vlc and check if any error is raised. If successful it should open vlc media player

Part 2: Script Time

Create a python file, anywhere really and open it in your favorite code editor

  1. Import some modules we dont really need a lot, and none from outside the stdlib, we need os and random but random is optional (we will use it for shuffling songs but some people dont like it)
import os
import random
Enter fullscreen mode Exit fullscreen mode
  1. Create the playlist.txt file

This is where all hyperlinks will be stored. you can execute copy NUL playlist.txt to create this file too

  1. Add some test links to the file

Add a few songs from your playlist, here are some to help you out

https://www.youtube.com/watch?v=dQw4w9WgXcQ
https://www.youtube.com/watch?v=QH2-TGUlwu4
https://www.youtube.com/watch?v=w0AOGeqOnFY
Enter fullscreen mode Exit fullscreen mode
  1. Read the file

to do so write a simple read mode open statement, something like this

file = open('playlist.txt', 'r').read().split('\n')
Enter fullscreen mode Exit fullscreen mode

Here we also split the file on each newline hence creating a list, so each link must be on a separate line

printing the output of file we should get something like this

['https://www.youtube.com/watch?v=dQw4w9WgXcQ', 'https://www.youtube.com/watch?v=QH2-TGUlwu4', 'https://www.youtube.com/watch?v=w0AOGeqOnFY']
Enter fullscreen mode Exit fullscreen mode
  1. Now we shuffle the songs This wont be hard we just use
random.shuffle(file)
Enter fullscreen mode Exit fullscreen mode

Omit this line if you don't like your playlist shuffled

  1. Appending all values to a single string

Now we append all values of that list from above. We will see the importance of it below. We do it using this for loop

videos = ''
for i in file:
    videos+= f"{i} "
Enter fullscreen mode Exit fullscreen mode

printing the output of videos we get something like this

https://www.youtube.com/watch?v=w0AOGeqOnFY https://www.youtube.com/watch?v=QH2-TGUlwu4 https://www.youtube.com/watch?v=dQw4w9WgXcQ
Enter fullscreen mode Exit fullscreen mode

As you can see, all of them are in a single list

  1. Running VLC Now we execute the command that loads vlc with your playlist This statement does the job
os.system(f'vlc --no-video {videos}')
Enter fullscreen mode Exit fullscreen mode

we use the --no-video flag because we are listening to songs, not seeing their YouTube videos. This should also cut down on bandwidth usage

  1. Running it

Execute the file and enjoy your playlist

Full code

this is all the code we discussed here

import os
import random

file = open('playlist.txt', 'r').read().split('\n') 

random.shuffle(file) # Shuffle the Playlist

## Add all hyperlinks to a single string
videos = ''
for i in file:
    videos+= f"{i} "

## Run it all
os.system(f'vlc --no-video {videos}')
Enter fullscreen mode Exit fullscreen mode

Find the full code here https://github.com/akionsight/Imagine-Using-Spotify

Credits

Credits to this blog from vlchelp telling me about how to add VLC to path

End Notes

This is the first episode of this series, hope you like it and please tell me in the comments if you liked this blog or not.

Thanks, cya

Top comments (3)

Collapse
 
pybash profile image
PyBash

nice this is gonna be useful, hope to see more such

Collapse
 
soulninjadev profile image
SoulNinja-dev

very nice

Collapse
 
akionsight profile image
AkIonSight

Thanks