Created
June 12, 2019 14:53
-
-
Save samcrosoft/22cef1894ebdfc31f1baf5d6f7c36cd1 to your computer and use it in GitHub Desktop.
Bash script to list all media files in a folder and display their durations and the total duration
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 | |
target_folder=$1 | |
declare -a durations | |
file_extension=${2:-mp4} | |
function get_duration() { | |
duration=$(ffmpeg -i "${1}" 2>&1 | grep Duration | cut -d ' ' -f 4 | sed 's/,//i'); | |
echo "${2}: ${1} is => [${duration}]"; | |
duration_seconds=$(echo "${duration}" | awk -F ':' '{answer=($1*60*60)+($2*60)+($3)} END{print answer}'); | |
durations[$2]="${duration_seconds}" | |
} | |
if [[ ! -d "${target_folder}" ]]; then | |
echo "die, not folder"; | |
fi | |
cd "${target_folder}"; | |
ffmpeg -i $i1 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//i | |
index=0 | |
for file in $(find -maxdepth 1 -type f -name "*.${file_extension}" -printf '%P\n' | sort); do | |
index=$((index+1)) | |
get_duration $file $index | |
done | |
#total duration | |
total=0 | |
for val in "${durations[@]}"; do | |
total=$(echo "${total}+${val}" | bc -l) | |
done | |
total_format=$(date -d@"${total}" -u +%H\h:%M\m:%S\s) | |
echo "Total Duration = ${total_format}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment