Skip to content

Instantly share code, notes, and snippets.

@Asynchronousx
Last active January 27, 2020 12:27
Show Gist options
  • Select an option

  • Save Asynchronousx/4861ecec3404655429143b2ff6f7877f to your computer and use it in GitHub Desktop.

Select an option

Save Asynchronousx/4861ecec3404655429143b2ff6f7877f to your computer and use it in GitHub Desktop.
Remove MP3 audio files shorter than X seconds from a folder.
#!/bin/bash
# Simple bash script that will remove MP3 audio file from a folder, if their duration in seconds
# does not match the passed duration input. Note: the script must be executed in the interested folder.
function get_duration {
total_len=0
for file in `pwd`/*; do
cur_len=$(ffprobe -i $file -show_entries format=duration -v quiet -of csv="p=0")
cur_len=${cur_len%.*}
total_len=$((total_len+cur_len))
done
echo $total_len
}
if ! [[ "$1" =~ ^[0-9]+$ ]] && [ "$#" -ne 1 ]; then
echo "Must provide an integer to define max duration lenght of the audio. Exiting.."
exit 1
fi
file_deleted=0
min_len=$1
dur=$(get_duration)
for file in `pwd`/*; do
if [ ${file: -4} == ".mp3" ]; then
len=$(ffprobe -i $file -show_entries format=duration -v quiet -of csv="p=0")
echo -n "$file got lenght of $len "
if [ ${len%.*} -gt ${min_len} ]; then
echo "- PRESERVED"
else
rm $file
echo "- DELETED"
file_deleted++
fi
fi
done
dur=$(get_duration)
echo "STATS ---------------------"
echo "File deleted: $file_deleted"
echo "Total audio lenght BEFORE deleting: $dur"
echo "Total audio lenght AFTER deleting: $dur"
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment