/ YOUTUBE2AUDIOBOOK

Introducing Playlist Support

Need to download all videos in a playlist from YouTube? Then the improved YouTube2Audiobook has all you need! Readily available on GitHub.

Update 2020-10-18: We are proud to announce that YouTube2­Audiobook is now also available on Docker Hub. Listen sooner. Leverage the Docker container.

Get Smarter With YouTube2Audiobook!

While in reality only a very thin wrapper around the excellent youtube-dl open source project, the YouTube2Audiobook app has all I really need when it comes to scaling out my reading.

I can now fill, what would otherwise be dead or lost time, with sort-of “speed-reading” through the unread sections of My Reading List. If you want, then, in a sense,

Household activities make me smarter. Commuting makes me smarter. By listening to audiobooks from YouTube, downloaded via YouTube2Audiobook.

If nothing else, then I now have a way to turn otherwise recurring or tedious (domestic) tasks that only require a limited amount of my attention into exciting learning opportunities. I just have to remember to keep those dishes out of the washing machine…

Downloading a Playlist

But this also means that I need to keep up a constant supply of audiobooks to then keep me going. Downloading and converting individual YouTube videos into audiobooks via the app is usually sufficient.

But it’s even easier to leverage the great playlist feature built right into YouTube. After finding a suitable playlist in the first place, that is, of course.

Take the below “Genre: Science” playlist from the great LibriVox Audiobooks channel, for example. It happens to have Henri Poincaré, Science and Hypothesis on it that also features quite prominently in the General Next Reads section of my reading list (it’s the only one as of 2020-10-03 — feel free to send me an email suggesting additional ones).

Splendid! One more to listen to while making a cuppa or two! It’s easy enough to go ahead, grab the corresponding URL, and download the YouTube video as an audiobook via

yt2ab -u https://youtu.be/gmmgWcbAmjo

Eventually, the resulting file is downloaded onto my machine.

Manually Downloading a Playlist

But what if I also wanted to give the remaining items on the above playlist a go?

As of 2020-10-03, the playlist already has 32 entries. Manually going through every video and making a note of the corresponding URL isn’t really a viable, let alone, scalable solution. My time is limited. But my machine has ample time.

There has to be a better way…?! And there is!

Wrapping YouTube2Audiobook

As stated above, YouTube2Audiobook uses the excellent youtube-dl open source project under the bonnet.

The nice thing about youtube-dl is that it allows to extract the JSON describing a given playlist without downloading the videos in the playlist via optional parameters.

So, when applying this approach to the above “Genre: Science” playlist, we get

$ youtube-dl --dump-json --flat-playlist https://youtu.be/gmmgWcbAmjo?list=PLZ-bKJtH3G7BYYscYohAFZAq65RwCf-K-
{"url": "z5f2bpiAdLQ", "_type": "url", "ie_key": "Youtube", "id": "z5f2bpiAdLQ", "title": "The Fairyland of Science by Arabella B. BUCKLEY read by Various | Full Audio Book"}
{"url": "TGMGfCfpcqY", "_type": "url", "ie_key": "Youtube", "id": "TGMGfCfpcqY", "title": "The Extermination of the American Bison by William T. HORNADAY read by Various | Full Audio Book"}
{"url": "z5f2bpiAdLQ", "_type": "url", "ie_key": "Youtube", "id": "z5f2bpiAdLQ", "title": "The Fairyland of Science by Arabella B. BUCKLEY read by Various | Full Audio Book"}
...

when omitting all output beyond the third line (which we will also do for long terminal output for the remainder of this article). You get the idea.

A closer inspection of the output reveals that each line is a valid JSON document in itself — just as ordered. More importantly, each JSON document has all the information we need to individually process the corresponding video in the playlist; most crucially the URL where to find it.

We can extract the value stored in key url from the above results using jq. Written as a single command by leveraging pipes, this eventually yields

$ youtube-dl --dump-json --flat-playlist https://youtu.be/gmmgWcbAmjo?list=PLZ-bKJtH3G7BYYscYohAFZAq65RwCf-K- | jq -r '.url'
z5f2bpiAdLQ
TGMGfCfpcqY
z5f2bpiAdLQ
...

All that remains to do now, is to convert every URL into a valid YouTube URL and prepend it with a yt2ab invocation, resulting in

$ youtube-dl --dump-json --flat-playlist https://youtu.be/gmmgWcbAmjo?list=PLZ-bKJtH3G7BYYscYohAFZAq65RwCf-K- | jq -r '.url' | while read url ; do echo "yt2ab -u https://youtu.be/${url}" ; done
yt2ab -u https://youtu.be/z5f2bpiAdLQ
yt2ab -u https://youtu.be/TGMGfCfpcqY
yt2ab -u https://youtu.be/z5f2bpiAdLQ
...

So, with one (not so trivial) Bash command, we now have a method of extracting the links of all videos from a given playlist and converting them into a series of yt2ab invocations.

What did we gain? Well, for a playlist of size n, we’ve replace n manual steps of extracting the links to the corresponding videos by one invocation that generates n invocations of yt2ab.

It’s progress. But. There has to be a better way…?! And there is!

Playlist Support Built Into YouTube2Audiobook

The above idea has been fully integrated into the latest version of YouTube2Audiobook. Thus, removing any dependency on external commands or other wrapper scripts.

YouTube2Audiobook now fully supports playlists as first class citizens! Simply pass the URL of a playlist to YouTube2Audiobook and let the app do the rest!

For the above playlist, downloading every video in it as an audiobook simply becomes

$ yt2ab -u "https://youtu.be/gmmgWcbAmjo?list=PLZ-bKJtH3G7BYYscYohAFZAq65RwCf-K-"

This finally closes the enhancement issue “Add Playlist Handling” which has been a pain point for quite some time (even though the ticket doesn’t really tell the full story) and was addressed in commit ae8019e.

The implementation only differs from the above solution as much as that it leverages the id key instead of the url key. Not only are the values identical in the case of playlists. But for single videos, the id key is actually the only key that’s available in the corresponding JSON document. And extendability matters.

So, How do You Download Playlists?!

While the above Worx for Me!™ when it comes to supporting playlists in YouTube2Audiobook, you may have an alternative or better way.

Feel free to leave a comment or contribute to the GitHub repository, so we can make things better for everyone! A big thank you to everyone who did already contribute! Think this is all rubbish, incomplete, massively overcomplicated, or simply the wrong tool for the job?! Reach out to me on LinkedIn and teach me something new!

As always, prove me wrong and I’ll buy you a pint!

dominic

Dominic Dumrauf

A Cloud Success Champion by profession, an avid outdoor enthusiast by heart, and a passionate barista by choice. Still hunting that elusive perfect espresso.

Read More