Skip to content

Instantly share code, notes, and snippets.

@epochblue
Created August 21, 2021 18:14
Show Gist options
  • Save epochblue/814a2f8ca72a60b1bd629ad69660b834 to your computer and use it in GitHub Desktop.
Save epochblue/814a2f8ca72a60b1bd629ad69660b834 to your computer and use it in GitHub Desktop.
A silly Bash script for playing YouTube audio files on macOS.
#!/usr/bin/env bash
#
# Download the audio from a YouTube video, the play it using the Mac's built-in
# `afplay`.
#
# Note: requires `youtube-tl` (https://github.com/ytdl-org/youtube-dl)
#
yt_url=$1
tmp_file="/tmp/play.m4a"
clean_up_and_exit() {
rm "$tmp_file"
exit $?
}
play() {
youtube-dl --audio-format=m4a -f 'bestaudio[ext=m4a]' -o "$tmp_file" "$yt_url" 2>&1 > /dev/null
afplay "$tmp_file"
rm "$tmp_file"
}
trap clean_up_and_exit SIGINT
trap clean_up_and_exit SIGTERM
play
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment