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.
This commit is contained in:
Emre AKYUZ 2023-04-22 11:46:47 +03:00 committed by GitHub
parent 77fd62b9f3
commit 6476766a21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

6
.local/bin/videosearch Normal file
View File

@ -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"