This commit is contained in:
Alexander Zhirov 2023-12-12 22:09:40 +03:00
commit af823f79fd
2 changed files with 74 additions and 0 deletions

1
README.md Normal file
View File

@ -0,0 +1 @@
# Скрипт загрузки видео с YouTube

73
load.sh Executable file
View File

@ -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 "${@}"