Last active
September 18, 2019 18:07
-
-
Save knez/c71661a4dcd3f553078042e77c1563f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # user defined variables | |
| #----------------------- | |
| SAVEDIR="Downloads" | |
| SONGLIST="list.txt" | |
| #----------------------- | |
| check_error() { | |
| # if last command failed | |
| if [[ $? -ne 0 ]]; then | |
| # append problematic song to list | |
| UNCOMPLETED="${UNCOMPLETED}${1}\n" | |
| return -1; | |
| fi | |
| return 0; | |
| } | |
| crop_mp3() { | |
| # get the video title | |
| TITLE=$(youtube-dl --get-filename -o "%(title)s" "$1") | |
| # fetch video and convert to mp3 | |
| youtube-dl -x --audio-format=mp3 -o "%(title)s.%(ext)s" "$1" | |
| if check_error $1; then | |
| ffmpeg -ss "$2" -i "$TITLE.mp3" -acodec copy "$SAVEDIR/$TITLE.mp3" >/dev/null | |
| # cleanup | |
| rm -f "$TITLE.mp3" | |
| fi | |
| } | |
| # keep track of failed attempts | |
| UNCOMPLETED="" | |
| # create new folder if it doesn't already exist | |
| mkdir -p "$SAVEDIR" | |
| # remove empty lines or comments and iterate through | |
| for SONG in $(sed -E '/^$|^\ *#/d' $SONGLIST); | |
| do | |
| OFFSET=$(grep -oP 't=\K\d+' <<< "$SONG") | |
| # get time offset if present | |
| if [[ -n "$OFFSET" ]]; then | |
| crop_mp3 "$SONG" "$OFFSET" | |
| else | |
| # fetch video and convert to mp3 | |
| youtube-dl -x --audio-format=mp3 -o "$SAVEDIR/%(title)s.%(ext)s" "$SONG" | |
| check_error "$SONG" | |
| fi | |
| done | |
| # prepend comment at the top if there were errors | |
| if [[ -n "$UNCOMPLETED" ]]; then | |
| UNCOMPLETED="# Couldn't download these files:\n${UNCOMPLETED}" | |
| fi | |
| # write uncompleted links to song list | |
| echo -e "$UNCOMPLETED" > "$SONGLIST" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment