From af823f79fd2676606e1fa2efb6e134569df038c0 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Tue, 12 Dec 2023 22:09:40 +0300 Subject: [PATCH] init --- README.md | 1 + load.sh | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 README.md create mode 100755 load.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..930fef4 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Скрипт загрузки видео с YouTube diff --git a/load.sh b/load.sh new file mode 100755 index 0000000..1db27a9 --- /dev/null +++ b/load.sh @@ -0,0 +1,73 @@ +#!/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 "${@}"