This script will get MPV watch history from files watch_later dir and display them in reverse order of watch. The list is numbered and a prompt for a number will play the desired file in MPV
Need line “write-filename-in-watch-later-config=yes” in mpv.conf Deps rg (ripgrep)
#!/usr/bin/env bash
# Return mpv watch history oldest to newest.
# Need line "write-filename-in-watch-later-config=yes" in mpv.conf
# Deps rg
watch_later_dir="$HOME/.config/mpv/watch_later/"
SAVEIFS=$IFS
IFS=$'\n'
if [ ! -d "$watch_later_dir" ]; then
echo "Specified dir doesn't exist: $watch_later_dir"
echo "Set var watch_later_dir to your watch later dir"
echo "also, mpv.conf should have line \"write-filename-in-watch-later-config=yes\""
exit 1
fi
watch_later_files="$(find "$watch_later_dir" -type f -printf "%T@ %p\n" | sort | sed 's/^\([0-9]\+\.[0-9]\+\) //')"
file_count=$(find "$watch_later_dir" -type f | wc -l)
if [ "$file_count" -eq 0 ]; then
echo "no files found in \"$watch_later_dir\""
exit 1
fi
watch_later_files=($watch_later_files)
filepaths_not_echoed="$(for (( i=0; i<${#watch_later_files[@]}; i++ ))
do
cat "${watch_later_files[$i]}" | rg -o --color=never '(/|http).*'
done)"
filepaths_not_echoed=($filepaths_not_echoed)
# Reverse the order of array
length=${#filepaths_not_echoed[@]}
for ((i=0; i<length/2; i++)); do
temp="${filepaths_not_echoed[i]}"
filepaths_not_echoed[i]="${filepaths_not_echoed[length-i-1]}"
filepaths_not_echoed[length-i-1]="$temp"
done
filepaths="$(for (( i=0; i<${#watch_later_files[@]}; i++ ))
do
echo -n "$(( $i - $file_count +1 )) " | sed 's/^-//'
cat "${watch_later_files[$i]}" | rg -o --color=never '/.*'
done)"
#echo "$filepaths" | perl -pe 's/^(\d+ ).*\//$1/g' | rg \
echo "$filepaths" | sed -E 's/^([0-9]+ ).*\//\1/g' | rg \
--colors 'match:none' \
--colors 'match:fg:0,200,0' \
--colors 'match:bg:0,0,0' \
--colors 'match:style:bold' \
"[^0-9 ].*"
IFS=$SAVEIFS
read -p "Enter number to play " selection
echo "${filepaths_not_echoed[$selection]}"
setsid >/dev/null 2>&1 </dev/null \
mpv "${filepaths_not_echoed[$selection]}" 2>&1 >/dev/null &
You must log in or register to comment.