diff --git a/load.sh b/load.sh index 1db27a9..4d94b14 100755 --- a/load.sh +++ b/load.sh @@ -10,15 +10,13 @@ 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 \ + --write-auto-subs --write-subs --sub-lang en,ru --convert-subs srt --sub-format vtt \ ${DOWNLOAD_LINK} } -create_structure() { +download_playlist() { PLAYLIST="${1}" JSON="$(${YTDLP} --flat-playlist -j "${PLAYLIST}" 2>/dev/null)" TITLE="$(echo ${JSON} | ${JQ} -r '.playlist_title' | head -n 1)" @@ -41,6 +39,58 @@ create_structure() { done } +merge() { + MP3="${1}" + MP4="${2}" + SRT_EN="${3}" + SRT_RU="${4}" + CURRENT_PATH="${5}" + OUTPUT="$(basename "${CURRENT_PATH}")" + + cd "${CURRENT_PATH}" + + ${FFMPEG} -i "${MP4}" -i "${MP3}" -i "${SRT_RU}" -i "${SRT_EN}" \ + -map 0 -map 1 -map 2 -map 3 \ + -c:v copy -c:a copy -metadata:s:a:0 language=eng -metadata:s:a:1 language=rus \ + -c:s mov_text -metadata:s:s:0 language=rus -metadata:s:s:1 language=eng -disposition:s:0 default -disposition:s:1 default \ + "${OUTPUT}.mp4" +} + +merge_mp3() { + SRCDIR="$(realpath "${1}")" + + for DIR in "${SRCDIR}"/* + do + for FILE in $(ls "${DIR}") + do + if [[ "${FILE}" == *".mp3" ]]; then + MP3="${FILE}" + elif [[ "${FILE}" == *".en.srt" ]]; then + SRT_EN="${FILE}" + elif [[ "${FILE}" == *".ru.srt" ]]; then + SRT_RU="${FILE}" + elif [[ "${FILE}" == *".mp4" ]]; then + MP4="${FILE}" + fi + done + [ ! -z "${MP3}" ] && [ ! -z "${SRT_EN}" ] && [ ! -z "${SRT_RU}" ] && [ ! -z "${MP4}" ] || continue + merge "${MP3}" "${MP4}" "${SRT_EN}" "${SRT_RU}" "${DIR}" + done +} + +move_mp4() { + SRCDIR="$(realpath "${1}")" + DSTDIR="${2}" + + mkdir -p "${DSTDIR}" + + for DIR in "${SRCDIR}"/* + do + FILE="$(basename "${DIR}").mp4" + mv -v "${DIR}/${FILE}" "${DSTDIR}" + done +} + main() { COMMAND="${1}" [ ! -z ${COMMAND} ] && shift @@ -53,6 +103,14 @@ main() { PLAYLIST="${VALUE}" shift ;; + -s|--source) + SRCDIR="${VALUE}" + shift + ;; + -d|--destination) + DSTDIR="${VALUE}" + shift + ;; *) ;; esac @@ -60,9 +118,18 @@ main() { done case "${COMMAND}" in - create) + download) [ -z "${PLAYLIST}" ] && echo "Playlist link unknown" && exit 1 - create_structure "${PLAYLIST}" + download_playlist "${PLAYLIST}" + ;; + merge) + [ -z "${SRCDIR}" ] && echo "Source directory not set" && exit 1 + merge_mp3 "${SRCDIR}" + ;; + move) + [ -z "${SRCDIR}" ] && echo "Source directory not set" && exit 1 + [ -z "${DSTDIR}" ] && echo "Destination directory not set" && exit 1 + move_mp4 "${SRCDIR}" "${DSTDIR}" ;; *) exit 1