Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 18, 2021 11:48 am GMT

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 osimport random
  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=dQw4w9WgXcQhttps://www.youtube.com/watch?v=QH2-TGUlwu4https://www.youtube.com/watch?v=w0AOGeqOnFY
  1. Read the file

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

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

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']
  1. Now we shuffle the songsThis wont be hard we just use
random.shuffle(file)

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

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

  1. Running VLCNow we execute the command that loads vlc with your playlistThis statement does the job
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

  1. Running it

Execute the file and enjoy your playlist

Full code

this is all the code we discussed here

import osimport randomfile = open('playlist.txt', 'r').read().split('
') random.shuffle(file) # Shuffle the Playlist## Add all hyperlinks to a single stringvideos = ''for i in file: videos+= f"{i} "## Run it allos.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 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


Original Link: https://dev.to/akionsight/imagine-using-spotify-nerding-out-episode-1-32f4

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To