Tuesday, February 27, 2024

Youtube Alternative

Recently, I was listening to a longish classical music piece on Youtube. Right in the middle of a movement, the video stops to show an ad. How annoying! I don't use Youtube very often, so I don't see the sense in buying Youtube Premium. However, for the few things that I do watch, I don't want the videos interrupted by ads in the middle. First, that should just be against the law, second, I wrote my own front end to Youtube -- no ads!


The parts are pretty simple, it's just search, display results, then show the selected video. I wrote it all in Java as that's what I'm most familiar with. The actual playing of videos is passed off to a local video player, both mpv and vlc work well for playing an http stream.


The complete code is here, what follows is the description of what it does.


Search on Youtube is just this URL:


https://www.youtube.com/results?search_query=search+words+here


The search results are mostly a lot of json, so a few regular expressions work well to extract the relevant content. In the code, the first regex pulls in those sections of the json that cover the videos, the subsequent regular expressions separate out the individual parts of each section. So a 'section' has the video ID, a url for a thumbnail, and a description of the video from the accessibility content. I display these with a button to play the video and a second button to download the video.


Getting a video from Youtube is just this URL;


https://www.youtube.com/watch?v=videoID


Both mpv and vlc will play an http stream from a url just by passing the url on the command line, like


mpv https://www.youtube.com/watch?v=Z8zk7XKyoE8


For the download, I'm using yt-dlp. I added a quick and dirty menu item to set the download directory. It's not a file chooser, you have to type in the path yourself, so yeah, not good. The command line for yt-dlp looks like this:

yt-dlp -P downloadDirectory https://www.youtube.com/watch?v=videoId


Here's a screenshot:




That's it. Quick and dirty, but useful. 

No comments: