Package

~/.local/bin/yu

Rina Kawakita 2019. 12. 26. 22:54

~/.local/bin/yu

#!/usr/bin/env bash

# For youtube playlist download
# Dependencies : youtube-dl, ffmpeg
# by nietz
# usage : $ yu [a|b|c] [bachfile]
#         $ yu d [youtube playlist URL]

# emby 뮤직비디오 라이브러리 경로
MUSIC_VIDEO_LIB_PATH="/run/media/nietz/data_01/media/Music Video"
WEB_DRAMA_VIDEO_LIB_PATH="/run/media/nietz/data_01/media/Web Drama"
# playlist url 리스트를 담고 있는 파일
PLAYLIST_URL_FILE="/home/user/albums_playlist"
SINGLE_PLAYLIST_URL=$2
BATCH_FILE=$2

get_album_playlist () {
    #youtube-dl --all-subs --embed-subs --write-thumbnail \ --format 'bestvideo[height<=?2160][fps<=?60][protocol!=http_dash_segments][ext=mp4]+(bestaudio[acodec=opus]/bestaudio[acodec=vorbis]/bestaudio[acodec=aac]/bestaudio/bestaudio[ext=webm])/bestvideo+bestaudio/best[ext=mp4]/best' --add-metadata --metadata-from-title '(?P<artist>.+?) - (?P<title>.+)' --output "${MUSIC_VIDEO_LIB_PATH}/%(uploader)s/albums/%(playlist)s/%(playlist_index)s %(title)s - (id)s.%(ext)s" --batch-file="${PLAYLIST_URL_FILE}"
    youtube-dl --all-subs --embed-subs --write-thumbnail \
        --format 'bestvideo[height<=?2160][fps<=?60][protocol!=http_dash_segments][ext=mp4]+(bestaudio[acodec=opus]/bestaudio[acodec=vorbis]/bestaudio[acodec=aac]/bestaudio/bestaudio[ext=webm])/bestvideo+bestaudio/best[ext=mp4]/best' \
        --add-metadata --metadata-from-title '(?P<artist>.+?) - (?P<title>.+)' \
        --output "${MUSIC_VIDEO_LIB_PATH}/%(uploader)s/albums/%(playlist)s/%(playlist_index)s - %(title)s - %(id)s.%(ext)s" \
        --batch-file="${BATCH_FILE}"
    # 업로더가 2개인 경우가 있뜸 %(uploader)s 대신 가수 이름을 직접 넘겨야 할듯. 

    get_batch_file_album_coverart
}

get_normal_playlist () {
    #youtube-dl --all-subs --embed-subs --write-thumbnail --format 'bestvideo[height<=?2160][fps<=?60][protocol!=http_dash_segments][ext=mp4]+(bestaudio[acodec=opus]/bestaudio[acodec=vorbis]/bestaudio[acodec=aac]/bestaudio/bestaudio[ext=webm])/bestvideo+bestaudio/best[ext=mp4]/best' --add-metadata --metadata-from-title '(?P<artist>.+?) - (?P<title>.+)' --output "${MUSIC_VIDEO_LIB_PATH}/%(uploader)s/playlists/%(playlist)s/%(title)s - (id)s.%(ext)s" --batch-file="${PLAYLIST_URL_FILE}" 
    youtube-dl --all-subs --embed-subs --write-thumbnail \
        --format 'bestvideo[height<=?2160][fps<=?60][protocol!=http_dash_segments][ext=mp4]+(bestaudio[acodec=opus]/bestaudio[acodec=vorbis]/bestaudio[acodec=aac]/bestaudio/bestaudio[ext=webm])/bestvideo+bestaudio/best[ext=mp4]/best' \
        --add-metadata --metadata-from-title '(?P<artist>.+?) - (?P<title>.+)' \
        --output "${MUSIC_VIDEO_LIB_PATH}/%(uploader)s/playlists/%(playlist)s/%(title)s - %(id)s.%(ext)s" \
        --batch-file="${BATCH_FILE}" 

    get_batch_file_album_coverart
}

get_single_playlist () {
    youtube-dl --all-subs --embed-subs --write-thumbnail \
        --format 'bestvideo[height<=?2160][fps<=?60][protocol!=http_dash_segments][ext=mp4]+(bestaudio[acodec=opus]/bestaudio[acodec=vorbis]/bestaudio[acodec=aac]/bestaudio/bestaudio[ext=webm])/bestvideo+bestaudio/best[ext=mp4]/best' \
        --add-metadata --metadata-from-title '(?P<artist>.+?) - (?P<title>.+)' \
        --output "${MUSIC_VIDEO_LIB_PATH}/playlists/%(playlist)s/%(playlist_index)s - %(title)s - %(id)s.%(ext)s" \
        ${SINGLE_PLAYLIST_URL}

    get_album_coverart $SING_PLAYLIST_URL
}

get_mp3_playlist () {
    youtube-dl --extract-audio --audio-format mp3 \
        --output '%(playlist)s/%(playlist_index)s - %(title)s - %(id)s.%(ext)s' \
        --batch-file="${BATCH_FILE}"
    get_batch_file_album_coverart
}

get_album_coverart () {
    URL=$1

    if [ "$URL" = "" ]
    then
        URL=$SINGLE_PLAYLIST_URL
        echo 1 single playlist
    else
        echo 2 batch file playlist
    fi
    echo $URL

    COVER_TEMP=$(wget ${URL} -q -O /tmp/youtube_playlist_url.html)

    URL_TAG_LINE=$(grep "sddefault" /tmp/youtube_playlist_url.html | tail -n1)
    #echo $URL_TAG_LINE

    COVER_ART_URL=$(echo $URL_TAG_LINE | \
        sed "s/\"//g" | \
        sed "s/>//g" | \
        awk '{print $3}' | \
        sed "s/^content=//" | \
        sed "s/\&amp;/\&/g")
    echo $COVER_ART_URL

    ALBUM_NAME=$(grep "<title>" /tmp/youtube_playlist_url.html | \
        tail -n1 | \
        sed 's/<[^>]\+>//g' | \
        sed 's/ - YouTube//' | \
        sed "s/\&#39;/\'/g" | \
        sed 's/^ *//; s/ *$//; /^$/d; /^\s*$/d')
    echo $ALBUM_NAME

    wget $COVER_ART_URL -q -O "${MUSIC_VIDEO_LIB_PATH}"/"${ALBUM_NAME}"-poster.png
    rm -rf /tmp/youtube_playlist_url.html
}

get_batch_file_album_coverart () {
    #cat ${PLAYLIST_URL_FILE} |
    cat "${BATCH_FILE}" |
        while read -r n; do
            get_album_coverart $n
        done
}

if [ $# -eq 0 ] || [ $# -eq 1 ]; then
    echo "yu a|b [플레리리스트 url 담고 있는 batch 리스트 파일명]"
    echo "yu c [플레리리스트 url 담고 있는 batch 리스트 파일명]"
    echo "yu d [youtube playlist url]"
    exit
fi

if [ $3 == "web" ]; then MUSIC_VIDEO_LIB_PATH=$WEB_DRAMA_VIDEO_LIB_PATH; fi

case $1 in
    a)  get_album_playlist      ;;
    b)  get_normal_playlist     ;;
    c)  get_mp3_playlist        ;;
    d)  get_single_playlist     ;;
    *)  exit                    ;;
esac