Skip to content

Instantly share code, notes, and snippets.

@MasWag
Forked from ivan/make-audio-feed.sh
Created February 3, 2024 13:18
Show Gist options
  • Save MasWag/3802b53d5baf31379ce741a73b639e5b to your computer and use it in GitHub Desktop.
Save MasWag/3802b53d5baf31379ce741a73b639e5b to your computer and use it in GitHub Desktop.
Script to generate a Podcasts/Overcast-compatible RSS feed for audio files on your own web server
#!/usr/bin/env bash
# Generates an RSS feed for a list of audio files, for consumption by Overcast
# and other podcast players.
#
# Usage: make-audio-feed TITLE BASE_URL FILE...
# Example: (cd directory && make-audio-feed 'My Audio Files' 'https://your.server/directory/' * > .feed.rss)
set -eu -o pipefail
# https://gist.github.com/cdown/1163649#gistcomment-2157284
urlencode() {
local LANG=C i c e=''
for ((i=0;i<${#1};i++)); do
c=${1:$i:1}
[[ "$c" =~ [a-zA-Z0-9/\.\~\_\-] ]] || printf -v c '%%%02X' "'$c"
e+="$c"
done
echo "$e"
}
entity-escape() {
sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g'
}
title=$1
shift
cat <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
<channel>
<title>$title</title>
<description>$title</description>
<link></link>
EOF
base_url=$1
shift
for f in "$@"; do
filename="${f##*/}"
if [[ "$filename" =~ ^[0-9]{14}- ]] ; then
# Use the date and time in the filename if exists
pubTimeEpoch="${filename::8} ${filename:8:6}"
elif [[ "$filename" =~ ^[0-9]{8}- ]] ; then
# Use the date in the filename if exists
pubTimeEpoch="${filename::8}"
else
# Use the file's mtime for the pubDate
pubTimeEpoch="@$(stat -c %Y -- "$f")"
fi
pubDate=$(LC_ALL=C date --date="$pubTimeEpoch" '+%a, %d %b %Y %H:%M:%S %z')
case ${f##*.} in
mp3)
mime=audio/mp3
;;
aac)
mime=audio/aac
;;
oga)
mime=audio/ogg
;;
ogg)
mime=audio/ogg
;;
opus)
mime=audio/opus
;;
wav)
mime=audio/wav
;;
weba)
mime=audio/webm
;;
*)
mime=application/octet-stream
;;
esac
cat <<EOF
<item>
<title>$(echo -n $filename | entity-escape)</title>
<guid>$(echo -n $filename | entity-escape)</guid>
<enclosure url="${base_url}$(urlencode "$f" | entity-escape)" type="$mime" />
<pubDate>$pubDate</pubDate>
</item>
EOF
done
cat <<EOF
</channel>
</rss>
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment