#!/bin/sh
set -e
TMP_PATH=/tmp/yt-dl #$(mktemp -d)
mkdir $TMP_PATH || true
DEST=/mnt/hs2/main/media/storage/music/

# Exit if the temp directory wasn't created successfully.
if [ ! -e "$TMP_PATH" ]; then
    >&2 echo "Failed to create temp directory"
    exit 1
fi


for url in "$@"
do
    yt-dlp "$url" \
        -x --audio-format m4a \
                -o "${TMP_PATH}/%(title)s [%(id)s].%(ext)s" \
        --embed-metadata \
        --embed-thumbnail \
                --embed-subs \
                --sub-langs en-US* \
                --write-auto-sub \
                --sponsorblock-remove all 

done
rsync -r --remove-source-files --ignore-existing  --progress -v "${TMP_PATH}/" "$DEST"

trap "exit 1"           HUP INT PIPE QUIT TERM
trap 'rm -rf "$TEMPD"'  EXIT
root@lambda:~# cat ytdl-bulk.sh 
#!/bin/sh
set -e

TMP_PATH=/tmp/yt-dl
mkdir -p "$TMP_PATH" || true
DEST=/mnt/hs2/main/media/storage/music/

echo "TMP: $TMP_PATH"
echo "DEST: $DEST"

if [ ! -e "$TMP_PATH" ]; then
    >&2 echo "Failed to create temp directory"
    exit 1
fi

trap 'rm -rf "$TMP_PATH"; exit 1' HUP INT PIPE QUIT TERM

echo waiting for links...

# Read URLs from stdin, one per line. No shell interpretation of & etc.
while IFS= read -r url || [ -n "$url" ]; do
    [ -z "$url" ] && continue
    yt-dlp "$url" \
        -x --audio-format m4a \
        -o "${TMP_PATH}/%(title)s [%(id)s].%(ext)s" \
        --embed-metadata \
        --embed-thumbnail \
        --embed-subs \
        --sub-langs en-US* \
        --write-auto-sub \
        --sponsorblock-remove all
done
echo syncing files...
rsync -r --remove-source-files --ignore-existing --progress -v "${TMP_PATH}/" "$DEST"
echo goodbye