Post your handy utility scripts!

Maybe someone may like this. After ytfzf stopped working for me, I wrote a small script that uses (and needs) yt-dlp, fzf and mpv to search and play YouTube videos. The functions are made for fish shell. You can simply put them into your config.fish file.

If you use bash or zsh, it shouldn’t be too hard rewriting the functions. I can only recommend running fish as an interactive shell inside your terminal. It’s so god damn good out of the box.

For searching in general:
ys endeavourOS test

For searching latest videos of a channel:
yc CHANNELNAME

For using a pre-defined channel list (defined inside function):
yl

Here’s the code (edited once for new version):

# Function to select a YouTube video from a list.
# Requires fzf and yt-dlp to be installed.
# Put this code into config.fish or into ~/.config/fish/conf.d/ytfzf.fish.
# select_yt is not meant to be used. Use ys, yc, yl for searching and playing videos.

function select_yt
    set temp_file "/var/tmp/ytfzf_list.txt"
    set video_titles
    set video_urls
    set line_index 0

    if not test -e $temp_file
        echo "Error: $temp_file does not exist."
    end

    if not command -v fzf >/dev/null
        echo "Error: fzf is not installed."
        return 1
    end

    if not command -v mpv >/dev/null
        echo "Error: mpv is not installed."
        return 1
    end

    # Parse the file into titles and URLs
    for line in (cat $temp_file)
        if test (math $line_index % 2) -eq 0
            set video_titles $video_titles $line
        else
            set video_urls $video_urls $line
        end
        set line_index (math $line_index + 1)
    end

    # Use fzf to select a title.
    set selected_title (printf "%s\n" $video_titles | command fzf)

    if test $status -ne 0
        echo "Aborted fzf selection..."
        return 1
    end

    # Find the index of the selected title.
    set line_index 0
    for title in $video_titles
        if test "$title" = "$selected_title"
            set selected_index (math $line_index + 1)
        end
        set line_index (math $line_index + 1)
    end

    echo "Starting the selected video in mpv! Might take a second..."
    command mpv "$video_urls[$selected_index]" </dev/null &>/dev/null &
    disown
end

# Function to fetch the ten latest videos from a YouTube channel.
# Usage example: yc DeStaatOfficial
function yc
    set temp_file "/var/tmp/ytfzf_list.txt"

    if test -z "$argv"
        echo "Error: No channel string provided."
        return 1
    end

    if not command -v yt-dlp >/dev/null
        echo "Error: yt-dlp is not installed."
        return 1
    end

    if test -e $temp_file
        command rm $temp_file >/dev/null # Delete temp_file, because --print-to-file doesn't overwrite the temp_file, but appends to it.
    end
    command yt-dlp --quiet -I1:10 --flat-playlist --lazy-playlist --print-to-file title,webpage_url $temp_file "https://www.youtube.com/@$argv/videos" 2>/dev/null

    if test $status -ne 0
        echo "Something went wrong with yt-dlp..."
        return 1
    end

    select_yt
end

# Function to search YouTube videos. Returns ten search results.
# Usage example: ys De Staat Peptalk
function ys
    set temp_file "/var/tmp/ytfzf_list.txt"

    if test -z "$argv"
        echo "Error: No search string provided."
        return 1
    end

    if not command -v yt-dlp >/dev/null
        echo "Error: yt-dlp is not installed."
        return 1
    end

    command yt-dlp ytsearch10:"$argv" --flat-playlist --print title,webpage_url >$temp_file

    if test $status -ne 0
        echo "Something went wrong with yt-dlp..."
        return 1
    end

    select_yt
end

# Function to list and select from predefined channels.
# Add channels to the predefined_channels list.
# Usage: yl
function yl
    set predefined_channels CHANNEL1 CHANNEL2

    if not command -v fzf >/dev/null
        echo "Error: fzf is not installed."
        return 1
    end

    set selected_channel (printf "%s\n" $predefined_channels | command fzf)

    if test $status -ne 0
        echo "Aborted fzf selection..."
        return 1
    end

    yc $selected_channel
end
4 Likes