diff options
| author | Steph Enders <steph@senders.io> | 2025-12-07 13:36:43 -0500 |
|---|---|---|
| committer | Steph Enders <steph@senders.io> | 2025-12-07 14:44:12 -0500 |
| commit | fe1c547893d97e9432c87b0838ff19bfdb7b9ea5 (patch) | |
| tree | c10fe0fa4795792cce8255347ad4c33238927da7 | |
| parent | f03544add8fffefadefba7587a39b1c7289e46b3 (diff) | |
Add support for setting target directory
Includes some enhancements and updates on how arguments/options are
parsed.
Passing `-d /target/dir` you can specify an output directory:
`./yt-dlp-best -d /home/$USER/Videos/ 'https://example.com?my=video'`
The file will first be written to a temporary directory (leveraging
mktemp if available on the system; else a default directory created in
/tmp/) then moved to your target directory.
This will allow for targetting a slower / networked drive and avoid
both the video & audio write and merge happening over the networked
disk.
Future improvement would be to add a -t to allow passing in a specific
temp dir.
| -rwxr-xr-x | yt-dlp-best | 106 |
1 files changed, 96 insertions, 10 deletions
diff --git a/yt-dlp-best b/yt-dlp-best index 9610e7a..6e1f459 100755 --- a/yt-dlp-best +++ b/yt-dlp-best @@ -1,44 +1,99 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh +## Helper functions +function mktemp_dir() { + if [ $(command -v mktemp) ]; then + echo $(mktemp -d) + else + DEFAULT_TEMP_DIR=/tmp/yt_dlp_best_dir + mkdir -p $DEFAULT_TEMP_DIR + echo $DEFAULT_TEMP_DIR + fi +} +## + +## Check Requirements + +if [ ! $(command -v yt-dlp) ]; then + echo "yt-dlp is required to run this script. Install it." + exit 1 +fi + +## Global Values USAGE="./yt-dlp-best [options] \"URL\" Fetches the best format of a YT video (ideally in mp4) Options: -y skips the format check normally requiring user action to continue with download - -s fetches subtitles" + -s fetches subtitles + -d [DIR] sets the output destination" +# failsafe +if [ $# -eq 0 ]; then + echo "$USAGE" + exit 1 +fi SKIP_CHECK=1 -SUBS=1 +SUBS_FLAG=1 +DEST_FLAG=1 +DEST_DIR= -while getopts "hys" opt; do +## main logic +while getopts "hysd:" opt; do case "$opt" in h) echo "$USAGE" exit 1 ;; y) SKIP_CHECK=0 ;; - s) SUBS=0 + s) SUBS_FLAG=0 + ;; + d) DEST_FLAG=0 + DEST_DIR="$OPTARG" ;; esac done +shift $(($OPTIND -1)) + -URL=${@:$OPTIND:1} +URL=$1 +if [ -z "$URL" ]; then + echo "Missing URL / invalid command arguments" + exit 1 +fi + +DEFAULT_FILE_TEMPLATE="%(fulltitle)s.%(ext)s" +DEFAULT_YTDLP_OPTS="--no-warnings --restrict-filename" DESIRED_FMT="mp4" DESIRED_SUBFMT="srt" DESIRED_SUBLANG="en" -FMT_LINE=$(yt-dlp -F "${URL}" | grep "${DESIRED_FMT}" | tail -n 1) +FMT_LINE=$(yt-dlp ${DEFAULT_YTDLP_OPTS} -F "${URL}" | grep "${DESIRED_FMT}" | tail -n 1) FMT=$(echo ${FMT_LINE} | cut -d" " -f1) -if [ $SUBS -eq 0 ]; then + +if [ $SUBS_FLAG -eq 0 ]; then substr="--write-subs --sub-langs=${DESIRED_SUBLANG} --sub-format=${DESIRED_SUBFMT}" else substr="" fi + +if [ $DEST_FLAG -eq 0 ]; then + if [ ! -d $DEST_DIR ]; then + echo "Destination directory "${DEST_DIR}" does not exist. Exiting" + exit 1 + fi + TITLE=$(yt-dlp ${DEFAULT_YTDLP_OPTS} -f ${FMT} --simulate -o $DEFAULT_FILE_TEMPLATE --print "%(filename)s" "${URL}") + TEMP=$(mktemp_dir) + echo "Temporarily writing to $TEMP. Will copy to $DEST_DIR on success" + OUTPUT_STR="-o ${TEMP}/%(fulltitle)s.%(ext)s" +fi + +echo "Fetching Video: ${TITLE}" echo "${FMT_LINE}" -echo "yt-dlp ${substr} -f ${FMT}+m4a \"${URL}\"" +echo "yt-dlp ${DEFAULT_YTDLP_OPTS} ${substr} -f ${FMT}+m4a \"${URL}\" ${OUTPUT_STR}" if [ $SKIP_CHECK -eq 1 ]; then read -p "proceed (Y/N): " answer @@ -48,7 +103,38 @@ else fi if [[ "$answer" =~ ^[yY][eE]{0,1}[sS]{0,1}$ ]]; then - yt-dlp ${substr} -f ${FMT}+m4a "${URL}" + + # Actually download the video + yt-dlp ${DEFAULT_YTDLP_OPTS} ${substr} -f ${FMT}+m4a "${URL}" ${OUTPUT_STR} + + # If success do any post work - like moves etc + if [ $? -eq 0 ]; then + # if destination was set - move tmp tile to dest dir + if [ $DEST_FLAG -eq 0 ]; then + # attempt to move the exact file (and optionally subs) + if [ -f $TEMP/${TITLE} ]; then + echo "Moving $TEMP/${TITLE} to $DEST_DIR" + mv -t $DEST_DIR $TEMP/${TITLE} + # copy subs as well + if [ $SUBS_FLAG -eq 0 ]; then + SUB_FILE=$TEMP/${TITLE%.*}.${DESIRED_SUBLANG}.${DESIRED_SUBFMT} + if [ -f $SUB_FILE ]; then + echo "Copying subs as well" + mv -t $DEST_DIR $SUB_FILE + else + echo "Cannot move subs they may've'n't downloaded properly. See logs above - no subs may be available in target lang/at all." + fi + fi + else # move everything somewhat matching as a failsafe (including subs) + echo "File downloaded in non-desired format. Moving all $TEMP/${TITLE%.*}* to $DEST_DIR" + mv -t $DEST_DIR $TEMP/${TITLE%.*}* + fi + fi + else + echo "Download did not run successfully. Exiting" + exit 1 + fi + echo "Done" exit 0 else echo "Okay, cancelling" |