Created
June 14, 2023 01:18
-
-
Save JohannSuarez/a9c447d1074ebf1258d62c54ebfdcf79 to your computer and use it in GitHub Desktop.
Get total duration of .wav and .mp3 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 | |
# Function to calculate the total duration | |
calculate_total_duration() { | |
local dir="$1" | |
local total_duration=0 | |
# Loop through each audio file | |
while IFS= read -r -d '' file; do | |
# Check if the file is a .wav or .mp3 file | |
if [[ $file == *.wav ]] || [[ $file == *.mp3 ]]; then | |
# Use ffprobe to get the duration | |
duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file" 2>/dev/null) | |
# Add the duration to the total | |
total_duration=$(awk "BEGIN {print $total_duration + $duration}") | |
fi | |
done < <(find "$dir" -type f \( -iname "*.wav" -o -iname "*.mp3" \) -print0) | |
echo "$total_duration" | |
} | |
# Main script | |
# Check if a directory is provided as an argument | |
if [[ $# -ne 1 ]]; then | |
echo "Usage: $0 <directory>" | |
exit 1 | |
fi | |
# Check if the directory exists | |
if [[ ! -d $1 ]]; then | |
echo "Directory not found: $1" | |
exit 1 | |
fi | |
# Call the function to calculate the total duration | |
total_duration=0 | |
while IFS= read -r -d '' sub_dir; do | |
duration=$(calculate_total_duration "$sub_dir") | |
total_duration=$(awk "BEGIN {print $total_duration + $duration}") | |
done < <(find "$1" -type d -print0) | |
# Display the total duration | |
echo "Total Duration: $total_duration seconds" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment