#!/bin/bash # MAIN: PLAYLIST VIEW # Displays Playlist, with details such as time and commented out tracks # $PLAYLIST_PATH # Includes and Declarations (global variables, formatting, and functions) source "${APPLICATION_ROOT}/scripts/system/system-functions" source "${APPLICATION_ROOT}/scripts/system/system-declarations" # Arrays Declarations MASTER_USABLE_FILE_CONTENTS_ARRAY=() # FUNCTION: Sum Times function sum_times { # Receive parmeters INBOUND_ARRAY_FOR_SUMMING=("$@") # Get array length INBOUND_ARRAY_FOR_SUMMING_LENGTH=${#INBOUND_ARRAY_FOR_SUMMING[@]} # Sum - Centiseconds in array for ((i=0; i&1 | grep "Duration"); FF_OUTPUT=${FF_OUTPUT:12}; FF_OUTPUT=${FF_OUTPUT::-36}; # Return value echo $FF_OUTPUT; } function calculate_playlist_time { # Instsantiate Array MASTER_USABLE_FILE_CONTENTS_ARRAY=() # Read playlist file contents while IFS= read -r PLAYLIST_FILE_LINE do # Remove leading spaces PLAYLIST_LINE_NO_LEADING_SPACES=$(echo -e "${PLAYLIST_FILE_LINE}" | sed -e 's/^[[:space:]]*//') # If file line is not empty and ends in .mp3 if [[ "$PLAYLIST_LINE_NO_LEADING_SPACES" != "" && $PLAYLIST_LINE_NO_LEADING_SPACES == *.mp3 ]]; then # Add file to master file contents array, get length MASTER_USABLE_FILE_CONTENTS_ARRAY+=("$PLAYLIST_LINE_NO_LEADING_SPACES") # Master usable array length MASTER_USABLE_ARRAY_LENGTH=${#MASTER_USABLE_FILE_CONTENTS_ARRAY[@]} fi done < "$PLAYLIST_PATH" # Create arrays for both commented and non commented lines ARRAY_OF_QUEUED_TIMES=() ARRAY_OF_ALL_TIMES=() for LINE_FOR_HASH_CHECK in "${MASTER_USABLE_FILE_CONTENTS_ARRAY[@]}"; do # If first character beings with hash if [[ ${LINE_FOR_HASH_CHECK:0:1} = "#" ]]; then # Remove Hash # LINE_FOR_HASH_CHECK=$(echo $LINE_FOR_HASH_CHECK | sed -e "s/\#//") LINE_FOR_HASH_CHECK=$(echo -e "${LINE_FOR_HASH_CHECK}" | sed -e 's/^[#| ]*//') LINE_FOR_HASH_CHECK=$(echo -e "${LINE_FOR_HASH_CHECK}" | sed -e 's/^[[:space:]]*//') # Run get_ffprobe_time for value FF_PROBE_RESULT=$(get_ffprobe_time "${LINE_FOR_HASH_CHECK}") # Add to arrays (both, in this case) ARRAY_OF_ALL_TIMES=( "${ARRAY_OF_ALL_TIMES[@]}" "$FF_PROBE_RESULT" ) # Else if line doesn't start with hash else # Run get_ffprobe_time for value FF_PROBE_RESULT=$(get_ffprobe_time "${LINE_FOR_HASH_CHECK}") # # Add to all array only ARRAY_OF_QUEUED_TIMES=( "${ARRAY_OF_QUEUED_TIMES[@]}" "$FF_PROBE_RESULT" ) ARRAY_OF_ALL_TIMES=( "${ARRAY_OF_ALL_TIMES[@]}" "$FF_PROBE_RESULT" ) fi done # Render Results printf "${COLOR_GREEN}[V] Playlist view:${COLOR_DEFAULT}\n"; echo "" echo "" echo " ${FONT_BOLD}PLAYLIST FILES (HH:MM:SS.MS)${FONT_DEFAULT}" echo "" # Numbmering Indexes (the printable ones) FRIENDLY_INDEX=1; COMPLETE_INDEX=1; # Create Array ARRAY_OF_QUEUED_TRACKS=() for (( i = 0; i < $MASTER_USABLE_ARRAY_LENGTH; i++ )); do # Prepare Filename to Echo FILENAME_TO_ECHO=${MASTER_USABLE_FILE_CONTENTS_ARRAY[$i]} FILENAME_TO_ECHO=$(sed -e s/"^.*queued\/"// <<< ${FILENAME_TO_ECHO}) # check if line begins with # if [[ ${MASTER_USABLE_FILE_CONTENTS_ARRAY[$i]:0:1} == "#" ]]; then if [[ ${MASTER_USABLE_FILE_CONTENTS_ARRAY[$i]:0:1} == "#" ]]; then # Prepare "printable complete index" if [[ ${#COMPLETE_INDEX} = 1 ]]; then printf " ${COLOR_GRAY}[0$COMPLETE_INDEX] ${ARRAY_OF_ALL_TIMES[$i]} – # ${FILENAME_TO_ECHO}${COLOR_DEFAULT}\n" else printf " ${COLOR_GRAY}[$COMPLETE_INDEX] ${ARRAY_OF_ALL_TIMES[$i]} – # ${FILENAME_TO_ECHO}${COLOR_DEFAULT}\n" fi fi # Else if line doesn't start with # else # If does not start with # if [[ ${#FRIENDLY_INDEX} = 1 ]]; then printf " 0$FRIENDLY_INDEX" else printf " $FRIENDLY_INDEX" fi FRIENDLY_INDEX=$((FRIENDLY_INDEX+1)) # Prepare "printable complete index" (later gets indexed outside loop) if [[ ${#COMPLETE_INDEX} = 1 ]]; then printf " $SYMBOL_RAQUO [0$COMPLETE_INDEX] " else printf " $SYMBOL_RAQUO [$COMPLETE_INDEX] " fi # Echo Time echo -e " ${ARRAY_OF_ALL_TIMES[$i]} – ${FILENAME_TO_ECHO}" fi # Index "complete index" COMPLETE_INDEX=$((COMPLETE_INDEX+1)) done # Deincrement friendly index, because increment happens in loop after it's printed FRIENDLY_INDEX=$(("$FRIENDLY_INDEX-1")) # Get final count of queued times FINAL_COUNT_OF_QUEUED_ITEMS=${#ARRAY_OF_QUEUED_TIMES[@]} # Unsetariables and get sum of time arrays (for all files and queued files) SUM_OF_QUEUED_TIMES="" # SUM_OF_NT_OF_ALL_ITEMS=${#ARRAY_OF_ALL_TIMES[@]}ALL_TIMES="" SUM_OF_QUEUED_TIMES=$(sum_times "${ARRAY_OF_QUEUED_TIMES[@]}") SUM_OF_ALL_TIMES=$(sum_times "${ARRAY_OF_ALL_TIMES[@]}") # Echo Output echo -e "" echo -e " ${FONT_BOLD}Total (${FINAL_COUNT_OF_QUEUED_ITEMS}/${MASTER_USABLE_ARRAY_LENGTH} tracks): $SUM_OF_QUEUED_TIMES / $SUM_OF_ALL_TIMES${FONT_DEFAULT}\n" echo -e "" echo -e "" echo -e "" } function verify_playlist_integrity() { # Clear Array INTEGRITY_CHECK_FILE_LINES_ARRAY=() # Declare integrity check flag INTEGRITY_CHECK_FLAG="true" # Read playlist file contents while IFS= read -r PLAYLIST_FILE_LINE do # Remove leading spaces PLAYLIST_LINE_WITH_NO_LEADING_SPACES=$(echo -e "${PLAYLIST_FILE_LINE}" | sed -e 's/^[[:space:]]*//') # If line isn't blank if [[ -n $PLAYLIST_LINE_WITH_NO_LEADING_SPACES ]]; then # If ends in .mp3 if [[ "$PLAYLIST_LINE_WITH_NO_LEADING_SPACES" == *.mp3 ]]; then # remove pound PLAYLIST_LINE_WITH_NO_LEADING_POUND=$(echo -e "${PLAYLIST_LINE_WITH_NO_LEADING_SPACES}" | sed -e 's/^[#| ]*//') # Add file to master file contents array, get length INTEGRITY_CHECK_FILE_LINES_ARRAY+=("$PLAYLIST_LINE_WITH_NO_LEADING_POUND") fi # If line is blank (then don't add to array / continue) else continue fi done < "$PLAYLIST_PATH" # Check that a file exists for each playlist line for INTEGRITY_CHECK_ITEM in "${INTEGRITY_CHECK_FILE_LINES_ARRAY[@]}" do # If doesn't exist then throw notice if [[ ! -f "$INTEGRITY_CHECK_ITEM" ]]; then # Flag as failed INTEGRITY_CHECK_FLAG="false" fi done echo $INTEGRITY_CHECK_FLAG } # If playlist file exists, then do routine if test -f "$PLAYLIST_PATH"; # Execute calcualte_playlist_time then MASTER_PLAYLIST_INTEGRITY_CHECK=$(verify_playlist_integrity) if [[ "$MASTER_PLAYLIST_INTEGRITY_CHECK" == "true" ]]; # If playlist integrity check passed then echo "Calculating playlist time..." calculate_playlist_time # else error else error_message "[V] View playlist: playlist contains invalid refs. Please edit or recreate for summary." echo "" fi # else if playlist file doesn't exist else error_message "[V] View playlist: playlist file not found. Run \"C\" to create."; echo "" fi