« Back to posts

Youtube to Apple audiobook converter

November 27, 2023

Buongiorno! Tutto bene?!

I created a simple "YouTube to audiobook" converter, which helps me enjoy loved episodes or whole playlists on the go and even on the airplane.

I'm open to sharing it! Take it!

I've put it into an Apple Shortcut, so it is effortless to use!

Just CMD+C and one click!

  • Copy the single video or playlist YouTube URL to the clipboard;

  • Run the shortcut on macOS by clicking on it:

    image-20231123095603027

The shortcut will start downloading YouTube videos as audio files, and then assemble an Apple Audiobook.

  • The book will be located in the Downloads folder;
  • Double-click to open in Books, and then you can listen or sync with your iPhone.

I didn't find a way to sync via iCloud; I sync via Finder on Mac.

Let's install it!

You need a Mac and 5 minutes of the time. Let's go!

Here are two ways:

Easiest way

Step 1. Install brew.sh (or go to step 2, if already installed)

Open the Terminal and run this:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Step 2. Install needed packages via brew (run in Terminal):

brew install yt-dlp ffmpeg AtomicParsley

Step 3. Add this shortcut to your Mac by clicking on it;

This shortcut should appear in the list of your available shortcuts.


Create your own shortcut

You need a little knowledge of how to install things : ) And you have to open the Terminal a few times before!

Step 1. Install brew.sh

Step 2. Create a new shortcut, and put these settings. Up to you:

Pinned to Menu bar:

image-20231123114719003

And in the privacy tab, it looks like this:

It will ask you during the initial run — can I have access to here, to there, etc

image-20231123114737720

Step 3. Copy&paste (and check thoroughly for yourself) the code below to your new shortcut:

#!/bin/zsh

# skk 23 Nov, 23
# before use:
# brew install yt-dlp ffmpeg AtomicParsley
# then CMD+C YOUTUBE VIDEO or PLAYLIST and RUN shortcut!

export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

YTDLP="/opt/homebrew/bin/yt-dlp"
FFMPEG="/opt/homebrew/bin/ffmpeg"
PARSLEY="/opt/homebrew/bin/AtomicParsley"
FFPROBE="/opt/homebrew/bin/ffprobe"
BREW="/opt/homebrew/bin/brew"

### /// install packages if needed
if [ ! -f "$YTDLP" ]; then
	${BREW} install yt-dlp
fi
if [ ! -f "$FFMPEG" ] || [ ! -f "$FFPROBE" ]; then
	${BREW} install ffmpeg
fi
if [ ! -f "$PARSLEY" ]; then
	${BREW} install atomicparsley
fi


YOUTUBE="Clipboard"

PLAYLIST_INDEX=""
if [[ "$YOUTUBE" == *"list="* ]]; then
    PLAYLIST_INDEX="%(playlist_index)s-" 
    BOOK_NAME=$("/opt/homebrew/bin/yt-dlp" --skip-download --print playlist_title -I 1:1 "$YOUTUBE")
else
    BOOK_NAME=$("/opt/homebrew/bin/yt-dlp" --skip-download --get-title "$YOUTUBE")
fi
BOOK_FILE_NAME=$(echo "${BOOK_NAME}" | sed -e 's/[^а-яА-Яa-zA-Z0-9 \-\—]//g')
FULLDIR=~/Downloads/processing_"$(date +%s%3)"
mkdir -p "${FULLDIR}"
cd "${FULLDIR}"

### download video or list
"/opt/homebrew/bin/yt-dlp" -x -o "${PLAYLIST_INDEX}%(title)s.%(ext)s" -f "bestaudio[ext=m4a]" --add-metadata --embed-thumbnail --ffmpeg-location "/opt/homebrew/bin/ffmpeg" "${YOUTUBE}"

echo ";FFMETADATA1" > chapters.txt
start=0
for f in *.m4a; do
    # Get the duration of the file in milliseconds and convert it to an integer
    duration=$("/opt/homebrew/bin/ffprobe" -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$f")
    duration_ms=$(echo "$duration*1000/1" | bc)
    # Calculate the end time for the chapter
    end=$(($start + $duration_ms))
    # Extract filename without extension for chapter title
    chapter_title="${f%.m4a}"
    # Write chapter details to the file
    printf "[CHAPTER]\nTIMEBASE=1/1000\nSTART=%d\nEND=%d\ntitle=%s\n" "$start" "$end" "$chapter_title" >> chapters.txt
    # Update start time for next chapter
    start=$end
done

AUTHOR=`"/opt/homebrew/bin/yt-dlp" --skip-download --print channel -I 1:1 "${YOUTUBE}"`

for f in *.m4a; do echo "file '$f'" >> filelist.txt; done
"/opt/homebrew/bin/ffmpeg" -f concat -safe 0 -i filelist.txt -c copy -vn concatenated_audio.m4a

THUMB=`"/opt/homebrew/bin/yt-dlp" --skip-download --get-thumbnail -I 1:1 "${YOUTUBE}"`
"/opt/homebrew/bin/ffmpeg" -i "${THUMB}" "cover.jpg"
"/opt/homebrew/bin/ffmpeg" -i concatenated_audio.m4a -i chapters.txt -map_metadata 1 -acodec copy "${BOOK_FILE_NAME}.m4b"

"/opt/homebrew/bin/AtomicParsley" "${BOOK_FILE_NAME}.m4b" --artwork cover.jpg --artist "${AUTHOR}" --title "${BOOK_NAME}" --overWrite

mv "${BOOK_FILE_NAME}.m4b" ~/Downloads/"${BOOK_FILE_NAME}.m4b"
rm -rf "${FULLDIR}"

Step 4. Replace value of YOTUBE variable to clipboard, should look like:

image-20231123114342250

Step 5. You can tune your shorcut with some side cases, like there nothing in clipboard, or not a link for YouTube, etc.


ps: this is for eductational purpose only. Use it on your own legal & other risks.

skk ❤️