Here's a one-line bash script that processes an .mp4 file and outputs the start and end times for each song based on silence detection:
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"); IFS=$'\n' songs_list="${songs[*]}"; ffmpeg -i input.mp4 -af silencedetect=noise=-30dB:d=0.5 -f null - 2>&1 | awk -v song_list="$songs_list" 'BEGIN{split(song_list,songs,"\n"); si=1;} /silence_end/ {start= $5; se[si]=start;} /silence_start/ {end= $5; ss[si]=end; si++; } END{for (i=1; i<=length(songs); i++) { printf("Song: %s, Start: %s, End: %s\n", songs[i], se[i], ss[i]);}}'
Explanation:
-
Define the song list in a bash array:
songs=( "Cineris Somnia" "Ivy" "Daydream" "Dust and Light" ... )
This array contains all the song names.
-
Set the Internal Field Separator (IFS) to a newline character to ensure proper handling of song names with spaces:
IFS=$'\n' songs_list="${songs[*]}"
This joins the array elements into a single string separated by newlines.
-
Use ffmpeg with silencedetect filter to detect silence in the audio:
ffmpeg -i input.mp4 -af silencedetect=noise=-30dB:d=0.5 -f null - 2>&1
noise=-30dB sets the silence threshold.
d=0.5 sets the minimum duration of silence to detect.
- The output is redirected to standard error and then processed.
-
Process the output with awk:
awk -v song_list="$songs_list" 'BEGIN{...} /silence_end/ { ... } /silence_start/ { ... } END{...}'
- The song list is passed to
awk via the -v option.
- In the
BEGIN block, split the song list into an array.
- When a line matches
silence_end, record the start time of a song.
- When a line matches
silence_start, record the end time of a song.
- In the
END block, loop through the songs and print their start and end times.
Note: Replace input.mp4 with the path to your actual video file.
This script uses the assumption that each song starts right after a period of silence and ends when the next silence starts. Adjust the noise and d parameters if needed to match the characteristics of your audio file.