A few days ago, it was pointed to me that my website was featured on the S-Config Webrings page and I was super happy to find that out. However, I was very disappointed in myself upon reading something there about my site:
There are sections that break rather easily on his site as some of his media links back to YouTube which bars a person (depending on the country) from viewing
As this was a very serious topic, especially since I’ve spoken about web accessibility five months ago, I’ve decided that something needs to be done. After trying to fiddle with the some Invidious installations here and there and playing cat and mouse for a few days when instances raise and fall faster than bitcoin prices, I decided that using Invidious will be an endless cat-and-mouse game that I don’t want to play, because it’s very time consuming and gives me no benefits. However, as I understand that the issues presented there are valid, so I decided to write my own Google-less Youtube-ish embeds.
1. Getting all the Youtube Embeds From My Site
Luckily, I’m smart and I use hugo’s built-in system of of shortcodes, so whenever I need to embed a clip, in a post or a quickie post, I’d just use the code
The first line just defines the ID of the video to being used and an optional title, if I decide to use the fancier key=“value” variant of the shortcode and the variables makes sure both cases are taken care of. Then I’m just displaying the thumbnail photo with a cute icon on top, which on click loads the default Youtube embed iframe. If your javascript is off, then it just opens the video in a new tab/page. If you block youtube completely, you can copy the video ID displayed at the bottom and just paste it into your favourite Invidious instance.
Update
I had to split the $video string after the ? character in order to use it as the image thumbnail, as sometimes I’m linking videos to a certain timestamp (like yGXxawJdviE?t=17) and that would break the thumbnail display. I did that by simply using this snippet in hugo in the area where I’m displaying the video thumbnail:
Additionally, I’m using Youtube as provider for trailers in the movies section, as well for music embeds on the records pages. These don’t use the shortcode per se, but they use a similar HTML and the Youtube IDs are added to each using a front-matter parameter, so I’ve just written a PHP script that searches all my files in the content/ directory and processes them. For the records, it’s a bit complex as it’s some arrays, so I’m just using thise snippet of code which is magic:
1
2
3
{{- range (where .Site.RegularPages.ByLastmod.Reverse "Type" "records") }}{{ range $v := .Params.video }}
{{ . }}
{{ end }}{{ end }}
2. The Functionality
Javascript (jQuery in my case), with a graceful degradation of performance in case your browser doesn’t support Javascript or it’s disabled, case in which it just opens the page in a new tab or window. Additionally, the iframe embed is loading from youtube-nocookie.com, so you won’t get pushed any tracking cookies (Google’s word, don’t trust it!).
So as soon as I find a video ID, I check if I already have the thumbnail file. If it’s there, I just skip it. Otherwise, I try to download the HD version, named “maxresdefault.jpg”. If it returns a 404, I try with the lower version, “hqdefault.jpg”, which is working for most videos. Youtube has being using always the same naming structure for thumnails (documented here), but I needed to download only these URLs: https://img.youtube.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg and https://img.youtube.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg.
Additionally, I could have saved somehow the titles of the videos to display them, and I could have also made a script to download and archive the video files themselves, but I would have just turned myself into a mirror of Youtube, which I have no desire or financial ability to do. However, using the trim($line) variable in my PHP script, I could theoretically make something like:
And that’s it, now all embeds display now enough stuff to be informative, without sending any of your requests to Google. The whole shebang took me about an hour and a half to set up, run and sync to the server and at the moment the images are taking only about 155 MB of data on the server, so that’s not a lot. Feel free to make the code above better and use it wherever and however you want!
PS: Upon writing this article, it seems that out of the 2169 youtube embeds I added to my posts over the years, at least 127 are not working anymore, either made private by their uploaders or removed by youtube entirely. Most likely those videos are lost forever.
Webmentions allow conversations across the web, based on a web standard. They are a powerful building block for the decentralized social web.
If you write something on your own site that links to this post, you can send me a Webmention by putting your article's URL in the form and it will appear in a list, below.