#!/bin/bash set -e YTDLP=$(which yt-dlp) JQ=$(which jq) FFMPEG=$(which ffmpeg) download() { DOWNLOAD_LINK="${1}" SAVE_PATH="${2}" # cd "${SAVE_PATH}" ${YTDLP} -o "${SAVE_PATH}/%(id)s.%(ext)s" \ -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best' \ --write-auto-subs --write-subs --sub-lang en,ru --convert-subs srt --sub-format txt \ ${DOWNLOAD_LINK} } create_structure() { PLAYLIST="${1}" JSON="$(${YTDLP} --flat-playlist -j "${PLAYLIST}" 2>/dev/null)" TITLE="$(echo ${JSON} | ${JQ} -r '.playlist_title' | head -n 1)" VIDEOS="$(echo ${JSON} | ${JQ} -r '.title')" IDS="$(echo ${JSON} | ${JQ} -r '.id')" LINKS="$(echo ${JSON} | ${JQ} -r '.original_url')" ALL_LINES=$(echo "${JSON}" | wc -l) mkdir -v "${TITLE}" for ((NUMBER=1; NUMBER<=ALL_LINES; NUMBER++)); do VIDEO="$(echo "${VIDEOS}" | awk "NR==${NUMBER}")" LINK="$(echo "${LINKS}" | awk "NR==${NUMBER}")" VIDEO="${VIDEO//\//-}" DIR_NAME="${NUMBER} - ${VIDEO}" mkdir -v "${TITLE}/${DIR_NAME}" download "${LINK}" "${TITLE}/${DIR_NAME}" done } main() { COMMAND="${1}" [ ! -z ${COMMAND} ] && shift while [[ $# -gt 0 ]]; do KEY="${1}" VALUE="${2}" case "${KEY}" in -p|--playlist) PLAYLIST="${VALUE}" shift ;; *) ;; esac shift done case "${COMMAND}" in create) [ -z "${PLAYLIST}" ] && echo "Playlist link unknown" && exit 1 create_structure "${PLAYLIST}" ;; *) exit 1 ;; esac } main "${@}"