Imagine using Spotify - Nerding Out Episode 1
Tags: python, nerds, vlc
Published: 2021-07-18 11:48:20

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

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

![Environment Variables](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pqab2jxyhnq3stfomi55.png)

Or alternatively, you can open it from the command line

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

![Edit it lol](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5veybbkw9oxtwpfkq8gj.png)

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

![🚀🚀](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0fwmen3t4kurf9wwprrj.png)
 
Then close the window

5. 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) 

```py
import os
import random
```


2. 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


3. 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
```


4. Read the file

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

```py
file = open('playlist.txt', 'r').read().split('\n')
```
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

```py
['https://www.youtube.com/watch?v=dQw4w9WgXcQ', 'https://www.youtube.com/watch?v=QH2-TGUlwu4', 'https://www.youtube.com/watch?v=w0AOGeqOnFY']
```


5. Now we shuffle the songs
This wont be hard we just use

```py
random.shuffle(file)
```
Omit this line if you don't like your playlist shuffled

6. 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

```py
videos = ''
for i in file:
    videos+= f"{i} "
```
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
```
As you can see, all of them are in a single list

7. Running VLC
Now we execute the command that loads vlc with your playlist
This statement does the job

```py
os.system(f'vlc --no-video {videos}')
```

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

8. Running it

Execute the file and enjoy your playlist

## Full code

this is all the code we discussed here

```py
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}')
```

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

## Credits
Credits to [this blog from vlchelp](https://www.vlchelp.com/add-vlc-command-prompt-windows/) 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