Skip to content

Instantly share code, notes, and snippets.

@viocar
Forked from ivan/grab-anchor-episode.sh
Last active November 16, 2022 14:06
Show Gist options
  • Save viocar/a6b6a0f485b3f400b8bcb0f8334b454d to your computer and use it in GitHub Desktop.
Save viocar/a6b6a0f485b3f400b8bcb0f8334b454d to your computer and use it in GitHub Desktop.
Download a podcast episode from anchor.fm
#!/usr/bin/env bash
# Download a podcast episode from anchor.fm
#
# Usage:
# grab-anchor-episode "https://anchor.fm/emerge/episodes/Robert-MacNaughton---Learnings-from-the-Life-and-Death-of-the-Integral-Center-e31val" # (m4a example)
# grab-anchor-episode "https://anchor.fm/free-chapel/episodes/Are-You-Still-In-Love-With-Praise--Pastor-Jentezen-Franklin-e19u4i8" # (mp3 example)
#
# anchor.fm serves a list of m4a or mp3 files that need to be concatenated with ffmpeg.
#
# For debugging, uncomment:
# set -o verbose
set -eu -o pipefail
url="https://anchor.fm/api/v3/episodes/${1##*-}"
json=$(curl -sL "$url")
ymd=$(echo -E $json | jq -r '.episode.publishOn' | cut -d 'T' -f 1)
extension=$((echo -E $json | jq -r '.episode.episodeEnclosureUrl' | grep -F --max-count=1 :// | grep -oP '\.[0-9a-z]+$' | cut -d . -f 2) || echo m4a)
output_basename=$ymd-$(basename -- "$1").$extension
if [[ -f "$output_basename" ]]; then
echo "$output_basename already exists; skipping download"
exit
fi
temp_dir="$(mktemp -d)"
cd "$temp_dir"
audio_urls=$(echo -E $json | jq -r '.episodeAudios|map(.audioUrl)|.[]')
for i in $audio_urls; do
output_file=$(basename -- "$i")
wget "$i" -O "$output_file"
echo "file '$output_file'" >> .copy_list
done
ffmpeg -f concat -safe 0 -i .copy_list -f mp4 -c copy "$output_basename"
cd -
mv "$temp_dir/$output_basename" ./
rm -rf "$temp_dir"
@marcelbonnici
Copy link

Thank you so much!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment