I’m upscaling some audio and the website asked me to state how many minutes I’d need:
In a code block, the one-liner looks like this:
find | grep mp3$ | xargs -L 1 mp3-minutes | math-sum
This one-liner does the following:
The find command lists all the files and directories in the current location.
The output of find is then piped to grep mp3$ which filters and retains only the lines ending with mp3.
The filtered list of mp3 files is then piped to xargs -L 1 mp3-minutes which executes the mp3-minutes command on each mp3 file to find the duration in minutes of each file.
The resulting list of durations is then piped to math-sum which sums up all the durations to give a total.
Lets break down each stage with partial outputs.
The find command
It lists all files and directories recursively in a directory. The output includes both file and directory names.
Partial Output:
.
./3_Vayikra
./3_Vayikra/7_Kedoshim
./3_Vayikra/7_Kedoshim/c19_v02.mp3
./3_Vayikra/4_Tazria
...
The grep mp3$ command
It scans its input lines for matches to the given pattern which is mp3$, and it returns only the lines ending with mp3, i.e. mp3 files.
Partial Output:
./3_Vayikra/7_Kedoshim/c19_v02.mp3
./3_Vayikra/4_Tazria/c12_v04.mp3
./3_Vayikra/4_Tazria/c12_v05.mp3
...
The xargs -L 1 mp3-minutes command
xargs is used to build and execute command lines from standard input. Here, it takes each mp3 file (one by one due to -L 1 option) and passes it as an argument to mp3-minutes command, a custom command gpt mostly wrote for me.
#!/usr/env/bin bash
mp3_file="$1"
if [ -f "$mp3_file" ]; then
duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$mp3_file")
minutes=$(bc <<< "scale=2; $duration / 60")
echo $minutes
else
exit 1
fi
Partial Output:
5
4
3
...
This hypothetical output assumes that each mp3 files duration represents in minutes.
The math-sum command
Finally, the math-sum command sums the durations listed in its input. The final output would be the total duration (i.e., the sum) of all mp3 files.
Final Output:
123
This hypothetical output assumes that the total durations of all mp3 files are 123 minutes.
you may want to include the definition for mp3-minutes in case someone actually wants to find this useful