koldfront

dl #shell

🕣︎ - 2025-06-08

One of the small, handy shell scripts I use all the time is called dl. If called with arguments, URLs, it will try downloading each argument using yt-dlp.

That's pretty mundane, basically just easier to spell. But here is the twist: if called without arguments, dl examines the clipboard, looking for a URL, and downloads it if found, then the selection.

So often I will see a link to a video, copy the address and then open a shell, type dl and away we go - skipping the "paste the URl"-step.

Small improvement, but very handy.

P.S. It also retries.

#!/bin/bash

# dl - wrapper to download with yt-dlp from clipboard to ~/video.
#
# Copyright (C) 2020-2025, by Adam Sjøgren  Under the GPLv2.

set -o pipefail

cd ~/video || exit 10

MAX_RETRIES=5

ARGS=( "$@" )
if [ "$*" == "" ]; then
    ARGS[0]=$(xsel --output --clipboard)
    if [[ ${ARGS[0]} != http* ]]; then
        # If not the clipboard, maybe the mouse:
        ARGS[0]=$(xsel --output)
        echo "dl: clipboard is empty, using string from the mouse"
    else
        # Clear clipboard if we got a URL from it, so we are ready for
        # the next, possibly from the mouse:
        echo "dl: using string from the clipboard"
        # This doesn't work, at least not with Emacs:
        #   xsel --clear --clipboard
        # so do this instead:
        echo -n "" | xsel --input --clipboard
    fi
fi

i=0
for url in "${ARGS[@]}"; do
    if [[ "$url" != http* ]]; then
        echo "'$url' doesn't look like a URL"
        exit 10
    fi
    url=$(echo "$url" | sed 's/drtv\/episode\//drtv\/se\//')
    url=$(echo "$url" | sed 's/drtv\/program\//drtv\/se\//')
    if [ $i -gt 0 ]; then
        echo ""
    fi
    FILE=$url
    LAST_ERROR=1
    TRY=1
    echo "Downloading $url ..."
    while ((LAST_ERROR != 0 && TRY < MAX_RETRIES)); do
        if [ $TRY -gt 1 ]; then
            echo "Trying again ($LAST_ERROR, $TRY)"
        fi
        yt-dlp --force-ipv4 --color always --print 'before_dl:%(title)s' --print-to-file before_dl:title "/tmp/dl-out-$$.txt" "$url"
        LAST_ERROR=$?
        FILE=$(cat /tmp/dl-out-$$.txt)
        rm /tmp/dl-out-$$.txt
        TRY=$((TRY + 1))
    done
    if [ $LAST_ERROR != 0 ]; then
        echo "Failed dl $url"
    else
        TITLE=$(ffprobe "$FILE" 2>&1 | grep ' title ' | cut -d: -f2-)
        if [ "$TITLE" == "" ]; then
            TITLE="$FILE"
        fi
        if [ "$TITLE" == "" ]; then
            TITLE="$url"
        fi
        notify-send --icon /usr/share/pixmaps/terminal-tango.svg --expire-time 2000 'dl' "${TITLE} downloaded"
        echo "Done."
    fi
    i=$((i+1))
done

Add comment

How to comment, in excruciating detail…

To avoid spam many websites make you fill out a CAPTCHA, or log in via an account at a corporation such as Twitter, Facebook, Google or even Microsoft GitHub.

I have chosen to use a more old school method of spam prevention.

To post a comment here, you need to:

  • Configure a newsreader¹ to connect to the server koldfront.dk on port 1119 using nntps (nntp over TLS).
  • Open the newsgroup called lantern.koldfront and post a follow up to the article.
¹ Such as Thunderbird, Pan, slrn, tin or Gnus (part of Emacs).

Or, you can fill in this form:

+=