Last active
June 6, 2021 09:18
-
-
Save FlandreDaisuki/bdbbe2048290a7082d2d6245c226dbe5 to your computer and use it in GitHub Desktop.
Download youtube playlist
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 | |
# usage: | |
# # if you install youtube-dl locally | |
# $ type youtube-dl | |
# youtube-dl 是 /usr/bin/youtube-dl | |
# $ bash sync.sh | |
# | |
# # if you use youtube-dl by docker alias | |
# $ type youtube-dl | |
# youtube-dl 是「docker run --rm -i -e PGID=$(id -g) -e PUID=$(id -u) -v "$(pwd)":/workdir:rw mikenye/youtube-dl」的別名 | |
# $ . sync.sh | |
# | |
ERR_YTDL_CMD=1 | |
ERR_JQ_CMD=2 | |
ERR_PL_DIR=3 | |
if [[ -z $(command -v youtube-dl) ]] ;then | |
>&2 echo "Error: Command 'youtube-dl' is not found" | |
exit "${ERR_YTDL_CMD}" | |
fi | |
if [[ -z $(command -v jq) ]] ;then | |
>&2 echo "Error: Command 'jq' is not found" | |
exit "${ERR_JQ_CMD}" | |
fi | |
YTDL_VERSION=$(youtube-dl --version) | |
echo "youtube-dl v${YTDL_VERSION}" | |
ROOT_DIR="$(pwd)" | |
PLAYLISTS=( | |
# Aoi ch. | |
'PLFPYP7GcgzUQD-sMeyh_OO1DGIA4ez52A' # 全カバー動画(投稿順) | |
'PLFPYP7GcgzUQNGMz8pQVgfU4vtegwu2iB' # 富士葵オリジナル曲 | |
# HIMEHINA Channel | |
'PL1tX8zAv8bPkOg13XRuyrNsFQw8X2d6CQ' # ヒメヒナMusic | |
# QuonTama Ch. 久遠たま | |
'PLLDVHep1V7dwo23o8LjXgCACtzXpXTBrU' # 【公式切り抜き】 | |
'PLLDVHep1V7dzkyTizvIiRuZUrz5jMu99G' # 【song】 | |
'PLLDVHep1V7dxq32eiO7Hj4q2aEAeIgbU-' # #くぉんたま【sing🎤】 | |
# 芦澤サキ / SAKI ASHIZAWA | |
'PLk2NROpUvNYTp24jG9JfgI1zNhVShplbX' # covered by 芦澤 サキ | |
) | |
# FORMAT SELECTION | |
# https://github.com/ytdl-org/youtube-dl/blob/master/README.md#format-selection | |
QUALITY='bestvideo+bestaudio/best' | |
for PL_ID in "${PLAYLISTS[@]}"; do | |
PL_META=$(youtube-dl -ciJ "${PL_ID}") | |
PL_NAME=$(jq -r '.title' <<< "${PL_META}" | sed 's~/~/~') | |
CHANNEL_NAME=$(jq -r '.uploader' <<< "${PL_META}" | sed 's~/~/~') | |
PL_DIR="${ROOT_DIR}/${CHANNEL_NAME}/${PL_NAME}" | |
mkdir -p "${PL_DIR}" && cd "${PL_DIR}" || exit "${ERR_PL_DIR}" | |
echo "Info: ${CHANNEL_NAME} :: ${PL_NAME}" | |
VIDEO_IDS=( $(jq -r '.entries[].id | select(. != null)' <<< "${PL_META}") ) | |
for VIDEO_ID in "${VIDEO_IDS[@]}"; do | |
youtube-dl -ci -f "${QUALITY}" "https://youtu.be/${VIDEO_ID}" | |
# handle duplicated | |
LINE_COUNT=$(find -name "*$VIDEO_ID*" | wc -l) | |
if [[ "$LINE_COUNT" -gt 1 ]]; then | |
LATEST_DUP_VIDEO_NAME= | |
LATEST_DUP_VIDEO_LAST_MODIFIED= | |
find -name "*$VIDEO_ID*" | while read -r DUP_VIDEO; do | |
LAST_MODIFIED=$(stat -c '%Y' "$DUP_VIDEO") | |
if [[ -z "$LATEST_DUP_VIDEO_NAME" ]]; then | |
LATEST_DUP_VIDEO_NAME="$DUP_VIDEO" | |
LATEST_DUP_VIDEO_LAST_MODIFIED="$LAST_MODIFIED" | |
fi | |
if [[ "$LAST_MODIFIED" -gt "$LATEST_DUP_VIDEO_LAST_MODIFIED" ]]; then | |
rm -f "$LATEST_DUP_VIDEO_NAME" | |
LATEST_DUP_VIDEO_NAME="$DUP_VIDEO" | |
LATEST_DUP_VIDEO_LAST_MODIFIED="$LAST_MODIFIED" | |
fi | |
done | |
fi | |
done | |
done | |
cd "$ROOT_DIR" | |
# youtube-dl options | |
# https://github.com/ytdl-org/youtube-dl/blob/master/README.md#options | |
# | |
# -i, --ignore-errors Continue on download errors, for | |
# example to skip unavailable videos in a | |
# playlist | |
## Filesystem Options: | |
# -c, --continue Force resume of partially downloaded | |
# files. By default, youtube-dl will | |
# resume downloads if possible. | |
## Verbosity / Simulation Options: | |
# -j, --dump-json Simulate, quiet but print JSON | |
# information. See the "OUTPUT TEMPLATE" | |
# for a description of available keys. | |
# -J, --dump-single-json Simulate, quiet but print JSON | |
# information for each command-line | |
# argument. If the URL refers to a | |
# playlist, dump the whole playlist | |
# information in a single line. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment