Skip to content

Instantly share code, notes, and snippets.

@Luxian
Last active February 21, 2024 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luxian/a9e4dfeffd5239691862a81675234e4a to your computer and use it in GitHub Desktop.
Save Luxian/a9e4dfeffd5239691862a81675234e4a to your computer and use it in GitHub Desktop.
Download a song, convert it to mp3 and resize the thumbnail to be square by fillinf the image with blur
#!/bin/bash
# This requires:
# - yt-dpl
# - ffmpeg
# For convenience, it can be added to alias file (e.g. ~.bash_aliases)
download_mp3() {
if [ $# -eq 0 ]; then
echo "Please specify an URL and optionally a destination folder";
echo "Example:"
echo " download_mp3 https://www.youtube.com/watch?v=LDU_Txk06tM 'Crab Rave'"
return 1;
fi
export URL="${1}"
export DIR="${2}"
export COVER_SIZE=1024
if [[ $DIR != "" ]]; then
if [[ -f "$DIR" ]]; then
echo "There is already a file with the name '$DIR'. Aborting..."
return 1
fi
if [[ ! -d "$DIR" ]]; then
(mkdir "$DIR" && echo "Created '$DIR'") || echo "Failed to create directory '$DIR'"
fi
cd "$DIR"
fi;
yt-dlp \
--write-thumbnail \
--convert-thumbnails jpg \
--embed-thumbnail \
--ppa "EmbedThumbnail+ffmpeg_o:-c:v mjpeg -vf '[0:v]split=2[blur][vid];[blur]scale=1024:1024:force_original_aspect_ratio=increase,crop=$COVER_SIZE:$COVER_SIZE,boxblur=luma_radius=min(h\,w)/20:luma_power=1:chroma_radius=min(cw\,ch)/20:chroma_power=1[bg];[vid]scale=$COVER_SIZE:$COVER_SIZE:force_original_aspect_ratio=decrease[ov];[bg][ov]overlay=(W-w)/2:(H-h)/2'" \
--extract-audio \
--audio-format mp3 \
--audio-quality 320K \
--embed-metadata \
--keep-video \
"$URL"
if [[ $DIR != "" ]]; then
cd .. # switch back to the original directory
fi;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment