Last active
September 26, 2022 18:39
-
-
Save tassoevan/0ec9c715b6b855af60df to your computer and use it in GitHub Desktop.
Bash script to measure length (duration) of media files in a directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
directory=$1 | |
[ -z "$directory" ] || directory=`pwd` | |
total=0 | |
function round_float { | |
echo "($1 + 0.5)/1" | bc | |
} | |
function print_duration { | |
local duration="$1" | |
local days=$((duration/60/60/24)) | |
local hours=$((duration/60/60%24)) | |
local minutes=$((duration/60%60)) | |
local seconds=$((duration%60)) | |
[[ $days > 0 ]] && printf '%d days ' $days | |
[[ $hours > 0 ]] && printf '%d hours ' $hours | |
[[ $minutes > 0 ]] && printf '%d minutes ' $minutes | |
[[ $days > 0 || $hours > 0 || $minutes > 0 ]] && printf 'and ' | |
printf '%d seconds\n' $seconds | |
} | |
function is_media_file { | |
local file="$1" | |
file --mime-type --brief "$file" | grep "audio\|video" > /dev/null | |
} | |
function process_media { | |
local file="$1" | |
local length=`avprobe -loglevel error -show_streams "$file" | grep duration= | cut -f2 -d= | sort --reverse | head -n1` | |
if [[ "$length" != "" ]] && [[ "$length" != "N/A" ]] && [[ "$length" != "0" ]]; then | |
total=`echo "scale=8;$total + $length" | bc` | |
length=`round_float "$length"` | |
echo -e "$file\n\t" $(print_duration "$length") "\t($length seconds)" | |
fi | |
} | |
while read file; do | |
file=`readlink -f "$file"` | |
if is_media_file "$file"; then | |
process_media "$file" | |
fi | |
done < <(find "$1" -type f) | |
echo -e "\e[1mTotal\e[0m\n\t" $(print_duration "$(round_float $total)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment