From e87dc7f4713b187b61392435c940095d319ecb0a Mon Sep 17 00:00:00 2001 From: Denis Lehmann Date: Wed, 12 Jan 2022 11:04:04 +0100 Subject: [PATCH 01/10] update ffmpeg call --- bin/ffcut | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/ffcut b/bin/ffcut index 6eb4649..9dc09d9 100755 --- a/bin/ffcut +++ b/bin/ffcut @@ -20,11 +20,11 @@ source "$(dirname $0)/../lib/sf" echo "Cutting file ${sftbf}$(basename "$FILE")${sftrs}" # Set additional ffmpeg arguments -ffmpeg_args=() -[ "$to" != "end" ] && ffmpeg_args+=("-t" "$to") -[ "${FILE##*.}" == "${out##*.}" ] && ffmpeg_args+=("-c" "copy") +args=() +[ "$to" != "end" ] && args+=("-to" "$to") +[ "${FILE##*.}" == "${out##*.}" ] && args+=("-c" "copy") # Execute ffmpeg -ffmpeg -hide_banner -loglevel error -ss "$from" -i "$FILE" -ss "$from" "${ffmpeg_args[@]}" "$out" +ffmpeg -hide_banner -loglevel error -ss "$from" -i "$FILE" "${args[@]}" "$out" [ "$?" == "0" ] && echo "The extracted part was saved to ${sftbf}$out${sftrs}" From f108de65569b5b865d3e6ac6a18bc58cdc52c7cb Mon Sep 17 00:00:00 2001 From: Denis Lehmann Date: Wed, 12 Jan 2022 11:13:49 +0100 Subject: [PATCH 02/10] fix ffmpeg argument order --- bin/ffcut | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/ffcut b/bin/ffcut index 9dc09d9..770a45a 100755 --- a/bin/ffcut +++ b/bin/ffcut @@ -25,6 +25,6 @@ args=() [ "${FILE##*.}" == "${out##*.}" ] && args+=("-c" "copy") # Execute ffmpeg -ffmpeg -hide_banner -loglevel error -ss "$from" -i "$FILE" "${args[@]}" "$out" +ffmpeg -hide_banner -loglevel error -i "$FILE" -ss "$from" "${args[@]}" "$out" [ "$?" == "0" ] && echo "The extracted part was saved to ${sftbf}$out${sftrs}" From 6788c3d38756c8bade48b3b3487d6e5a7f6c2261 Mon Sep 17 00:00:00 2001 From: Denis Lehmann Date: Sat, 15 Jan 2022 21:13:32 +0100 Subject: [PATCH 03/10] update sf --- lib/sf | 65 +++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/lib/sf b/lib/sf index b3beb93..e42cb65 100644 --- a/lib/sf +++ b/lib/sf @@ -32,17 +32,21 @@ function sfwarn { # Public input functions function sfask { - if [ "$2" == "" ]; then - read -p "$1? [${sftbf}Y${sftrs}/${sftbf}n${sftrs}] " sfin - [[ "$sfin" =~ y|Y|^$ ]] && sfin=true || sfin=false - else - read -p "$1? [${sftbf}y${sftrs}/${sftbf}N${sftrs}] " sfin + if [ -n "$2" ]; then + read -r -p "$1? [${sftbf}y${sftrs}/${sftbf}N${sftrs}] " sfin [[ "$sfin" =~ n|N|^$ ]] && sfin=false || sfin=true + else + read -r -p "$1? [${sftbf}Y${sftrs}/${sftbf}n${sftrs}] " sfin + [[ "$sfin" =~ y|Y|^$ ]] && sfin=true || sfin=false fi } function sfget { - [ "$2" != "" ] && read -p "$1 [${sftbf}$2${sftrs}]: " sfin || read -p "$1: " sfin + if [ -n "$2" ]; then + read -r -p "$1 [${sftbf}$2${sftrs}]: " sfin + else + read -r -p "$1: " sfin + fi [ "$sfin" == "" ] && [ "$2" != "" ] && sfin="$2" } @@ -57,7 +61,7 @@ OLDIFS=$IFS IFS=";" _sfphead="" _sfpdesc="" -_sfodesc="" +_sfodesc=" -h, --help;Show this help message\n" _sfexamples="" _sfpargs=() declare -A _sfflags @@ -76,6 +80,7 @@ for a in "${sfargs[@]}"; do read -r -a _sfparsearr <<< "${a}" # Add to positional argument arry + [[ " ${_sfpargs[*]} " =~ " ${_sfparsearr[0]} " ]] && _sferr "'${_sfparsearr[0]}' is already set: $a" _sfpargs+=("${_sfparsearr[0]}") # Set usage header and description @@ -89,11 +94,13 @@ for a in "${sfargs[@]}"; do read -r -a _sfparsearr <<< "${a}" # Set mappings - _sfflags["-${_sfparsearr[1]}"]="${_sfparsearr[0]}" + [ -n "${_sfflags["--${_sfparsearr[0]}"]}" ] && _sferr "'${_sfparsearr[0]}' is already set: $a" _sfflags["--${_sfparsearr[0]}"]="${_sfparsearr[0]}" + [ -n "${_sfflags["-${_sfparsearr[1]}"]}" ] && _sferr "'${_sfparsearr[1]}' is already set: $a" + _sfflags["-${_sfparsearr[1]}"]="${_sfparsearr[0]}" # Set default value - declare ${_sfparsearr[0]}=false + declare "${_sfparsearr[0]}"=false # Set description _sfodesc="$_sfodesc -${_sfparsearr[1]}, --${_sfparsearr[0]};${_sfparsearr[2]}\n" @@ -105,11 +112,13 @@ for a in "${sfargs[@]}"; do read -r -a _sfparsearr <<< "${a}" # Set mappings - _sfargs["-${_sfparsearr[1]}"]="${_sfparsearr[0]}" + [ -n "${_sfargs["--${_sfparsearr[0]}"]}" ] && _sferr "'${_sfparsearr[0]}' is already set: $a" _sfargs["--${_sfparsearr[0]}"]="${_sfparsearr[0]}" + [ -n "${_sfargs["-${_sfparsearr[1]}"]}" ] && _sferr "'${_sfparsearr[1]}' is already set: $a" + _sfargs["-${_sfparsearr[1]}"]="${_sfparsearr[0]}" # Set default value - declare ${_sfparsearr[0]}="${_sfparsearr[3]}" + declare "${_sfparsearr[0]}"="${_sfparsearr[3]}" # Set description _sfodesc="$_sfodesc -${_sfparsearr[1]}, --${_sfparsearr[0]} ${_sfparsearr[2]};${_sfparsearr[4]} (default: ${_sfparsearr[3]})\n" @@ -119,6 +128,9 @@ for a in "${sfargs[@]}"; do fi done +# Check if at least one positional argument is set if 'sfparr' is used +[ "$sfparr" == true ] && [ "${#_sfpargs[@]}" == 0 ] && _sferr "At least one positional argument must be used with 'sfparr'" + # Parse examples for e in "${sfexamples[@]}"; do @@ -141,10 +153,11 @@ IFS=$OLDIFS # Usage function function _sfusage { - echo -n "Usage: $(basename $0)" - [ "$_sfodesc" != "" ] && echo -n " [OPTIONS]" - echo -e "$_sfphead" - [ ! -z ${sfdesc+x} ] && echo -e "\n$sfdesc" + echo -n "Usage: $(basename "$0") [OPTIONS]" + echo -ne "$_sfphead" + [ "$sfparr" == true ] && echo -n " ..." + echo + [ -n "${sfdesc}" ] && echo -e "\n$sfdesc" if [ "$_sfpdesc" != "" ]; then echo -e "\nPOSITIONAL ARGUMENTS" echo -e "$_sfpdesc" | column -c 80 -s ";" -t -W 2 @@ -157,7 +170,7 @@ function _sfusage { echo -e "\nEXAMPLES" echo -e "$_sfexamples" | column -c 80 -s ";" -t -W 2 fi - if [ ! -z ${sfextra+x} ]; then + if [ -n "${sfextra}" ]; then echo -e "\n$sfextra" fi exit 0 @@ -173,15 +186,15 @@ done while (( "$#" )); do # Check if flag - if [ ! -z ${_sfflags["$1"]} ]; then - declare ${_sfflags["$1"]}=true + if [ -n "${_sfflags["$1"]}" ]; then + declare "${_sfflags["$1"]}"=true # Check if argument - elif [ ! -z ${_sfargs["$1"]} ]; then + elif [ -n "${_sfargs["$1"]}" ]; then # Check if argument has value if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then - declare ${_sfargs["$1"]}="$2" + declare "${_sfargs["$1"]}"="$2" shift else sferr "Argument for '$1' missing" @@ -195,8 +208,11 @@ while (( "$#" )); do else # Set positional argument if [ "${#_sfpargs[@]}" != 0 ]; then - declare ${_sfpargs[0]}="$1" + declare "${_sfpargs[0]}"="$1" + [ "$sfparr" == true ] && _sfplast="${_sfpargs[0]}" && _sfparr=("$1") _sfpargs=("${_sfpargs[@]:1}") + elif [ "$sfparr" == true ]; then + _sfparr+=("$1") else sferr "Too many positional arguments" fi @@ -205,8 +221,11 @@ while (( "$#" )); do shift done +# Parse additional arguments if 'sfparr' is set +[ "$sfparr" == true ] && [ "${#_sfparr[@]}" -ge 1 ] && read -r -a "${_sfplast?}" <<< "${_sfparr[@]}" + # Check if positional arguments left -if [ ${#_sfpargs[@]} != 0 ]; then +if [ "$sfparr" != true ] && [ "${#_sfpargs[@]}" -ge 1 ]; then for p in "${_sfpargs[@]}"; do sferr "Positional argument '$p' missing" 0 done @@ -214,4 +233,4 @@ if [ ${#_sfpargs[@]} != 0 ]; then fi # Unset all internal variables and functions -unset a e _sfphead _sfpdesc _sfodesc _sfexamples _sfpargs _sfflags _sfargs _sferr _sfusage +unset a e _sfargs _sferr _sfexamples _sfflags _sfodesc _sfpargs _sfparr _sfpdesc _sfphead _sfplast _sfusage From 938c53fd85f08f4a770514eef22f367f21efe54b Mon Sep 17 00:00:00 2001 From: Denis Lehmann Date: Sun, 16 Jan 2022 15:45:10 +0100 Subject: [PATCH 04/10] update sf --- lib/sf | 119 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 89 insertions(+), 30 deletions(-) diff --git a/lib/sf b/lib/sf index e42cb65..efca931 100644 --- a/lib/sf +++ b/lib/sf @@ -59,11 +59,14 @@ function _sferr { # Declare variables for parsing OLDIFS=$IFS IFS=";" -_sfphead="" -_sfpdesc="" -_sfodesc=" -h, --help;Show this help message\n" -_sfexamples="" _sfpargs=() +_sfpheads=() +_sfpoffset=0 +_sfptails=() +_sfpusage="" +_sfoheads=() +_sfooffset=0 +_sfotails=() declare -A _sfflags declare -A _sfargs @@ -74,7 +77,7 @@ for a in "${sfargs[@]}"; do _sfsubst=${a//";"} _sfcount="$(((${#a} - ${#_sfsubst})))" - if [ $_sfcount -eq 1 ]; then + if [ "$_sfcount" -eq 1 ]; then # Read positional argument declaration read -r -a _sfparsearr <<< "${a}" @@ -83,12 +86,15 @@ for a in "${sfargs[@]}"; do [[ " ${_sfpargs[*]} " =~ " ${_sfparsearr[0]} " ]] && _sferr "'${_sfparsearr[0]}' is already set: $a" _sfpargs+=("${_sfparsearr[0]}") - # Set usage header and description - _sfphead="$_sfphead ${_sfparsearr[0]}" - _sfpdesc="$_sfpdesc ${_sfparsearr[0]};${_sfparsearr[1]}\n" + # Set usage header and append description arrays + _sfpusage="$_sfpusage ${_sfparsearr[0]}" + _sfphead="${_sfparsearr[0]}" + [ "${#_sfphead}" -gt "${_sfpoffset}" ] && _sfpoffset="${#_sfphead}" + _sfpheads+=("$_sfphead") + _sfptails+=("${_sfparsearr[1]}") # Flags - elif [ $_sfcount -eq 2 ]; then + elif [ "$_sfcount" -eq 2 ]; then # Read flag declaration read -r -a _sfparsearr <<< "${a}" @@ -100,13 +106,16 @@ for a in "${sfargs[@]}"; do _sfflags["-${_sfparsearr[1]}"]="${_sfparsearr[0]}" # Set default value - declare "${_sfparsearr[0]}"=false + declare "${_sfparsearr[0]//-/_}"=false - # Set description - _sfodesc="$_sfodesc -${_sfparsearr[1]}, --${_sfparsearr[0]};${_sfparsearr[2]}\n" + # Append description arrays + _sfohead="-${_sfparsearr[1]}, --${_sfparsearr[0]}" + [ "${#_sfohead}" -gt "${_sfooffset}" ] && _sfooffset="${#_sfohead}" + _sfoheads+=("$_sfohead") + _sfotails+=("${_sfparsearr[2]}") # Arguments - elif [ $_sfcount -eq 4 ]; then + elif [ "$_sfcount" -eq 4 ]; then # Read argument declaration read -r -a _sfparsearr <<< "${a}" @@ -118,57 +127,107 @@ for a in "${sfargs[@]}"; do _sfargs["-${_sfparsearr[1]}"]="${_sfparsearr[0]}" # Set default value - declare "${_sfparsearr[0]}"="${_sfparsearr[3]}" + declare "${_sfparsearr[0]//-/_}"="${_sfparsearr[3]}" - # Set description - _sfodesc="$_sfodesc -${_sfparsearr[1]}, --${_sfparsearr[0]} ${_sfparsearr[2]};${_sfparsearr[4]} (default: ${_sfparsearr[3]})\n" + # Append description arrays + _sfohead="-${_sfparsearr[1]}, --${_sfparsearr[0]} ${_sfparsearr[2]}" + [ "${#_sfohead}" -gt "${_sfooffset}" ] && _sfooffset="${#_sfohead}" + _sfoheads+=("$_sfohead") + _sfotails+=("${_sfparsearr[4]} (default: ${_sfparsearr[3]})") else _sferr "Wrong argument declaration: $a" fi done +# Set _sfwidth to current terminal size +_sfwidth=$(stty size | cut -d ' ' -f 2) + +# Create positional argument description with correct line breaks +_sfpoffset=$(( "_sfpoffset" + 3 )) +_sfpdesc="" +for i in "${!_sfptails[@]}"; do + _sfptail="${_sfptails[$i]}" + if [ $(( "${#_sfptail}" + "$_sfpoffset" )) -gt "$_sfwidth" ]; then + _sfptail=$(echo "$_sfptail" | fold -s -w "$(( _sfwidth - _sfpoffset ))") + _sfptail="${_sfptail//$' \n'/$'\n;'}" + fi + _sfpdesc="${_sfpdesc} ${_sfpheads[$i]};${_sfptail}\n" +done + +# Create option description with correct line breaks +_sfooffset=$(( "_sfooffset" + 3 )) +_sfodesc=" -h, --help;Show this help message\n" +for i in "${!_sfotails[@]}"; do + _sfotail="${_sfotails[$i]}" + if [ $(( "${#_sfotail}" + "$_sfooffset" )) -gt "$_sfwidth" ]; then + _sfotail=$(echo "$_sfotail" | fold -s -w "$(( _sfwidth - _sfooffset ))") + _sfotail="${_sfotail//$' \n'/$'\n;'}" + fi + _sfodesc="${_sfodesc} ${_sfoheads[$i]};${_sfotail}\n" +done + # Check if at least one positional argument is set if 'sfparr' is used [ "$sfparr" == true ] && [ "${#_sfpargs[@]}" == 0 ] && _sferr "At least one positional argument must be used with 'sfparr'" # Parse examples +_sfeheads=() +_sfetails=() +_sfeoffset=0 for e in "${sfexamples[@]}"; do # Get amount of ; _sfsubst=${e//";"} _sfcount="$(((${#e} - ${#_sfsubst})))" - if [ $_sfcount -eq 1 ]; then + if [ "$_sfcount" -eq 1 ]; then # Read example read -r -a _sfparsearr <<< "${e}" - _sfexamples="$_sfexamples ${_sfparsearr[0]};${_sfparsearr[1]}\n" + # Append example arrays + _sfehead="${_sfparsearr[0]}" + [ "${#_sfehead}" -gt "${_sfeoffset}" ] && _sfeoffset="${#_sfehead}" + _sfeheads+=("$_sfehead") + _sfetails+=("${_sfparsearr[1]}") + else _sferr "Wrong example declaration: $e" fi done +# Create examples description with correct line breaks +_sfeoffset=$(( "_sfeoffset" + 3 )) +_sfexamples="" +for i in "${!_sfetails[@]}"; do + _sfetail="${_sfetails[$i]}" + if [ $(( "${#_sfetail}" + "$_sfeoffset" )) -gt "$_sfwidth" ]; then + _sfetail=$(echo "$_sfetail" | fold -s -w "$(( _sfwidth - _sfeoffset ))") + _sfetail="${_sfetail//$' \n'/$'\n;'}" + fi + _sfexamples="${_sfexamples} ${_sfeheads[$i]};${_sfetail}\n" +done + IFS=$OLDIFS # Usage function function _sfusage { echo -n "Usage: $(basename "$0") [OPTIONS]" - echo -ne "$_sfphead" + echo -ne "$_sfpusage" [ "$sfparr" == true ] && echo -n " ..." echo [ -n "${sfdesc}" ] && echo -e "\n$sfdesc" if [ "$_sfpdesc" != "" ]; then echo -e "\nPOSITIONAL ARGUMENTS" - echo -e "$_sfpdesc" | column -c 80 -s ";" -t -W 2 + echo -e "$_sfpdesc" | column -s ";" -t -W 2 fi if [ "$_sfodesc" != "" ]; then echo -e "\nOPTIONS" - echo -e "$_sfodesc" | column -c 80 -s ";" -t -W 2 + echo -e "$_sfodesc" | column -s ";" -t -W 2 fi if [ "$_sfexamples" != "" ]; then echo -e "\nEXAMPLES" - echo -e "$_sfexamples" | column -c 80 -s ";" -t -W 2 + echo -e "$_sfexamples" | column -s ";" -t -W 2 fi if [ -n "${sfextra}" ]; then echo -e "\n$sfextra" @@ -187,14 +246,14 @@ while (( "$#" )); do # Check if flag if [ -n "${_sfflags["$1"]}" ]; then - declare "${_sfflags["$1"]}"=true + declare "${_sfflags["$1"]//-/_}"=true # Check if argument elif [ -n "${_sfargs["$1"]}" ]; then # Check if argument has value if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then - declare "${_sfargs["$1"]}"="$2" + declare "${_sfargs["$1"]//-/_}"="$2" shift else sferr "Argument for '$1' missing" @@ -204,12 +263,12 @@ while (( "$#" )); do else # Check if arg starts with - if [ "${1:0:1}" == "-" ]; then - sferr "Unsupported argument: $1" + sferr "Unsupported argument/flag: $1" else # Set positional argument if [ "${#_sfpargs[@]}" != 0 ]; then - declare "${_sfpargs[0]}"="$1" - [ "$sfparr" == true ] && _sfplast="${_sfpargs[0]}" && _sfparr=("$1") + declare "${_sfpargs[0]//-/_}"="$1" + [ "$sfparr" == true ] && _sfplast="${_sfpargs[0]//-/_}" && _sfparr=("$1") _sfpargs=("${_sfpargs[@]:1}") elif [ "$sfparr" == true ]; then _sfparr+=("$1") @@ -222,10 +281,10 @@ while (( "$#" )); do done # Parse additional arguments if 'sfparr' is set -[ "$sfparr" == true ] && [ "${#_sfparr[@]}" -ge 1 ] && read -r -a "${_sfplast?}" <<< "${_sfparr[@]}" +[ "$sfparr" == true ] && [ "${#_sfparr[@]}" -gt 0 ] && read -r -a "${_sfplast?}" <<< "${_sfparr[@]}" # Check if positional arguments left -if [ "$sfparr" != true ] && [ "${#_sfpargs[@]}" -ge 1 ]; then +if [ "${#_sfpargs[@]}" -gt 0 ]; then for p in "${_sfpargs[@]}"; do sferr "Positional argument '$p' missing" 0 done @@ -233,4 +292,4 @@ if [ "$sfparr" != true ] && [ "${#_sfpargs[@]}" -ge 1 ]; then fi # Unset all internal variables and functions -unset a e _sfargs _sferr _sfexamples _sfflags _sfodesc _sfpargs _sfparr _sfpdesc _sfphead _sfplast _sfusage +unset a e i _sfargs _sfehead _sfeheads _sfeoffset _sferr _sfetails _sfexamples _sfflags _sfodesc _sfohead _sfoheads _sfooffset _sfotails _sfpargs _sfparr _sfpdesc _sfphead _sfpheads _sfplast _sfpoffset _sfptails _sfpusage _sfusage _sfwidth From 1250e3f2481f97d84197ab6471cae3dd68aacc8e Mon Sep 17 00:00:00 2001 From: Denis Lehmann Date: Sun, 16 Jan 2022 15:51:58 +0100 Subject: [PATCH 05/10] update README --- README.org | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/README.org b/README.org index 29538e7..9a18537 100644 --- a/README.org +++ b/README.org @@ -12,7 +12,7 @@ ** Installation - Grab a script and execute it. + Grab a script and execute it. This repo is also a [[https://nixos.wiki/wiki/Flakes][Nix Flake]]. You can directly start a script with the following command if you have at least version 2.4 of [[https://nixos.org/][Nix]] installed: @@ -20,7 +20,7 @@ : $ nix run github:Deleh/ffutils# -- --help ** Scripts - + *** =ffconv= :properties: :custom_id: ffconv @@ -52,19 +52,20 @@ TO_FORMAT To format OPTIONS - -d, --directory DIRECTORY Convert files in DIRECTORY (default: current work d - irectory) + -h, --help Show this help message + -d, --directory DIRECTORY Convert files in DIRECTORY (default: current work + directory) -k, --keep Keep original files -l, --list List files which match the FROM_FORMAT - -m, --move DIRECTORY Move old files to DIRECTORY (omits --keep) (default - : ) + -m, --move DIRECTORY Move old files to DIRECTORY (omits --keep) + (default: ) EXAMPLES ffconv mp3 opus Convert all mp3 files to opus - ffconv -m trash mp4 mkv Convert all mp4 to mkv and move the original one - s to './trash' - ffconv -d ~/music -l wma mp3 List all wma files from '~/music' and ask for co - nverting them to mp3 + ffconv -m trash mp4 mkv Convert all mp4 to mkv and move the original + ones to './trash' + ffconv -d ~/music -l wma mp3 List all wma files from '~/music' and ask for + converting them to mp3 #+end_example *** =ffcut= @@ -79,7 +80,7 @@ Cutting file video.mp4 The extracted part was saved to cutted_video.mp4 #+end_example - + **** Usage #+begin_example @@ -91,14 +92,15 @@ FILE File which will be cutted OPTIONS + -h, --help Show this help message -f, --from TIMESTAMP/SECONDS Extract from TIMESTAMP/SECONDS (default: 0) - -o, --out FILE Save extracted part to FILE (default: cutted_) + -o, --out FILE Save extracted part to FILE (default: + cutted_) -t, --to TIMESTAMP/DURATION Extract to TIMESTAMP/DURATION (default: end) EXAMPLES - ffcut -t 5 video.mp4 -o cut.webm Extract the first five seconds of 'vi - deo.mp4' to 'cut.webm' - ffcut -f 00:10:30 -t 00:14:15 video.mp4 Extract the part from 00:10:30 to 00: - 14:15 from 'video.mp4' + ffcut -t 5 video.mp4 -o cut.webm Extract the first five seconds of + 'video.mp4' to 'cut.webm' + ffcut -f 00:10:30 -t 00:14:15 video.mp4 Extract the part from 00:10:30 to + 00:14:15 from 'video.mp4' #+end_example From c0c69f19dd6224c6b35d43fa291b0ab4825f9026 Mon Sep 17 00:00:00 2001 From: Denis Lehmann Date: Mon, 17 Jan 2022 18:07:55 +0100 Subject: [PATCH 06/10] update sf --- lib/sf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sf b/lib/sf index efca931..284c190 100644 --- a/lib/sf +++ b/lib/sf @@ -133,7 +133,7 @@ for a in "${sfargs[@]}"; do _sfohead="-${_sfparsearr[1]}, --${_sfparsearr[0]} ${_sfparsearr[2]}" [ "${#_sfohead}" -gt "${_sfooffset}" ] && _sfooffset="${#_sfohead}" _sfoheads+=("$_sfohead") - _sfotails+=("${_sfparsearr[4]} (default: ${_sfparsearr[3]})") + [ "${_sfparsearr[3]}" != "" ] && _sfotails+=("${_sfparsearr[4]} (default: ${_sfparsearr[3]})") || _sfotails+=("${_sfparsearr[4]}") else _sferr "Wrong argument declaration: $a" From 9e399abdf92886b076f8a4af51067c0814b10b4c Mon Sep 17 00:00:00 2001 From: Denis Lehmann Date: Wed, 19 Jan 2022 15:53:55 +0100 Subject: [PATCH 07/10] update sf --- lib/sf | 81 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/lib/sf b/lib/sf index 284c190..05ed987 100644 --- a/lib/sf +++ b/lib/sf @@ -71,6 +71,7 @@ declare -A _sfflags declare -A _sfargs # Parse sf arguments +sfargs=("help;h;Show this help message" "${sfargs[@]}") for a in "${sfargs[@]}"; do # Get amount of ; @@ -140,36 +141,6 @@ for a in "${sfargs[@]}"; do fi done -# Set _sfwidth to current terminal size -_sfwidth=$(stty size | cut -d ' ' -f 2) - -# Create positional argument description with correct line breaks -_sfpoffset=$(( "_sfpoffset" + 3 )) -_sfpdesc="" -for i in "${!_sfptails[@]}"; do - _sfptail="${_sfptails[$i]}" - if [ $(( "${#_sfptail}" + "$_sfpoffset" )) -gt "$_sfwidth" ]; then - _sfptail=$(echo "$_sfptail" | fold -s -w "$(( _sfwidth - _sfpoffset ))") - _sfptail="${_sfptail//$' \n'/$'\n;'}" - fi - _sfpdesc="${_sfpdesc} ${_sfpheads[$i]};${_sfptail}\n" -done - -# Create option description with correct line breaks -_sfooffset=$(( "_sfooffset" + 3 )) -_sfodesc=" -h, --help;Show this help message\n" -for i in "${!_sfotails[@]}"; do - _sfotail="${_sfotails[$i]}" - if [ $(( "${#_sfotail}" + "$_sfooffset" )) -gt "$_sfwidth" ]; then - _sfotail=$(echo "$_sfotail" | fold -s -w "$(( _sfwidth - _sfooffset ))") - _sfotail="${_sfotail//$' \n'/$'\n;'}" - fi - _sfodesc="${_sfodesc} ${_sfoheads[$i]};${_sfotail}\n" -done - -# Check if at least one positional argument is set if 'sfparr' is used -[ "$sfparr" == true ] && [ "${#_sfpargs[@]}" == 0 ] && _sferr "At least one positional argument must be used with 'sfparr'" - # Parse examples _sfeheads=() _sfetails=() @@ -196,27 +167,65 @@ for e in "${sfexamples[@]}"; do fi done -# Create examples description with correct line breaks +IFS=$OLDIFS + +# Check if at least one positional argument is set if 'sfparr' is used +[ "$sfparr" == true ] && [ "${#_sfpargs[@]}" == 0 ] && _sferr "At least one positional argument must be used with 'sfparr'" + +# Correct offsets +_sfpoffset=$(( "_sfpoffset" + 3 )) +_sfooffset=$(( "_sfooffset" + 3 )) _sfeoffset=$(( "_sfeoffset" + 3 )) + +# Set _sfwidth to current terminal width +_sfwidth=$(stty size | cut -d ' ' -f 2) + +# Create positional argument description with correct line breaks +_sfpdesc="" +for i in "${!_sfptails[@]}"; do + _sfptail="${_sfptails[$i]}" + if [ $(( "${#_sfptail}" + "$_sfpoffset" )) -gt "$_sfwidth" ]; then + _sftmpwidth="$(( _sfwidth - _sfpoffset ))" + _sftmpwidth=$(echo -e "${_sftmpwidth}\n1" | sort -nr | head -n 1) + _sfptail=$(echo "$_sfptail" | fold -s -w "$_sftmpwidth") + _sfptail="${_sfptail//$' \n'/$'\n;'}" + fi + _sfpdesc="${_sfpdesc} ${_sfpheads[$i]};${_sfptail}\n" +done + +# Create option description with correct line breaks +_sfodesc="" +for i in "${!_sfotails[@]}"; do + _sfotail="${_sfotails[$i]}" + if [ $(( "${#_sfotail}" + "$_sfooffset" )) -gt "$_sfwidth" ]; then + _sftmpwidth="$(( _sfwidth - _sfooffset ))" + _sftmpwidth=$(echo -e "${_sftmpwidth}\n1" | sort -nr | head -n 1) + _sfotail=$(echo "$_sfotail" | fold -s -w "$_sftmpwidth") + _sfotail="${_sfotail//$' \n'/$'\n;'}" + fi + _sfodesc="${_sfodesc} ${_sfoheads[$i]};${_sfotail}\n" +done + +# Create examples description with correct line breaks _sfexamples="" for i in "${!_sfetails[@]}"; do _sfetail="${_sfetails[$i]}" if [ $(( "${#_sfetail}" + "$_sfeoffset" )) -gt "$_sfwidth" ]; then - _sfetail=$(echo "$_sfetail" | fold -s -w "$(( _sfwidth - _sfeoffset ))") + _sftmpwidth="$(( _sfwidth - _sfeoffset ))" + _sftmpwidth=$(echo -e "${_sftmpwidth}\n1" | sort -nr | head -n 1) + _sfetail=$(echo "$_sfetail" | fold -s -w "$_sftmpwidth") _sfetail="${_sfetail//$' \n'/$'\n;'}" fi _sfexamples="${_sfexamples} ${_sfeheads[$i]};${_sfetail}\n" done -IFS=$OLDIFS - # Usage function function _sfusage { echo -n "Usage: $(basename "$0") [OPTIONS]" echo -ne "$_sfpusage" [ "$sfparr" == true ] && echo -n " ..." echo - [ -n "${sfdesc}" ] && echo -e "\n$sfdesc" + [ -n "${sfdesc}" ] && echo -e "\n$sfdesc" | fold -s -w "$_sfwidth" if [ "$_sfpdesc" != "" ]; then echo -e "\nPOSITIONAL ARGUMENTS" echo -e "$_sfpdesc" | column -s ";" -t -W 2 @@ -292,4 +301,4 @@ if [ "${#_sfpargs[@]}" -gt 0 ]; then fi # Unset all internal variables and functions -unset a e i _sfargs _sfehead _sfeheads _sfeoffset _sferr _sfetails _sfexamples _sfflags _sfodesc _sfohead _sfoheads _sfooffset _sfotails _sfpargs _sfparr _sfpdesc _sfphead _sfpheads _sfplast _sfpoffset _sfptails _sfpusage _sfusage _sfwidth +unset a e i _sfargs _sfehead _sfeheads _sfeoffset _sferr _sfetails _sfexamples _sfflags _sfodesc _sfohead _sfoheads _sfooffset _sfotails _sfpargs _sfparr _sfpdesc _sfphead _sfpheads _sfplast _sfpoffset _sfptails _sfpusage _sftmpwidth _sfusage _sfwidth From 0b679c4a01bdbfc9b00ed8266c438f93774a1cf0 Mon Sep 17 00:00:00 2001 From: Denis Lehmann Date: Thu, 20 Jan 2022 18:10:22 +0100 Subject: [PATCH 08/10] update sf --- README.org | 5 ++--- lib/sf | 19 ++++++++++++++----- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/README.org b/README.org index 9a18537..36bf661 100644 --- a/README.org +++ b/README.org @@ -52,13 +52,12 @@ TO_FORMAT To format OPTIONS - -h, --help Show this help message -d, --directory DIRECTORY Convert files in DIRECTORY (default: current work directory) -k, --keep Keep original files -l, --list List files which match the FROM_FORMAT -m, --move DIRECTORY Move old files to DIRECTORY (omits --keep) - (default: ) + -h, --help Show this help message and exit EXAMPLES ffconv mp3 opus Convert all mp3 files to opus @@ -92,11 +91,11 @@ FILE File which will be cutted OPTIONS - -h, --help Show this help message -f, --from TIMESTAMP/SECONDS Extract from TIMESTAMP/SECONDS (default: 0) -o, --out FILE Save extracted part to FILE (default: cutted_) -t, --to TIMESTAMP/DURATION Extract to TIMESTAMP/DURATION (default: end) + -h, --help Show this help message and exit EXAMPLES ffcut -t 5 video.mp4 -o cut.webm Extract the first five seconds of diff --git a/lib/sf b/lib/sf index 05ed987..364c883 100644 --- a/lib/sf +++ b/lib/sf @@ -71,7 +71,7 @@ declare -A _sfflags declare -A _sfargs # Parse sf arguments -sfargs=("help;h;Show this help message" "${sfargs[@]}") +sfargs=("${sfargs[@]}" "help;h;Show this help message and exit") for a in "${sfargs[@]}"; do # Get amount of ; @@ -184,7 +184,7 @@ _sfwidth=$(stty size | cut -d ' ' -f 2) _sfpdesc="" for i in "${!_sfptails[@]}"; do _sfptail="${_sfptails[$i]}" - if [ $(( "${#_sfptail}" + "$_sfpoffset" )) -gt "$_sfwidth" ]; then + if [ "$(( ${#_sfptail} + _sfpoffset ))" -gt "$_sfwidth" ]; then _sftmpwidth="$(( _sfwidth - _sfpoffset ))" _sftmpwidth=$(echo -e "${_sftmpwidth}\n1" | sort -nr | head -n 1) _sfptail=$(echo "$_sfptail" | fold -s -w "$_sftmpwidth") @@ -197,7 +197,7 @@ done _sfodesc="" for i in "${!_sfotails[@]}"; do _sfotail="${_sfotails[$i]}" - if [ $(( "${#_sfotail}" + "$_sfooffset" )) -gt "$_sfwidth" ]; then + if [ "$(( ${#_sfotail} + _sfooffset ))" -gt "$_sfwidth" ]; then _sftmpwidth="$(( _sfwidth - _sfooffset ))" _sftmpwidth=$(echo -e "${_sftmpwidth}\n1" | sort -nr | head -n 1) _sfotail=$(echo "$_sfotail" | fold -s -w "$_sftmpwidth") @@ -210,7 +210,7 @@ done _sfexamples="" for i in "${!_sfetails[@]}"; do _sfetail="${_sfetails[$i]}" - if [ $(( "${#_sfetail}" + "$_sfeoffset" )) -gt "$_sfwidth" ]; then + if [ "$(( ${#_sfetail} + _sfeoffset ))" -gt "$_sfwidth" ]; then _sftmpwidth="$(( _sfwidth - _sfeoffset ))" _sftmpwidth=$(echo -e "${_sftmpwidth}\n1" | sort -nr | head -n 1) _sfetail=$(echo "$_sfetail" | fold -s -w "$_sftmpwidth") @@ -250,6 +250,15 @@ for a in "$@"; do [ "$a" == "-h" ] || [ "$a" == "--help" ] && _sfusage done +# Check if dependencies are available +for d in "${sfdeps[@]}"; do + if ! command -v "$d" &> /dev/null; then + sferr "Command ${sftbf}${d}${sftrs} not found" 0 + _sfdeperr=true + fi +done +[ "$_sfdeperr" == true ] && exit 1 + # Parse arguments while (( "$#" )); do @@ -301,4 +310,4 @@ if [ "${#_sfpargs[@]}" -gt 0 ]; then fi # Unset all internal variables and functions -unset a e i _sfargs _sfehead _sfeheads _sfeoffset _sferr _sfetails _sfexamples _sfflags _sfodesc _sfohead _sfoheads _sfooffset _sfotails _sfpargs _sfparr _sfpdesc _sfphead _sfpheads _sfplast _sfpoffset _sfptails _sfpusage _sftmpwidth _sfusage _sfwidth +unset a d e i OLDIFS _sfargs _sfehead _sfeheads _sfeoffset _sferr _sfetails _sfexamples _sfflags _sfodesc _sfohead _sfoheads _sfooffset _sfotails _sfpargs _sfparr _sfpdesc _sfphead _sfpheads _sfplast _sfpoffset _sfptails _sfpusage _sftmpwidth _sfusage _sfwidth From 312f67789bc970c28845ce74702d352a013e1c47 Mon Sep 17 00:00:00 2001 From: Denis Lehmann Date: Thu, 20 Jan 2022 18:12:13 +0100 Subject: [PATCH 09/10] update README --- README.org | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.org b/README.org index 36bf661..0fa15ce 100644 --- a/README.org +++ b/README.org @@ -6,6 +6,8 @@ | [[#ffconv][ffconv]] | Convert multiple media files from one format to another | | [[#ffcut][ffcut]] | Extract a part of a media file | + All are created with the [[https://github.com/Deleh/sf][sf]] script framework. + ** Dependencies - [[https://ffmpeg.org/][FFmpeg]] From cf550ef9cab77e6a8d0b66deb2a462b7f0f597a0 Mon Sep 17 00:00:00 2001 From: Denis Lehmann Date: Fri, 21 Oct 2022 21:14:23 +0200 Subject: [PATCH 10/10] add sfdeps --- bin/ffconv | 1 + bin/ffcut | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/bin/ffconv b/bin/ffconv index fe1429d..618738e 100755 --- a/bin/ffconv +++ b/bin/ffconv @@ -11,6 +11,7 @@ sfargs+=("move;m;DIRECTORY;;Move old files to DIRECTORY (omits --keep)") sfexamples+=("ffconv mp3 opus;Convert all mp3 files to opus") sfexamples+=("ffconv -m trash mp4 mkv;Convert all mp4 to mkv and move the original ones to './trash'") sfexamples+=("ffconv -d ~/music -l wma mp3;List all wma files from '~/music' and ask for converting them to mp3") +sfdeps=("ffmpeg") source "$(dirname $0)/../lib/sf" diff --git a/bin/ffcut b/bin/ffcut index 770a45a..ba5db91 100755 --- a/bin/ffcut +++ b/bin/ffcut @@ -8,23 +8,23 @@ sfargs+=("out;o;FILE;cutted_;Save extracted part to FILE") sfargs+=("to;t;TIMESTAMP/DURATION;end;Extract to TIMESTAMP/DURATION") sfexamples+=("ffcut -t 5 video.mp4 -o cut.webm;Extract the first five seconds of 'video.mp4' to 'cut.webm'") sfexamples+=("ffcut -f 00:10:30 -t 00:14:15 video.mp4;Extract the part from 00:10:30 to 00:14:15 from 'video.mp4'") +sfdeps=("ffmpeg") source "$(dirname $0)/../lib/sf" # Handle missing parameters -[ "$from" == 0 ] && [ "$to" == "end" ] && sferr "Set at least '--from' or '--to'" +[ "$from" == 0 ] && [ "$to" == "end" ] && sferr "Set at least ${sftbf}--from${sftrs} or ${sftbf}--to${sftrs}" # Set default value for output file [ "$out" == "cutted_" ] && out="$(dirname "$FILE")/cutted_$(basename "$FILE")" -echo "Cutting file ${sftbf}$(basename "$FILE")${sftrs}" - # Set additional ffmpeg arguments args=() [ "$to" != "end" ] && args+=("-to" "$to") [ "${FILE##*.}" == "${out##*.}" ] && args+=("-c" "copy") # Execute ffmpeg +echo "Cutting file ${sftbf}$(basename "$FILE")${sftrs}" ffmpeg -hide_banner -loglevel error -i "$FILE" -ss "$from" "${args[@]}" "$out" [ "$?" == "0" ] && echo "The extracted part was saved to ${sftbf}$out${sftrs}"