clean up code and add ability to search for and play mp3 files

This commit is contained in:
ksyasuda 2022-01-04 11:39:25 -08:00
parent f5219775e4
commit 78ef98562b

View File

@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# set -Eeo pipefail set -Eeo pipefail
############# #############
# Globals # # Globals #
@ -16,10 +16,11 @@ GET_QUALITY=0
VERBOSE=0 VERBOSE=0
IS_ROFI=1 IS_ROFI=1
IS_DOWNLOAD=0 IS_DOWNLOAD=0
IS_PLAY_FROM_FILE=0
quit="6. Quit" quit="6. Quit"
options="1. Stream|2. Download|3. Continue|4. Play from File|5. Sync History|$quit" options="1. Stream|2. Download|3. Continue|4. Play from File|5. Sync History|$quit"
playable="\.mp4|\.mkv|\.ts" playable="\.mp4|\.mkv|\.ts|\.mp3"
############# #############
# Functions # # Functions #
@ -53,6 +54,13 @@ run() {
fi fi
} }
create_default_download() {
# make sure download directory exists
if [ ! -d "$DEFAULT_DOWNLOAD" ]; then
mkdir -p "$DEFAULT_DOWNLOAD"
fi
}
get_quality() { get_quality() {
if [ "$IS_ROFI" -eq 1 ]; then if [ "$IS_ROFI" -eq 1 ]; then
selection=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \ selection=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \
@ -71,10 +79,14 @@ play_file() {
log "Checking if file is playable" log "Checking if file is playable"
if [[ "$1" =~ ($playable)$ ]]; then if [[ "$1" =~ ($playable)$ ]]; then
log "File is playable..." log "File is playable..."
log "Playing file: $1" if [[ "$1" =~ .mp3 ]]; then
if [[ "$VERBOSE" -eq 1 ]]; then log ".mp3 file found... playing without video"
$PLAYER_CMD --no-video --volume=50 "$1"
elif [[ "$VERBOSE" -eq 1 ]]; then
log "Playing file: $1"
$PLAYER_CMD "$1" $PLAYER_CMD "$1"
else else
log "Playing file: $1"
$PLAYER_CMD "$1" &> /dev/null & $PLAYER_CMD "$1" &> /dev/null &
fi fi
exit $? exit $?
@ -105,6 +117,7 @@ generate_inputlist() {
outstr="$outstr|$directory" outstr="$outstr|$directory"
fi fi
done <<< "$(find "$1" -maxdepth 1 -type d | sed "s|$1/||" | tail -n +2 | sort -V)" done <<< "$(find "$1" -maxdepth 1 -type d | sed "s|$1/||" | tail -n +2 | sort -V)"
log "DIRS: $outstr" 1> /dev/stderr
while read -r filename; do while read -r filename; do
if [[ "${filename// /}" == "" ]]; then if [[ "${filename// /}" == "" ]]; then
continue continue
@ -115,13 +128,17 @@ generate_inputlist() {
outstr="$outstr|$filename" outstr="$outstr|$filename"
fi fi
done <<< "$(find "$1" -maxdepth 1 -type f | sed "s|$1/||" | grep -E "$playable$" | sort -V)" done <<< "$(find "$1" -maxdepth 1 -type f | sed "s|$1/||" | grep -E "$playable$" | sort -V)"
outstr="$outstr|Quit" log "DIRS + FILES: $outstr" 1> /dev/stderr
outstr="$outstr|Back|Quit"
printf "%s\n" "$outstr" printf "%s\n" "$outstr"
} }
# recursive function for finding path to video file given a starting directory # recursive function for finding path to video file given a starting directory
find_videos() { find_media() {
inp="$1" inp="$1"
[ -z "$inp" ] && inp="/"
# workaround to allow logging w/o affecting return output
log "INPUT DIR: $inp" 1> /dev/stderr
# base case hit when a file is found # base case hit when a file is found
if [ -f "$inp" ]; then if [ -f "$inp" ]; then
@ -136,16 +153,41 @@ find_videos() {
if [ -z "$selection" ] || [ "$selection" = "Quit" ]; then if [ -z "$selection" ] || [ "$selection" = "Quit" ]; then
return 1 return 1
elif [ "$selection" = "Back" ]; then
# go up one directory
find_media "$(sed -E "s/(\/[^\/]*$)//" <<< "$inp")"
elif [ -d "$inp/$selection" ] || [ -f "$inp/$selection" ]; then elif [ -d "$inp/$selection" ] || [ -f "$inp/$selection" ]; then
find_videos "$inp/$selection" find_media "$inp/$selection"
return $? return $?
fi fi
} }
play_from_file() {
if [[ $# -eq 0 ]]; then
msg="Provide a valid path to a directory or leave blank to go with the default: $HOME/Videos/sauce/"
msg="$(printf '%s\n%s\n' "$msg" "The program will begin searching for media files from the supplied directory")"
span=$(generate_span "$msg")
play_dir=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \
-l 1 -mesg "$span" -p "Enter path to starting directory:")
else
play_dir="$1"
fi
[ -z "$play_dir" ] && play_dir="$DEFAULT_DOWNLOAD"
[ "$play_dir" = "$DEFAULT_DOWNLOAD" ] && create_default_download
log "STARTING DIR: $play_dir"
[ ! -d "$play_dir" ] && seppuku "$play_dir does not exist"
video_path=$(find_media "$play_dir") || quit
log "VIDEO PATH: $video_path"
if [ -z "$video_path" ]; then
seppuku "Something went wrong getting path... path is empty"
fi
play_file "$video_path"
}
######## ########
# Main # # Main #
######## ########
while getopts 'vhqcd' OPT; do while getopts 'vhqcdf:-:' OPT; do
case "$OPT" in case "$OPT" in
h) h)
help_text help_text
@ -165,6 +207,12 @@ while getopts 'vhqcd' OPT; do
IS_DOWNLOAD=1 IS_DOWNLOAD=1
log "Download flag set..." log "Download flag set..."
;; ;;
f)
IS_PLAY_FROM_FILE=1
play_path="$OPTARG"
log "Play from file flag set... skipping main menu"
log "PLAY_PATH: $play_path"
;;
*) *)
log "Invalid option" log "Invalid option"
exit 1 exit 1
@ -185,6 +233,9 @@ elif [[ "$IS_ROFI" -eq 0 ]] && [[ "$IS_DOWNLOAD" -eq 1 ]]; then
[ -d "$dl_dir" ] && mkdir -p "$dl_dir" || dir "Error creating directory: $dl_dir" [ -d "$dl_dir" ] && mkdir -p "$dl_dir" || dir "Error creating directory: $dl_dir"
run "-cd $dl_dir" run "-cd $dl_dir"
exit $? exit $?
elif [[ "$IS_ROFI" -eq 1 ]] && [[ "$IS_PLAY_FROM_FILE" -eq 1 ]]; then
play_from_file "$play_path"
exit $?
fi fi
# ------------------------------------------------------------------------------- # -------------------------------------------------------------------------------
@ -214,10 +265,7 @@ case "$selection" in
-l 1 -p "Enter download dir:") -l 1 -p "Enter download dir:")
# if dl_dir is none set to current directory # if dl_dir is none set to current directory
[ "$dl_dir" == "" ] && dl_dir="$DEFAULT_DOWNLOAD" [ "$dl_dir" == "" ] && dl_dir="$DEFAULT_DOWNLOAD"
# make sure download directory exists create_default_download
if [ ! -d "$DEFAULT_DOWNLOAD" ]; then
mkdir -p "$DEFAULT_DOWNLOAD"
fi
run -d "$dl_dir" run -d "$dl_dir"
;; ;;
3.) 3.)
@ -232,19 +280,8 @@ case "$selection" in
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# play # play
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
msg="Enter path to the videos directory or leave blank to go with the default: $HOME/Videos/sauce/" log "Play from file selected"
span=$(generate_span "$msg") play_from_file
play_dir=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \
-l 1 -mesg "$span" -p "Enter path to play dir:")
[ -z "$play_dir" ] && play_dir="$DEFAULT_DOWNLOAD"
log "PLAY DIR: $play_dir"
[ ! -d "$play_dir" ] && seppuku "$play_dir does not exist"
video_path=$(find_videos "$play_dir") || quit
log "VIDEO PATH: $video_path"
if [ -z "$video_path" ]; then
seppuku "Something went wrong getting path"
fi
play_file "$video_path"
;; ;;
5.) 5.)
log "Sync history database" log "Sync history database"