media scwrypts v5 refactor

This commit is contained in:
2025-02-19 21:58:15 -07:00
parent a3410d9b15
commit a20d23ad5e
25 changed files with 868 additions and 0 deletions

39
scwrypts/media/youtube/download Executable file
View File

@ -0,0 +1,39 @@
#!/bin/zsh
#####################################################################
use youtube --group media
#####################################################################
USAGE__description="
download videos from youtube
"
USAGE__args='
$@ any number of URLS to download (becomes interactive if omitted)
'
#####################################################################
MAIN() {
local URLS=($@)
local ARGS=()
local DOWNLOAD_ERRORS=0
[[ $# -eq 0 ]] && {
URLS=($(echo '' | utils.fzf.user-input 'download URL'))
[[ ${#URLS[@]} -gt 0 ]] || ABORT
ARGS+=(--interactive)
}
local URL FILENAME
for URL in ${URLS[@]}
do
media.youtube.download ${ARGS[@]} --url "${URL}" \
|| ((DOWNLOAD_ERRORS+=1))
done
return ${DOWNLOAD_ERRORS}
}

View File

@ -0,0 +1,63 @@
#####################################################################
use youtube/yt-dlp --group media
use youtube/get-filename --group media
use youtube/get-download-path --group media
#####################################################################
${scwryptsmodule}() {
eval "$(USAGE.reset)"
local \
URL INTERACTIVE=false \
PARSERS=()
eval "$ZSHPARSEARGS"
##########################################
local FILENAME="$(media.youtube.get-filename "${URL}")"
[ "${FILENAME}" ] \
|| ERROR "could not find metadata; cannot proceed with download\n${URL}" \
|| return 1
media.youtube.yt-dlp "${URL}" \
--format 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4' \
&& echo.success "finished download of ${URL}\n$(media.youtube.get-download-path)/${FILENAME}" \
|| ERROR "failed to download '${FILENAME}' (${URL})"
;
}
#####################################################################
${scwryptsmodule}.parse() {
# local URL INTERACTIVE=false
local PARSED=0
case $1 in
--url )
PARSED=2
URL="$2"
;;
--interactive )
PARSED=1
INTERACTIVE=true
;;
esac
return $PARSED
}
${scwryptsmodule}.parse.usage() {
USAGE__options+="
--url <string> the URL of the target video
"
}
${scwryptsmodule}.parse.validate() {
[ "$URL" ] \
|| ERROR "must provide download URL"
}

View File

@ -0,0 +1,38 @@
#!/bin/zsh
use ffmpeg/get-audio-clip-from-video --group media
use youtube/get-download-path --group media
#####################################################################
#####################################################################
MAIN() {
local DOWNLOAD_PATH="$(media.youtube.get-download-path)"
local INPUT_FILENAME="$(
cd -- "${DOWNLOAD_PATH}"
find . -type f -name \*.mp4 \
| sed 's|^./||' \
| utils.fzf 'select a video' \
| sed 's/\.mp4$//' \
)"
[ "${INPUT_FILENAME}" ] || ABORT
local OUTPUT_FILENAME="$(\
basename -- "${INPUT_FILENAME}" \
| sed 's/\.[^.]*$//' \
| utils.fzf.user-input 'what should I call this clip? (.mp3)' \
| sed 's/\(\.mp3\)*$//' \
)"
[ "${OUTPUT_FILENAME}" ] || ABORT
INPUT_FILENAME="${DOWNLOAD_PATH}/${INPUT_FILENAME}.mp4"
OUTPUT_FILENAME="${DOWNLOAD_PATH}/${OUTPUT_FILENAME}.mp3"
media.ffmpeg.get-audio-clip-from-video \
--input-filename "${INPUT_FILENAME}" \
--output-filename "${OUTPUT_FILENAME}" \
;
}

View File

@ -0,0 +1,7 @@
${scwryptsmodule}() {
local DOWNLOAD_PATH="${SCWRYPTS_DATA_PATH}/youtube"
mkdir -p -- "${DOWNLOAD_PATH}" &>/dev/null
echo "${DOWNLOAD_PATH}"
}

View File

@ -0,0 +1,12 @@
#####################################################################
use youtube/yt-dlp --group media
#####################################################################
${scwryptsmodule}() {
media.youtube.yt-dlp --dump-json $@ \
| jq -r '._filename' \
| sed 's/\.[^.]*$/\.mp4/' \
;
}

View File

@ -0,0 +1,15 @@
#
# interact with youtube
#
# download a youtube video by URL
use youtube/download --group media
# show fully-qualified path to downloads
use youtube/get-download-path --group media
# interact with yt-dlp directly
use youtube/yt-dlp --group media

View File

@ -0,0 +1,15 @@
#####################################################################
use youtube/get-download-path --group media
#####################################################################
DEPENDENCIES+=(yt-dlp)
${scwryptsmodule}() {
(
cd -- "$(media.youtube.get-download-path)"
yt-dlp \
--restrict-filenames \
$@
)
}