Make a bash script for Mac, that process a output.mp4 file containing consolidated music, and outputs the proper start and end time for each of the songs in the mp4. All songs end in silence, use the sudden volume change as an indication that a new song is starting. Use MM:SS format. The mp4 is 52:19 minutes long. The songs contains spaces and special characters. Use best practices and code robustness The list of songs is as follows:
Cineris Somnia
Ivy
Daydream
Dust and Light
Swaying Daisy
Garden of Light
The White Box
Waiting by the Windowsill
Charlotte's Theme
The White Box-Broken
A Warm Room
Charlotte's Theme-Wither
Wild Rose
Marie's Theme
Deep Forest
Mist
Night Carol
False Blue
Death of the Little Bird
Ophelia's Theme-Darkness
Hall of Silence
Downstairs
Black Insanity
Judgment
Decaying Corridor
Pandora's Box
Ophelia's Theme-Light
Under Cloudy Skies
Tender Black
Neverending Dream
Angel's Lament
World of Ashes
Cineris Somnia - Alone In The Ashes
Cineris Somnia - PV Version
Cineris Somnia - Fragile
Assistant
Certainly! Below is a robust Bash script for macOS that processes an output.mp4 file containing consolidated music. The script detects the start and end times of each song based on silence detection and maps these timings to a predefined list of song titles. The results are output in MM:SS format.
Prerequisites
Homebrew: Ensure you have Homebrew installed on your Mac. If not, install it using:
FFmpeg: This tool is essential for audio processing. Install it via Homebrew:
bash
brew install ffmpeg
jq: A lightweight and flexible command-line JSON processor.
bash
brew install jq
Script Overview
Extract Audio: The script extracts the audio from output.mp4 into a temporary WAV file.
Detect Silence: Using ffmpeg's silencedetect filter, the script identifies silence periods which likely indicate song boundaries.
Parse Silence Data: The script processes the ffmpeg output to determine the start and end times of each song.
Map to Song List: It maps the detected timings to the provided list of song titles.
Output Results: The start and end times for each song are displayed in MM:SS format.
The Bash Script
bash
#!/bin/bash
# Exit immediately if a command exits with a non-zero status
set -e
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check for required commands
for cmd in ffmpeg jq; do
if ! command_exists "$cmd"; then
echo "Error: $cmd is not installed. Please install it using Homebrew:"
echo " brew install $cmd"
exit 1
fi
done
# Define the list of songs
songs=(
"Cineris Somnia"
"Ivy"
"Daydream"
"Dust and Light"
"Swaying Daisy"
"Garden of Light"
"The White Box"
"Waiting by the Windowsill"
"Charlotte's Theme"
"The White Box-Broken"
"A Warm Room"
"Charlotte's Theme-Wither"
"Wild Rose"
"Marie's Theme"
"Deep Forest"
"Mist"
"Night Carol"
"False Blue"
"Death of the Little Bird"
"Ophelia's Theme-Darkness"
"Hall of Silence"
"Downstairs"
"Black Insanity"
"Judgment"
"Decaying Corridor"
"Pandora's Box"
"Ophelia's Theme-Light"
"Under Cloudy Skies"
"Tender Black"
"Neverending Dream"
"Angel's Lament"
"World of Ashes"
"Cineris Somnia - Alone In The Ashes"
"Cineris Somnia - PV Version"
"Cineris Somnia - Fragile"
)
# Input file
INPUT_FILE="output.mp4"
# Check if input file exists
if [[ ! -f "$INPUT_FILE" ]]; then
echo "Error: $INPUT_FILE not found in the current directory."
exit 1
fi
# Temporary files
AUDIO_TEMP="temp_audio.wav"
SILENCE_LOG="silence.log"
# Extract audio to WAV
echo "Extracting audio from $INPUT_FILE..."
ffmpeg -y -i "$INPUT_FILE" -ac 1 -ar 44100 "$AUDIO_TEMP" >/dev/null 2>&1
# Detect silence using ffmpeg silencedetect filter
# Parameters:
# - silence_duration: minimum duration of silence to detect (e.g., 2 seconds)
# - silence_threshold: below which is considered silence (e.g., -40dB)
echo "Detecting silence in audio..."
ffmpeg -i "$AUDIO_TEMP" -af silencedetect=noise=-40dB:d=2 -f null - 2> "$SILENCE_LOG"
# Remove temporary audio file
rm "$AUDIO_TEMP"
# Parse silence log to get silence periods
# We will store silence_end times which indicate the end of a song
# and silence_start times which indicate the start of silence
# Initialize arrays
silence_starts=()
silence_ends=()
while IFS= read -r line; do
if [[ $line =~ silence_start: ([0-9]+\.[0-9]+) ]]; then
silence_starts+=("${BASH_REMATCH[1]}")
elif [[ $line =~ silence_end: ([0-9]+\.[0-9]+) ]]; then
silence_ends+=("${BASH_REMATCH[1]}")
fi
done < "$SILENCE_LOG"
# Remove silence.log
rm "$SILENCE_LOG"
# Determine song start and end times based on silence
# Assume that songs are separated by silence
# The first song starts at 0
# Each silence_end marks the end of a song
# Each silence_start marks the beginning of silence after a song
# Initialize song boundaries
song_starts=()
song_ends=()
# Start of first song
song_starts+=(0)
# Iterate through silence ends to mark song ends
for end in "${silence_ends[@]}"; do
song_ends+=("$end")
# Next song starts at the end of silence
song_starts+=("$end")
done
# Remove the last start as it would be after the last song
unset 'song_starts[-1]'
# Check if number of detected songs matches the song list
detected_song_count=${#song_ends[@]}
expected_song_count=${#songs[@]}
if [[ "$detected_song_count" -ne "$expected_song_count" ]]; then
echo "Warning: Detected $detected_song_count songs, but expected $expected_song_count."
echo "The script will map as many songs as possible."
fi
# Function to convert seconds to MM:SS format
convert_time() {
local total_seconds="$1"
local minutes=$(printf "%02d" $((total_seconds / 60)))
local seconds=$(printf "%02d" $((total_seconds % 60)))
echo "${minutes}:${seconds}"
}
# Output the song timings
echo "Song Timings:"
for ((i=0; i<expected_song_count; i++)); do
song="${songs[$i]}"
start_sec=${song_starts[$i]}
end_sec=${song_ends[$i]}
# If end_sec is empty, assign the total duration (52:19 -> 3139 seconds)
if [[ -z "$end_sec" ]]; then
end_sec=3139
fi
# Convert to MM:SS
start_time=$(convert_time "${start_sec%.*}")
end_time=$(convert_time "${end_sec%.*}")
echo "\"$song\": Start - $start_time, End - $end_time"
done
How to Use the Script
Save the Script: Create a new file named detect_song_timings.sh and paste the script content into it.
Make the Script Executable:
bash
chmod +x detect_song_timings.sh
Place output.mp4: Ensure that your output.mp4 file is in the same directory as the script.
Run the Script:
bash
./detect_song_timings.sh
Script Output
The script will output something like:
code
Extracting audio from output.mp4...
Detecting silence in audio...
Song Timings:
"Cineris Somnia": Start - 00:00, End - 04:35
"Ivy": Start - 04:35, End - 09:10
"Daydream": Start - 09:10, End - 13:45
...
"Cineris Somnia - Fragile": Start - 50:50, End - 52:19
Each line represents a song with its corresponding start and end times in MM:SS format.
Notes and Best Practices
Error Handling: The script includes checks for required commands and the presence of the input file. It also warns if the number of detected songs doesn't match the expected list.
Temporary Files: Audio extraction and silence logs are handled using temporary files, which are cleaned up after processing to avoid clutter.
Flexibility: The silencedetect parameters (noise and d) can be adjusted based on the audio's characteristics to improve detection accuracy.
Time Conversion: The convert_time function ensures that times are consistently formatted in MM:SS.
Assumptions: The script assumes that each silence detected corresponds to a boundary between songs. Ensure that the audio has clear silences between tracks for optimal results.
Feel free to customize and enhance the script based on your specific requirements!