diff --git a/README.org b/README.org index 0fa15ce..29538e7 100644 --- a/README.org +++ b/README.org @@ -6,15 +6,13 @@ | [[#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]] ** 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: @@ -22,7 +20,7 @@ : $ nix run github:Deleh/ffutils# -- --help ** Scripts - + *** =ffconv= :properties: :custom_id: ffconv @@ -54,19 +52,19 @@ TO_FORMAT To format OPTIONS - -d, --directory DIRECTORY Convert files in DIRECTORY (default: current work - directory) + -d, --directory DIRECTORY Convert files in DIRECTORY (default: current work d + irectory) -k, --keep Keep original files -l, --list List files which match the FROM_FORMAT - -m, --move DIRECTORY Move old files to DIRECTORY (omits --keep) - -h, --help Show this help message and exit + -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 - ones to './trash' - ffconv -d ~/music -l wma mp3 List all wma files from '~/music' and ask for - converting them to mp3 + 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 #+end_example *** =ffcut= @@ -81,7 +79,7 @@ Cutting file video.mp4 The extracted part was saved to cutted_video.mp4 #+end_example - + **** Usage #+begin_example @@ -94,14 +92,13 @@ OPTIONS -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) - -h, --help Show this help message and exit EXAMPLES - 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' + 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' #+end_example diff --git a/bin/ffconv b/bin/ffconv index 618738e..fe1429d 100755 --- a/bin/ffconv +++ b/bin/ffconv @@ -11,7 +11,6 @@ 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 ba5db91..6eb4649 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 ${sftbf}--from${sftrs} or ${sftbf}--to${sftrs}" +[ "$from" == 0 ] && [ "$to" == "end" ] && sferr "Set at least '--from' or '--to'" # 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") +ffmpeg_args=() +[ "$to" != "end" ] && ffmpeg_args+=("-t" "$to") +[ "${FILE##*.}" == "${out##*.}" ] && ffmpeg_args+=("-c" "copy") # Execute ffmpeg -echo "Cutting file ${sftbf}$(basename "$FILE")${sftrs}" -ffmpeg -hide_banner -loglevel error -i "$FILE" -ss "$from" "${args[@]}" "$out" +ffmpeg -hide_banner -loglevel error -ss "$from" -i "$FILE" -ss "$from" "${ffmpeg_args[@]}" "$out" [ "$?" == "0" ] && echo "The extracted part was saved to ${sftbf}$out${sftrs}" diff --git a/lib/sf b/lib/sf index 364c883..b3beb93 100644 --- a/lib/sf +++ b/lib/sf @@ -32,21 +32,17 @@ function sfwarn { # Public input functions function sfask { - 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 + 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 + [[ "$sfin" =~ n|N|^$ ]] && sfin=false || sfin=true fi } function sfget { - if [ -n "$2" ]; then - read -r -p "$1 [${sftbf}$2${sftrs}]: " sfin - else - read -r -p "$1: " sfin - fi + [ "$2" != "" ] && read -p "$1 [${sftbf}$2${sftrs}]: " sfin || read -p "$1: " sfin [ "$sfin" == "" ] && [ "$2" != "" ] && sfin="$2" } @@ -59,82 +55,64 @@ function _sferr { # Declare variables for parsing OLDIFS=$IFS IFS=";" +_sfphead="" +_sfpdesc="" +_sfodesc="" +_sfexamples="" _sfpargs=() -_sfpheads=() -_sfpoffset=0 -_sfptails=() -_sfpusage="" -_sfoheads=() -_sfooffset=0 -_sfotails=() declare -A _sfflags declare -A _sfargs # Parse sf arguments -sfargs=("${sfargs[@]}" "help;h;Show this help message and exit") for a in "${sfargs[@]}"; do # Get amount of ; _sfsubst=${a//";"} _sfcount="$(((${#a} - ${#_sfsubst})))" - if [ "$_sfcount" -eq 1 ]; then + if [ $_sfcount -eq 1 ]; then # Read positional argument declaration 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 append description arrays - _sfpusage="$_sfpusage ${_sfparsearr[0]}" - _sfphead="${_sfparsearr[0]}" - [ "${#_sfphead}" -gt "${_sfpoffset}" ] && _sfpoffset="${#_sfphead}" - _sfpheads+=("$_sfphead") - _sfptails+=("${_sfparsearr[1]}") + # Set usage header and description + _sfphead="$_sfphead ${_sfparsearr[0]}" + _sfpdesc="$_sfpdesc ${_sfparsearr[0]};${_sfparsearr[1]}\n" # Flags - elif [ "$_sfcount" -eq 2 ]; then + elif [ $_sfcount -eq 2 ]; then # Read flag declaration read -r -a _sfparsearr <<< "${a}" # Set mappings - [ -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]}" + _sfflags["--${_sfparsearr[0]}"]="${_sfparsearr[0]}" # Set default value - declare "${_sfparsearr[0]//-/_}"=false + declare ${_sfparsearr[0]}=false - # Append description arrays - _sfohead="-${_sfparsearr[1]}, --${_sfparsearr[0]}" - [ "${#_sfohead}" -gt "${_sfooffset}" ] && _sfooffset="${#_sfohead}" - _sfoheads+=("$_sfohead") - _sfotails+=("${_sfparsearr[2]}") + # Set description + _sfodesc="$_sfodesc -${_sfparsearr[1]}, --${_sfparsearr[0]};${_sfparsearr[2]}\n" # Arguments - elif [ "$_sfcount" -eq 4 ]; then + elif [ $_sfcount -eq 4 ]; then # Read argument declaration read -r -a _sfparsearr <<< "${a}" # Set mappings - [ -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]}" + _sfargs["--${_sfparsearr[0]}"]="${_sfparsearr[0]}" # Set default value - declare "${_sfparsearr[0]//-/_}"="${_sfparsearr[3]}" + declare ${_sfparsearr[0]}="${_sfparsearr[3]}" - # Append description arrays - _sfohead="-${_sfparsearr[1]}, --${_sfparsearr[0]} ${_sfparsearr[2]}" - [ "${#_sfohead}" -gt "${_sfooffset}" ] && _sfooffset="${#_sfohead}" - _sfoheads+=("$_sfohead") - [ "${_sfparsearr[3]}" != "" ] && _sfotails+=("${_sfparsearr[4]} (default: ${_sfparsearr[3]})") || _sfotails+=("${_sfparsearr[4]}") + # Set description + _sfodesc="$_sfodesc -${_sfparsearr[1]}, --${_sfparsearr[0]} ${_sfparsearr[2]};${_sfparsearr[4]} (default: ${_sfparsearr[3]})\n" else _sferr "Wrong argument declaration: $a" @@ -142,26 +120,18 @@ for a in "${sfargs[@]}"; do done # 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}" - # Append example arrays - _sfehead="${_sfparsearr[0]}" - [ "${#_sfehead}" -gt "${_sfeoffset}" ] && _sfeoffset="${#_sfehead}" - _sfeheads+=("$_sfehead") - _sfetails+=("${_sfparsearr[1]}") - + _sfexamples="$_sfexamples ${_sfparsearr[0]};${_sfparsearr[1]}\n" else _sferr "Wrong example declaration: $e" fi @@ -169,76 +139,25 @@ done 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 - _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 - # Usage function function _sfusage { - echo -n "Usage: $(basename "$0") [OPTIONS]" - echo -ne "$_sfpusage" - [ "$sfparr" == true ] && echo -n " ..." - echo - [ -n "${sfdesc}" ] && echo -e "\n$sfdesc" | fold -s -w "$_sfwidth" + echo -n "Usage: $(basename $0)" + [ "$_sfodesc" != "" ] && echo -n " [OPTIONS]" + echo -e "$_sfphead" + [ ! -z ${sfdesc+x} ] && echo -e "\n$sfdesc" if [ "$_sfpdesc" != "" ]; then echo -e "\nPOSITIONAL ARGUMENTS" - echo -e "$_sfpdesc" | column -s ";" -t -W 2 + echo -e "$_sfpdesc" | column -c 80 -s ";" -t -W 2 fi if [ "$_sfodesc" != "" ]; then echo -e "\nOPTIONS" - echo -e "$_sfodesc" | column -s ";" -t -W 2 + echo -e "$_sfodesc" | column -c 80 -s ";" -t -W 2 fi if [ "$_sfexamples" != "" ]; then echo -e "\nEXAMPLES" - echo -e "$_sfexamples" | column -s ";" -t -W 2 + echo -e "$_sfexamples" | column -c 80 -s ";" -t -W 2 fi - if [ -n "${sfextra}" ]; then + if [ ! -z ${sfextra+x} ]; then echo -e "\n$sfextra" fi exit 0 @@ -250,28 +169,19 @@ 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 # Check if flag - if [ -n "${_sfflags["$1"]}" ]; then - declare "${_sfflags["$1"]//-/_}"=true + if [ ! -z ${_sfflags["$1"]} ]; then + declare ${_sfflags["$1"]}=true # Check if argument - elif [ -n "${_sfargs["$1"]}" ]; then + elif [ ! -z ${_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" @@ -281,15 +191,12 @@ while (( "$#" )); do else # Check if arg starts with - if [ "${1:0:1}" == "-" ]; then - sferr "Unsupported argument/flag: $1" + sferr "Unsupported argument: $1" else # Set positional argument if [ "${#_sfpargs[@]}" != 0 ]; then - declare "${_sfpargs[0]//-/_}"="$1" - [ "$sfparr" == true ] && _sfplast="${_sfpargs[0]//-/_}" && _sfparr=("$1") + declare ${_sfpargs[0]}="$1" _sfpargs=("${_sfpargs[@]:1}") - elif [ "$sfparr" == true ]; then - _sfparr+=("$1") else sferr "Too many positional arguments" fi @@ -298,11 +205,8 @@ while (( "$#" )); do shift done -# Parse additional arguments if 'sfparr' is set -[ "$sfparr" == true ] && [ "${#_sfparr[@]}" -gt 0 ] && read -r -a "${_sfplast?}" <<< "${_sfparr[@]}" - # Check if positional arguments left -if [ "${#_sfpargs[@]}" -gt 0 ]; then +if [ ${#_sfpargs[@]} != 0 ]; then for p in "${_sfpargs[@]}"; do sferr "Positional argument '$p' missing" 0 done @@ -310,4 +214,4 @@ if [ "${#_sfpargs[@]}" -gt 0 ]; then fi # Unset all internal variables and functions -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 +unset a e _sfphead _sfpdesc _sfodesc _sfexamples _sfpargs _sfflags _sfargs _sferr _sfusage