From 6476766a210727366470f23f58c38c21bba38120 Mon Sep 17 00:00:00 2001 From: Emre AKYUZ Date: Sat, 22 Apr 2023 11:46:47 +0300 Subject: [PATCH 1/7] Extremely Simple and Efficient Script I Think You Like The inspiration: (Luke Smith's Video: "Unix Chad JUST WON'T STOP Piping!" youtu.be/w_37upFE-qw Execution Time: 0.15s to 0.30s on my system (opening a video then closing it to see the output of "time" command) Very Short Summary: 1- This script feeds the "dmenu" with a list of all videos in a hard drive. 2- File paths and extensions are removed for better readability for the dmenu list. 3- Opens the chosen video file in "mpv" using the complete paths and extensions. 4- An approach of having the least amount of lines & characters while keeping the simplicity and performance has been used. updatedb command needs to be executed at first showing the database file "~/.config/.mymlocate.db" Or the script should be changed with a new path. For example: $updatedb -o ~/.config/.mymlocate.db -U /mnt/externaldrive Thoroughly detailed explanation for everyone: temp_file=$(mktemp): This command creates a temporary file using the mktemp command and stores its path in the temp_file variable. This temporary file will be used later to store the search results. locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\...)$': This command searches for files with the ".mp4" or other extension in the database located at "~/.config/.mymlocate.db". The -d flag specifies the database path, the -b flag searches for files with matching base names (ignores the path), and the -r flag allows the use of regular expressions for matching. awk -F/ '{name=$NF; gsub(/\.(mp4|mkv)$/, "", name); print name "\t" $0}': This command processes the search results obtained from the locate command. It uses / as a field separator and works on each line of the output: name=$NF: Stores the last field (file name with extension) in the name variable. gsub(/\.(mp4|mkv)$/, "", name): Removes the ".mp4" or ".mkv" file extension from the name variable. print name "\t" $0: Prints the modified file name (without extension) followed by a tab character and the original line (complete file path with the extension). > "$temp_file": Redirects the output of the previous awk command to the temporary file created earlier. chosen_file_name_and_path=$(cut -f1 "$temp_file" | dmenu -i -l 10 -p "Select a video file:"): This command reads the file names without extensions from the temporary file, presents them to the user using dmenu, and stores the user's selection in the chosen_file_name_and_path variable. The cut -f1 command extracts the file names, and the dmenu -i -l 10 -p "Select a video file:" command displays the list in a menu with a prompt. mpv "$(awk -F'\t' -v chosen="$chosen_file_name_and_path" '$1 == chosen {print $2; exit}' "$temp_file")": This command plays the selected file using mpv. It uses awk to search the temporary file for the line where the first field (file name without extension) matches the user's selection and then extracts the second field (the complete file path with the extension) to pass it as an argument to the mpv command. && rm "$temp_file": After the mpv command has finished executing, this command removes the temporary file. --- .local/bin/videosearch | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .local/bin/videosearch diff --git a/.local/bin/videosearch b/.local/bin/videosearch new file mode 100644 index 00000000..e696eb24 --- /dev/null +++ b/.local/bin/videosearch @@ -0,0 +1,6 @@ +#!/bin/bash + +temp_file=$(mktemp) +locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\wmv\|flv\|avi\)$' | awk -F/ '{name=$NF; gsub(/\.(mp4|mkv|webm|mov|m4v|wmv|flv|avi)$/, "", name); print name "\t" $0}' > "$temp_file" +chosen_file_name_and_path=$(cut -f1 "$temp_file" | dmenu -i -l 10 -p "Select a video file:") +mpv "$(awk -F'\t' -v chosen="$chosen_file_name_and_path" '$1 == chosen {print $2; exit}' "$temp_file")" && rm "$temp_file" From fe8f0bce94e051c0a88972134aafad6050ae8149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Wed, 18 Oct 2023 03:04:57 +0300 Subject: [PATCH 2/7] improve && simplify --- .local/bin/videosearch | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/.local/bin/videosearch b/.local/bin/videosearch index e696eb24..09df1878 100644 --- a/.local/bin/videosearch +++ b/.local/bin/videosearch @@ -1,6 +1,10 @@ -#!/bin/bash +#!/bin/sh -temp_file=$(mktemp) -locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\wmv\|flv\|avi\)$' | awk -F/ '{name=$NF; gsub(/\.(mp4|mkv|webm|mov|m4v|wmv|flv|avi)$/, "", name); print name "\t" $0}' > "$temp_file" -chosen_file_name_and_path=$(cut -f1 "$temp_file" | dmenu -i -l 10 -p "Select a video file:") -mpv "$(awk -F'\t' -v chosen="$chosen_file_name_and_path" '$1 == chosen {print $2; exit}' "$temp_file")" && rm "$temp_file" +tempfile=$(mktemp) +locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\|wmv\|flv\|avi\)$' > "$tempfile" + +chosen_name=$(sed 's|.*/||; s/\.[^.]*$//' "$tempfile" | dmenu -i -l 10 -p "Select a video file:") +chosen_path=$(grep "/$chosen_name\." "$tempfile") + +mpv "$chosen_path" +rm -f "$tempfile" From 533c21c3af0e02b35fa7cb9c0d85c6c224dcba71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Thu, 9 Nov 2023 11:25:02 +0300 Subject: [PATCH 3/7] Use RAM instead of I/O | Minimize further --- .local/bin/videosearch | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/.local/bin/videosearch b/.local/bin/videosearch index 09df1878..259c2b16 100644 --- a/.local/bin/videosearch +++ b/.local/bin/videosearch @@ -1,10 +1,4 @@ #!/bin/sh - -tempfile=$(mktemp) -locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\|wmv\|flv\|avi\)$' > "$tempfile" - -chosen_name=$(sed 's|.*/||; s/\.[^.]*$//' "$tempfile" | dmenu -i -l 10 -p "Select a video file:") -chosen_path=$(grep "/$chosen_name\." "$tempfile") - -mpv "$chosen_path" -rm -f "$tempfile" +video_files=$(locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\|wmv\|flv\|avi\)$') +chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | dmenu -p "Select Video") +mpv "$(echo "$video_files" | grep "/$chosen_file.")" From 18018b18ff0646d82f7d85471d755a4c73daecdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Fri, 10 Nov 2023 09:55:38 +0300 Subject: [PATCH 4/7] fgrep is 0.00001% faster and safer. --- .local/bin/videosearch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.local/bin/videosearch b/.local/bin/videosearch index 259c2b16..ce979ba0 100644 --- a/.local/bin/videosearch +++ b/.local/bin/videosearch @@ -1,4 +1,4 @@ #!/bin/sh video_files=$(locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\|wmv\|flv\|avi\)$') chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | dmenu -p "Select Video") -mpv "$(echo "$video_files" | grep "/$chosen_file.")" +mpv "$(echo "$video_files" | fgrep "/$chosen_file.")" From 4c80fed876eb22912e0385d191a2c7673d8cb59b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Sat, 18 Nov 2023 22:39:31 +0300 Subject: [PATCH 5/7] fgrep outputs a deprecation message --- .local/bin/videosearch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.local/bin/videosearch b/.local/bin/videosearch index ce979ba0..5a4aa400 100644 --- a/.local/bin/videosearch +++ b/.local/bin/videosearch @@ -1,4 +1,4 @@ #!/bin/sh video_files=$(locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\|wmv\|flv\|avi\)$') chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | dmenu -p "Select Video") -mpv "$(echo "$video_files" | fgrep "/$chosen_file.")" +mpv "$(echo "$video_files" | grep -F "/$chosen_file.")" From 9d921b33afcae945ed36671c7ffb5a02559f1ee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Sat, 18 Nov 2023 23:43:26 +0300 Subject: [PATCH 6/7] Add dropdown menu --- .local/bin/videosearch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.local/bin/videosearch b/.local/bin/videosearch index 5a4aa400..9d3fe1d8 100644 --- a/.local/bin/videosearch +++ b/.local/bin/videosearch @@ -1,4 +1,4 @@ #!/bin/sh video_files=$(locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\|wmv\|flv\|avi\)$') -chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | dmenu -p "Select Video") +chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | dmenu -l 10 -p "Select Video") mpv "$(echo "$video_files" | grep -F "/$chosen_file.")" From e259c2af0a25a893e8e5033874e5bc1985cb10e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Sun, 19 Nov 2023 00:54:48 +0300 Subject: [PATCH 7/7] Make dmenu case insensitive --- .local/bin/videosearch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.local/bin/videosearch b/.local/bin/videosearch index 9d3fe1d8..eb19d92d 100644 --- a/.local/bin/videosearch +++ b/.local/bin/videosearch @@ -1,4 +1,4 @@ #!/bin/sh video_files=$(locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\|wmv\|flv\|avi\)$') -chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | dmenu -l 10 -p "Select Video") +chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | dmenu -i -l 10 -p "Select Video") mpv "$(echo "$video_files" | grep -F "/$chosen_file.")"