From 56fd5566a9d150b967bf486e614c62378b6e699d Mon Sep 17 00:00:00 2001 From: iconoclast hero Date: Sun, 28 Apr 2024 08:27:05 -0400 Subject: [PATCH] update --- indexopus | 65 ++++++++++++++++++++++-------------------- m4b2opus | 85 ++++++++++++++++++++++++++++++++++++++++++++++++------- rmr | 4 +-- 3 files changed, 111 insertions(+), 43 deletions(-) diff --git a/indexopus b/indexopus index bc60fe4..f4f98d6 100755 --- a/indexopus +++ b/indexopus @@ -13,7 +13,7 @@ # https://mywiki.wooledge.org/BashFAQ/054 -- How can I tell whether a variable contains a valid number? -shopt -s extglob +shopt -s extglob nullglob dotglob scriptname=$(realpath "$0") ulon="$(tput smul)" #underline on uloff="$(tput rmul)" #underline off @@ -21,6 +21,7 @@ red="$(tput setaf 9)" #red fg white="$(tput setaf 15)" #white fg tput0="$(tput sgr0)" #reset tput changes bold="$(tput bold)" #bold on +relipsis="$red..." editscript(){ local script path; script="${scriptname##*/}"; path="${scriptname%/*}"; swp="$path/.$script.swp" @@ -152,16 +153,16 @@ title=( *\ --\ Audiobook.opus ) if [[ "$(opustags "$title")" = *CHAPTER* ]] then - printf '\n%s%s%s contains existing tags; removing...%s\n' "$red" "$bold" "$title" "$tput0" + printf '\n%s%s%s contains existing tags; removing...%s\n' "$relipsis" "$bold" "$title" "$tput0" tags=( $(opustags "$title") ) tags=( $(printf -- '-d%s\n' "${tags[@]%=*}"|\grep CHAPTER|sort -u) ) - printf '\n%s%s...mediainfo of existing %s:%s\n' "$red" "$bold" "$title" "$tput0" + printf '\n%s%smediainfo of existing %s:%s\n' "$relipsis" "$bold" "$title" "$tput0" mediainfo "$title" - printf '\n%s...removing exiting CHAPTER tags with opustags...%s\n\n' "$red" "$tput0" + printf '\n%sremoving exiting CHAPTER tags with opustags...%s\n\n' "$relipsis" "$tput0" opustags "${tags[@]}" -i "$title" ck4tags=( "$(opustags "$title" | grep --color=always -i chapter)" ) && printf 'Error!\nCHAPTER tags still present in opus file.\n%s' "${ck4tags[@}}" && exit -# printf '%s%s...mediainfo of %s after tag removal:%s\n' "$red" "$bold" "$title" "$tput0" +# printf '%s%smediainfo of %s after tag removal:%s\n' "$relipsis" "$bold" "$title" "$tput0" # mediainfo "$title" # if [[ "$rmmatch" != true ]] # then @@ -254,6 +255,7 @@ while read -r chaptag opustags -i "$writetitle" "${args[@]}" printf '%s...Chapter info added...\n\n%s' "$red" "$tput0" + } ##writeindex()################################################################################# @@ -285,25 +287,32 @@ if [[ "$reindex" = true ]] exit fi -if ! compgen -G @(*\ --\ Part\ ???\:\ *.opus|*\ --\ Part\ ??\:\ *.opus) > /dev/null + +if ! compgen -G @(*\ --\ Part\ ???\:\ *.opus|*\ --\ Part\ ??\:\ *.opus) &> /dev/null then - if compgen -G @(*\ --\ Chapter\ ??*.opus|*\ --\ Disc\ ??.opus) > /dev/null + if compgen -G @(*\ --\ Chapter\ ??*.opus|*\ --\ Disc\ ??.opus|*-Part??.opus) > /dev/null then for renopus in *Chapter\ ??*.opus; do [[ -f "$renopus" ]] && mv "$renopus" "${renopus/ Chapter / Part }"; done for renopus in *Disc\ ??.opus; do [[ -f "$renopus" ]] && mv "$renopus" "${renopus/ Disc / Part }"; done + for renopus in *-Part??.opus; do [[ -f "$renopus" ]] && mv "$renopus" "${renopus/-Part/ -- Part 00: Part }"; done fi - renopus=(*Part\ 01*.opus) - if [[ "${renopus[0]/ -- Part / -- Part 00: Part }" == *\ --\ Part\ ??\:\ *.opus ]] + renopus=( *\ --\ Part\ *.opus ) + +# if [[ "${renopus[0]}" != @(*\ --\ Part\ ???\:\ *.opus|*\ --\ Part\ ??\:\ *.opus) ]] + if ! compgen -G @(*\ --\ Part\ ???\:\ *.opus|*\ --\ Part\ ??\:\ *.opus) &> /dev/null then - for i in *opus - do - n="${i#* -- Part }"; n="${n%%.opus}" - mv "$i" "${i/ -- Part / -- Part $n: Part }" - done - else - printf 'This directory does not contain files in the format of %s%s -- Part ##:<Chapter Title>.opus\nPlease check the directory this script was launched from to make sure it contains the proper files.\n' "${ulon}" "${uloff}" - exit + if [[ "${renopus[0]/ -- Part / -- Part 00: Part }" == *\ --\ Part\ ??\:\ *.opus ]] + then + for i in *opus + do + n="${i#* -- Part }"; n="${n%%.opus}" + mv "$i" "${i/ -- Part / -- Part $n: Part }" + done + else + printf 'This directory does not contain files in the format of %s<Title>%s -- Part ##:<Chapter Title>.opus\nPlease check the directory this script was launched from to make sure it contains the proper files.\n' "${ulon}" "${uloff}" + exit + fi fi fi @@ -317,14 +326,12 @@ gettitle "$title" echo "$title" - if [[ -f ./"$output" ]] && [[ "$rmmatch" != true ]] then printf 'This directory already contains %s\n' "$ioutput" if confirm "Overwrite the existing file? (y/n)" then printf '\n...Overwriting %s -- Audiobook.opus...\n' "$ititle" - elipsis="..." else exit fi @@ -360,7 +367,7 @@ done ffmpeg -nostdin -hide_banner -stats -f concat -safe 0 -thread_queue_size 1024 -i "$tmp/opusfiles" -n -acodec copy "/tmp/$output" [[ "$?" != 0 ]] && echo && echo "ffmpeg failed to concatinate; exit 1" && echo && exit 1 -printf '\n\n...Incorporating index into %s -- Audiobook.opus...\n\n' "$ititle" +printf '\n\n%sincorporating index into%s %s -- Audiobook.opus%s\n\n%s' "$relipsis" "$white" "$ititle" "$relipsis" "$tput0" mv "/tmp/$output" "$output" @@ -375,24 +382,21 @@ mv "/tmp/$output" "$output" writeindex "$output" -printf '...Chapter info added...\n\n' - - if [[ -f "$output" ]] && eval command -v mediainfo &> /dev/null then - printf '...Checking output from mediainfo...\n\n' + printf '%schecking output from mediainfo...\n\n%s' "$relipsis" "$tput0" outdur="$(mediainfo -f "$output"|grep Duration|head -n1)" outdur="${outdur#*: }" outdur="$(echo "$outdur" | perl -nle '/([0-9\.]+)/ && ($t += $1) && printf "%02d:%02d:%02d\n", $t/3600000, ($t/60000)%60, ($t/1000)%60')" - data=( "${bold}Duration(s):" - "${red} $duration ${white}${title} -- Part *opus $tput0" - "${red} $outdur ${white}${title} -- Audiobook.opus $tput0") + data=( "${bold}Duration(s):${tput0}" + "${red}${bold} $duration ${tput0}${ititle} -- Part *opus $tput0" + "${red}${bold} $outdur ${tput0}${ititle} -- Audiobook.opus $tput0") # Print the aligned data using column, right justifying durations - printf '%s\n\n\n' "${data[@]}" #| column -s "~" -t -R 2 - + printf '%s\n' "${data[@]}" #| column -s "~" -t -R 2 + echo elif [[ -f "$output" ]] && eval command -v ffprobe &> /dev/null then printf 'Checking the output from ffprobe...\n\n' @@ -406,8 +410,7 @@ fi [[ "$rmmatch" ]] && [[ "$duration" = "$outdur" ]] && - printf 'Durations of part and indexed opus files match, deleting temporary files.\n\n...Done.\n\nPlease see %s/%s\n\n' "$PWD" "$ioutput" && - pause "rm -r $title -- Part ??:*.opus .tmp &&" && + printf 'Durations of part and indexed opus files match, automatically deleting temporary files!\n\n%s%sPlease see %s/%s%s\n\n%sDone.\n\n%s' "$white" "$bold" "$PWD" "$ioutput" "$tput0" "$relipsis" "$tput0" && rm -r "$title"\ --\ Part\ ??:*.opus "$tmp" && exit diff --git a/m4b2opus b/m4b2opus index 94c66aa..8d6de06 100755 --- a/m4b2opus +++ b/m4b2opus @@ -16,14 +16,75 @@ white="$(tput setaf 15)" #white fg pause(){ read -p "$*" ; } ##pause()##################################################################################### + +rmr(){ + fn_rmfile(){ + printf "%s\n%s is a file:\n" "$bold" "$1" + ls -ltr "$1" + printf '\nrmr: remove this file? (y/n) %s' "$tput0" + old_stty_cfg=$(stty -g) +# https://stackoverflow.com/questions/226703/how-do-i-prompt-for-yes-no-cancel-input-in-a-linux-shell-script + stty raw -echo + answer=$(head -c 1) + stty "$old_stty_cfg" +# Careful playing with stty + + if [ "$answer" != "${answer#[Yy]}" ];then + rm "$1"; printf "\n\n"; if [ ! -e "$1" ]; then printf "\nDone! File \"%s\" removed.\n\n" "$1"; else printf "\n\n%s%s not removed, check permissions:\n\n" "$bold" "$1"; ls -ltr "$1"; fi + else + printf '\n\n%sFiles remain:\n%s \n%s\n' "$red" "$(ls -ltr "$1")" "$tput0" + fi + +###################################################################################################### +# There isn't really anything wrong with this code, but the above method removes the CR +# There're other ways to do this https://stackoverflow.com/questions/226703/how-do-i-prompt-for-yes-no-cancel-input-in-a-linux-shell-script + + } + ##--> fn_rmfile() <--############################################################################### + +for path in "$@" + do + if [[ -d "$path" ]] + then + printf '%s\n' "$bold" + if rmdir "$path" 2>/dev/null + then + printf 'Removing %s... \n\n$ ls %s/ \n\n' "$path" "$path" + else + find "$path" + printf '\n' + rm -rI "$path" + printf '%s' "$tput0" + fi + + if [[ ! -e "$path" ]] + then + printf "\nDone!\n" + else + printf "\n\n%sRemoval Failed!%s\n\n%s is still present check ownership and permissions:\n" "$bold" "$tput0" "$path" + ls -lad --color=always "$path" + fi + + elif [[ -f "$path" ]] + then + fn_rmfile "$path" + else + printf "\n\n No such directory or file found!\n\n" + + fi + +done +} +##--> rmr() <--####################################################################################### + editscript(){ local script path; script="${scriptname##*/}"; path="${scriptname%/*}"; swp="$path/.$script.swp" if [[ ! -e "$swp" ]]; then printf "\n\n%s\n\n" "$swp"; (/usr/bin/nano "$scriptname"); exit else printf "\n%s is already being edited.\n%s exists; try fg or look in another window.\n" "$scriptname" "$swp"; exit; fi; } -##editscript()################################################################################ +##--> editscript() <--################################################################################ -filecheck() { +filecheck(){ if [[ ! -z "$1" ]] && [[ "$1" != *\ --\ Audiobook.m4b ]] then printf 'The file specified: %s\nis not in the correct format of\n* -- Audiobook.m4b, checking directory for an m4b file...' "$1" @@ -48,7 +109,7 @@ filecheck() { exit 1 fi fi; } -##filecheck()################################################################################# +##--> filecheck() <--################################################################################# calculatetime(){ duration=$(find -type f -name "$1" -print0 | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1) @@ -75,8 +136,8 @@ calculatetime(){ ##echo "totalsec $totalsec" divsec=$(python3 -c "print($totalsec/4)") ##echo "divsec $divsec" -} -##calculatetime{}############################################################################# +} +##--> calculatetime{} <--############################################################################# m4bsplit(){ # Original code from: http://crunchbang.org/forums/viewtopic.php?id=38748#p414992 @@ -136,7 +197,7 @@ if [[ "$1" ]] printf '%s %s%s%s %s%s\n' "$bold" "$red" "$( find -type f -iname "*.opus" -print0 | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1)" "$white" "${opus[@]}" "$tput0" fi } -##checkdur()################################################################################## +##--> checkdur() <--################################################################################## rmyn(){ while true @@ -149,11 +210,11 @@ rmyn(){ esac done } -##rmyn()###################################################################################### +##--> rmyn() <--###################################################################################### -##start####################################################################################### +##--> m4b2opus <--#################################################################################### [[ "$1" == @(edit|e|nano) ]] && editscript @@ -288,7 +349,7 @@ elif [[ "$rmmatch" = true ]] && [[ "$(checkdur m4b)" != "$(checkdur opus)" ]] checkdur printf '\n\n' # rmyn "Remove the conversion ../tmp and ../m4a directories? " "...Leaving ../tmp and ../m4a" rm -r "$convertdir"/tmp "$convertdir"/m4a - rmyn "Remove the conversion ../tmp and ../m4a directories? " "...Leaving ../tmp and ../m4a" rm -r "$tmp" "$convertdir"/m4a + rmyn "Remove the conversion ../tmp and ../m4a directories? " "...Leaving ../tmp and ../m4a" rm -r "$tmp" "$convertdir"/m4a 2>/dev/null [[ ! "$iorm" ]] && rmyn "Remove $ititle -- Part *.opus files? " "...Leaving $ititle -- Part *.opus files..." rm "$convertdir/$title"\ --\ Part\ ??:*.opus rmr *m4b "$convertdir" #rmr is a dependency printf '\n\n...Done.\n\nPlease see %s/%s\n\n' "$PWD" "$ioutput" @@ -296,8 +357,12 @@ else ls "$tmp" "$convertdir/m4a" 2>/dev/null checkdur printf '\n\n' - rmyn "Remove the conversion ../tmp and ../m4a directories? " "...Leaving ../tmp and ../m4a" rm -r "$tmp" "$convertdir"/m4a + rmyn "Remove the conversion ../tmp and ../m4a directories? " "...Leaving ../tmp and ../m4a" rm -r "$tmp" "$convertdir"/m4a 2>/dev/null [[ ! "$iorm" ]] && rmyn "Remove $ititle -- Part *.opus files? " "...Leaving $ititle -- Part *.opus files..." rm "$convertdir/$title"\ --\ Part\ ??:*.opus rmr *m4b "$convertdir" #rmr is a dependency printf '\n\n...Done.\n\nPlease see %s/%s\n\n' "$PWD" "$ioutput" fi + + + + diff --git a/rmr b/rmr index f91cbfd..7a2c4c4 100755 --- a/rmr +++ b/rmr @@ -31,12 +31,12 @@ fn_rmfile(){ printf '\n\n%sFiles remain:\n%s \n%s\n' "$red" "$(ls -ltr "$1")" "$tput0" fi -######################################################################################################################################################### +###################################################################################################### # There isn't really anything wrong with this code, but the above method removes the CR # There're other ways to do this https://stackoverflow.com/questions/226703/how-do-i-prompt-for-yes-no-cancel-input-in-a-linux-shell-script } - +##--> fn_rmfile() <--################################################################################# [[ "$1" == @(edit|e|nano|-e|-E) ]] && editscript