commit b15abdedcb7744a8343d914309b318f6fd2c5864 Author: Denis Lehmann Date: Sun Jan 9 21:57:12 2022 +0100 initial commit diff --git a/README.org b/README.org new file mode 100644 index 0000000..df879ed --- /dev/null +++ b/README.org @@ -0,0 +1,102 @@ +* ffutils + + This is a collection of ffmpeg wrapper scripts: + + | [[#ffconv][=ffconv=]] | Convert multiple media files from one format to another | + | [[#ffcut][=ffcut=]] | Extract a part of a media file | + +** Dependencies + + - [[https://ffmpeg.org/][FFmpeg]] + +** Installation + + 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: + + : nix run github:Deleh/ffutils# -- --help + +** Scripts + +*** =ffconv= + :properties: + :custom_id: ffconv + :end: + + =ffconf= converts multiple media files from one format to another. + This is done recursively so the files can be in any directory structure. + + #+begin_example + $ ffconv mp3 opus + Converting 4 files from mp3 to opus + Processing file 4/4: Alphaville - Big in Japan + done + #+end_example + + *Warning*: The default behavior removes the original files. + Use the =--keep= or =--move= flags to keep the original files or move them to another directory (see *Usage* below). + +**** Usage + + #+begin_example + Usage: ffconv [OPTIONS] FROM_FORMAT TO_FORMAT + + Convert multiple media files from one format to another. + Subdirectories are visited recursively. + + POSITIONAL ARGUMENTS + FROM_FORMAT From format + TO_FORMAT To format + + OPTIONS + -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) (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 + #+end_example + +*** =ffcut= + :properties: + :custom_id: ffcut + :end: + + =ffcut= extracts a part of a media file. + + #+begin_example + $ ffcut --from 00:15:00 --to 00:16:30 video.mp4 + Cutting file video.mp4 + The extracted part was saved to cutted_video.mp4 + #+end_example + +**** Usage + + #+begin_example + Usage: ffcut [OPTIONS] FILE + + Extract a part of a file. + The cutted file will be saved as cutted_. + + POSITIONAL ARGUMENTS + FILE File which will be cutted + + OPTIONS + -f, --from TIMESTAMP/SECONDS Extract from TIMESTAMP/SECONDS (default: 0) + -t, --to TIMESTAMP/DURATION Extract to TIMESTAMP/DURATION (default: end) + + EXAMPLES + ffcut -t 5 video.mp4 Extract the first five seconds of 'vi + deo.mp4' + 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 new file mode 100755 index 0000000..313e2b0 --- /dev/null +++ b/bin/ffconv @@ -0,0 +1,64 @@ +#!/usr/bin/env bash + +# sf variables +sfdesc="Convert multiple media files from one format to another.\nSubdirectories are visited recursively." +sfargs+=("FROM_FORMAT;From format") +sfargs+=("TO_FORMAT;To format") +sfargs+=("directory;d;DIRECTORY;current work directory;Convert files in DIRECTORY") +sfargs+=("keep;k;Keep original files") +sfargs+=("list;l;List files which match the FROM_FORMAT") +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") + +# sf -- script framework (https://github.com/Deleh/sf) +sftrs=$'\e[0m';sftbf=$'\e[1m';sftdim=$'\e[2m';sftul=$'\e[4m';sftblink=$'\e[5m';sftinv=$'\e[7m';sfthide=$'\e[8m';sftclr=$'\e[1A\e[K';sftk=$'\e[30m';sftr=$'\e[31m';sftg=$'\e[32m';sfty=$'\e[33m';sftb=$'\e[34m';sftm=$'\e[35m';sftc=$'\e[36m';sftw=$'\e[97m';function sferr { echo -e "${sftbf}${sftr}ERROR${sftrs} $1";if [ -z "$2" ];then exit 1;fi;};function sfwarn { echo -e "${sftbf}${sfty}WARNING${sftrs} $1";};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;[[ "$sfin" =~ n|N|^$ ]]&&sfin=false||sfin=true;fi;};function sfget { [ "$2" != "" ]&&read -p "$1 [${sftbf}$2${sftrs}]: " sfin||read -p "$1: " sfin;[ "$sfin" == "" ]&&[ "$2" != "" ]&&sfin="$2";};function _sferr { echo -e "${sftbf}${sftr}SF PARSE ERROR${sftrs} $1";exit 1;};OLDIFS=$IFS;IFS=";";_sfphead="";_sfpdesc="";_sfodesc="";_sfexamples="";_sfpargs=();declare -A _sfflags;declare -A _sfargs;for a in "${sfargs[@]}";do _sfsubst=${a//";"};_sfcount="$(((${#a} - ${#_sfsubst})))";if [ $_sfcount -eq 1 ];then read -r -a _sfparsearr<<<"${a}";_sfpargs+=("${_sfparsearr[0]}");_sfphead="$_sfphead ${_sfparsearr[0]}";_sfpdesc="$_sfpdesc ${_sfparsearr[0]};${_sfparsearr[1]}\n";elif [ $_sfcount -eq 2 ];then read -r -a _sfparsearr<<<"${a}";_sfflags["-${_sfparsearr[1]}"]="${_sfparsearr[0]}";_sfflags["--${_sfparsearr[0]}"]="${_sfparsearr[0]}";declare ${_sfparsearr[0]}=false;_sfodesc="$_sfodesc -${_sfparsearr[1]}, --${_sfparsearr[0]};${_sfparsearr[2]}\n";elif [ $_sfcount -eq 4 ];then read -r -a _sfparsearr<<<"${a}";_sfargs["-${_sfparsearr[1]}"]="${_sfparsearr[0]}";_sfargs["--${_sfparsearr[0]}"]="${_sfparsearr[0]}";declare ${_sfparsearr[0]}="${_sfparsearr[3]}";_sfodesc="$_sfodesc -${_sfparsearr[1]}, --${_sfparsearr[0]} ${_sfparsearr[2]};${_sfparsearr[4]} (default: ${_sfparsearr[3]})\n";else _sferr "Wrong argument declaration: $a";fi;done;for e in "${sfexamples[@]}";do _sfsubst=${e//";"};_sfcount="$(((${#e} - ${#_sfsubst})))";if [ $_sfcount -eq 1 ];then read -r -a _sfparsearr<<<"${e}";_sfexamples="$_sfexamples ${_sfparsearr[0]};${_sfparsearr[1]}\n";else _sferr "Wrong example declaration: $e";fi;done;IFS=$OLDIFS;function _sfusage { 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 -c 80 -s ";" -t -W 2;fi;if [ "$_sfodesc" != "" ];then echo -e "\nOPTIONS";echo -e "$_sfodesc"|column -c 80 -s ";" -t -W 2;fi;if [ "$_sfexamples" != "" ];then echo -e "\nEXAMPLES";echo -e "$_sfexamples"|column -c 80 -s ";" -t -W 2;fi;if [ ! -z ${sfextra+x} ];then echo -e "\n$sfextra";fi;exit 0;};for a in "$@";do [ "$a" == "-h" ]||[ "$a" == "--help" ]&&_sfusage;done;while(("$#"));do if [ ! -z ${_sfflags["$1"]} ];then declare ${_sfflags["$1"]}=true;elif [ ! -z ${_sfargs["$1"]} ];then if [ -n "$2" ]&&[ "${2:0:1}" != "-" ];then declare ${_sfargs["$1"]}="$2";shift;else sferr "Argument for '$1' missing";fi;else if [ "${1:0:1}" == "-" ];then sferr "Unsupported argument: $1";else if [ "${#_sfpargs[@]}" != 0 ];then declare ${_sfpargs[0]}="$1";_sfpargs=("${_sfpargs[@]:1}");else sferr "Too many positional arguments";fi;fi;fi;shift;done;if [ ${#_sfpargs[@]} != 0 ];then for p in "${_sfpargs[@]}";do sferr "Positional argument '$p' missing" 0;done;exit 1;fi;unset a e _sfphead _sfpdesc _sfodesc _sfexamples _sfpargs _sfflags _sfargs _sferr _sfusage + +# Handle default directory +[ "$directory" == "current work directory" ] && directory="." + +# Get files +mapfile -d $'\0' files < <(find "$directory" -name "*.$FROM_FORMAT" -print0) + +# Check number of files +if [ "${#files[@]}" == 0 ]; then + echo "No files of format ${sftbf}$FROM_FORMAT${sftrs} found" + exit +fi + +# Create move directory if set +if [ "$move" != "" ]; then + mkdir -p "$move" +fi + +# List files +if [ "$list" == true ]; then + for file in "${files[@]}"; do + echo "$file" + done + echo + sfask "Do you want to convert the files to ${sftbf}$TO_FORMAT${sftrs}" "no" + [ "$sfin" == false ] && exit +fi + +echo -e "Converting ${sftbf}${#files[@]}${sftrs} files from ${sftbf}$FROM_FORMAT${sftrs} to ${sftbf}$TO_FORMAT${sftrs}\n" + +# Convert files +for i in "${!files[@]}"; do + + file="${files[$i]}" + filename=$(basename "${file%.*}") + echo "${sftclr}Processing file ${sftbf}$((i+1))${sftrs}/${sftbf}${#files[@]}${sftrs}: ${sftbf}$filename${sftrs}" + ffmpeg -hide_banner -loglevel error -nostdin -i "$file" "${file%.*}.$TO_FORMAT" + + if [ "$?" != 0 ]; then + echo + elif [ "$move" != "" ]; then + mv --backup=t "$file" "$move" + elif [ "$keep" == false ]; then + rm "$file" + fi +done + +echo "done" diff --git a/bin/ffcut b/bin/ffcut new file mode 100755 index 0000000..14431f0 --- /dev/null +++ b/bin/ffcut @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +# sf variables +sfdesc="Extract a part of a file.\nThe cutted file will be saved as cutted_." +sfargs+=("FILE;File which will be cutted") +sfargs+=("from;f;TIMESTAMP/SECONDS;0;Extract from TIMESTAMP/SECONDS") +sfargs+=("to;t;TIMESTAMP/DURATION;end;Extract to TIMESTAMP/DURATION") +sfexamples+=("ffcut -t 5 video.mp4;Extract the first five seconds of 'video.mp4'") +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'") + +# sf -- script framework (https://github.com/Deleh/sf) +sftrs=$'\e[0m';sftbf=$'\e[1m';sftdim=$'\e[2m';sftul=$'\e[4m';sftblink=$'\e[5m';sftinv=$'\e[7m';sfthide=$'\e[8m';sftclr=$'\e[1A\e[K';sftk=$'\e[30m';sftr=$'\e[31m';sftg=$'\e[32m';sfty=$'\e[33m';sftb=$'\e[34m';sftm=$'\e[35m';sftc=$'\e[36m';sftw=$'\e[97m';function sferr { echo -e "${sftbf}${sftr}ERROR${sftrs} $1";if [ -z "$2" ];then exit 1;fi;};function sfwarn { echo -e "${sftbf}${sfty}WARNING${sftrs} $1";};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;[[ "$sfin" =~ n|N|^$ ]]&&sfin=false||sfin=true;fi;};function sfget { [ "$2" != "" ]&&read -p "$1 [${sftbf}$2${sftrs}]: " sfin||read -p "$1: " sfin;[ "$sfin" == "" ]&&[ "$2" != "" ]&&sfin="$2";};function _sferr { echo -e "${sftbf}${sftr}SF PARSE ERROR${sftrs} $1";exit 1;};OLDIFS=$IFS;IFS=";";_sfphead="";_sfpdesc="";_sfodesc="";_sfexamples="";_sfpargs=();declare -A _sfflags;declare -A _sfargs;for a in "${sfargs[@]}";do _sfsubst=${a//";"};_sfcount="$(((${#a} - ${#_sfsubst})))";if [ $_sfcount -eq 1 ];then read -r -a _sfparsearr<<<"${a}";_sfpargs+=("${_sfparsearr[0]}");_sfphead="$_sfphead ${_sfparsearr[0]}";_sfpdesc="$_sfpdesc ${_sfparsearr[0]};${_sfparsearr[1]}\n";elif [ $_sfcount -eq 2 ];then read -r -a _sfparsearr<<<"${a}";_sfflags["-${_sfparsearr[1]}"]="${_sfparsearr[0]}";_sfflags["--${_sfparsearr[0]}"]="${_sfparsearr[0]}";declare ${_sfparsearr[0]}=false;_sfodesc="$_sfodesc -${_sfparsearr[1]}, --${_sfparsearr[0]};${_sfparsearr[2]}\n";elif [ $_sfcount -eq 4 ];then read -r -a _sfparsearr<<<"${a}";_sfargs["-${_sfparsearr[1]}"]="${_sfparsearr[0]}";_sfargs["--${_sfparsearr[0]}"]="${_sfparsearr[0]}";declare ${_sfparsearr[0]}="${_sfparsearr[3]}";_sfodesc="$_sfodesc -${_sfparsearr[1]}, --${_sfparsearr[0]} ${_sfparsearr[2]};${_sfparsearr[4]} (default: ${_sfparsearr[3]})\n";else _sferr "Wrong argument declaration: $a";fi;done;for e in "${sfexamples[@]}";do _sfsubst=${e//";"};_sfcount="$(((${#e} - ${#_sfsubst})))";if [ $_sfcount -eq 1 ];then read -r -a _sfparsearr<<<"${e}";_sfexamples="$_sfexamples ${_sfparsearr[0]};${_sfparsearr[1]}\n";else _sferr "Wrong example declaration: $e";fi;done;IFS=$OLDIFS;function _sfusage { 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 -c 80 -s ";" -t -W 2;fi;if [ "$_sfodesc" != "" ];then echo -e "\nOPTIONS";echo -e "$_sfodesc"|column -c 80 -s ";" -t -W 2;fi;if [ "$_sfexamples" != "" ];then echo -e "\nEXAMPLES";echo -e "$_sfexamples"|column -c 80 -s ";" -t -W 2;fi;if [ ! -z ${sfextra+x} ];then echo -e "\n$sfextra";fi;exit 0;};for a in "$@";do [ "$a" == "-h" ]||[ "$a" == "--help" ]&&_sfusage;done;while(("$#"));do if [ ! -z ${_sfflags["$1"]} ];then declare ${_sfflags["$1"]}=true;elif [ ! -z ${_sfargs["$1"]} ];then if [ -n "$2" ]&&[ "${2:0:1}" != "-" ];then declare ${_sfargs["$1"]}="$2";shift;else sferr "Argument for '$1' missing";fi;else if [ "${1:0:1}" == "-" ];then sferr "Unsupported argument: $1";else if [ "${#_sfpargs[@]}" != 0 ];then declare ${_sfpargs[0]}="$1";_sfpargs=("${_sfpargs[@]:1}");else sferr "Too many positional arguments";fi;fi;fi;shift;done;if [ ${#_sfpargs[@]} != 0 ];then for p in "${_sfpargs[@]}";do sferr "Positional argument '$p' missing" 0;done;exit 1;fi;unset a e _sfphead _sfpdesc _sfodesc _sfexamples _sfpargs _sfflags _sfargs _sferr _sfusage + +# Script +[ "$from" == 0 ] && [ "$to" == "end" ] && sferr "Set at least '--from' or '--to'" +echo "Cutting file ${sftbf}$(basename "$FILE")${sftrs}" +if [ "$to" == "end" ]; then + ffmpeg -hide_banner -loglevel error -ss "$from" -i "$FILE" -ss "$from" -c copy "$(dirname "$FILE")/cutted_$(basename "$FILE")" +else + ffmpeg -hide_banner -loglevel error -ss "$from" -i "$FILE" -ss "$from" -t "$to" -c copy "$(dirname "$FILE")/cutted_$(basename "$FILE")" +fi +[ "$?" == "0" ] && echo "The extracted part was saved to ${sftbf}cutted_$(basename "$FILE")${sftrs}" diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..c8a793f --- /dev/null +++ b/flake.lock @@ -0,0 +1,40 @@ +{ + "nodes": { + "flake-utils": { + "locked": { + "lastModified": 1638122382, + "narHash": "sha256-sQzZzAbvKEqN9s0bzWuYmRaA03v40gaJ4+iL1LXjaeI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "74f7e4319258e287b0f9cb95426c9853b282730b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1641593416, + "narHash": "sha256-Vn/vqQtYnVuZlbGGO0gSzLjmtFwb6OPvakwyoG1D/MY=", + "path": "/nix/store/5vivfhinhgzs7pp8p5anhik6y4nr5vsx-source", + "rev": "36480448d470bf41bb21267cf9062a1542c4a95f", + "type": "path" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..5fac73c --- /dev/null +++ b/flake.nix @@ -0,0 +1,44 @@ +{ + description = "A collection of FFmpeg wrapper scripts"; + nixConfig.bash-prompt = "\[\\e[1mffutils-dev\\e[0m:\\w\]$ "; + inputs.flake-utils.url = "github:numtide/flake-utils"; + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem + (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + in + { + packages = { + ffconv = pkgs.stdenv.mkDerivation { + name = "ffconv"; + src = self; + patchPhase = with pkgs; '' + substituteInPlace bin/ffconv \ + --replace ffmpeg ${ffmpeg}/bin/ffmpeg + ''; + installPhase = '' + install -m 755 -D bin/ffconv $out/bin/ffconv + ''; + }; + ffcut = pkgs.stdenv.mkDerivation { + name = "ffcut"; + src = self; + patchPhase = with pkgs; '' + substituteInPlace bin/ffcut \ + --replace ffmpeg ${ffmpeg}/bin/ffmpeg + ''; + installPhase = '' + install -m 755 -D bin/ffcut $out/bin/ffcut + ''; + }; + }; + + devShell = pkgs.mkShell { + buildInputs = with pkgs; [ + ffmpeg + ]; + }; + } + ); +}