This one-liner: "songs=("Cineris Somnia" "Ivy" "Daydream" "Dust and Light" "Swaying…
- User
This one-liner: "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"); LC_ALL=C ends=((ffmpeg -i output.mp4 -af silencedetect=n=-50dB:d=1 -f null - 2>&1 | grep "silence_end" | awk '{gsub(",",".",5); if(5 + 0.5)}')); start=0; i=0; for end in "i" -ge "{#songs[@]}" ]; then break; fi; s_m=((start /60)); s_s=((end /60)); e_s=s_m" "e_m" "{songs[i]}"; start=((i + 1)); done" Outputs this: " 00:00-01:01 01:01-03:40 Cineris Somnia 03:40-05:15 Ivy 05:15-08:14 Daydream 08:14-09:31 Dust and Light 09:31-11:10 Swaying Daisy 11:10-12:44 Garden of Light 12:44-12:51 The White Box 12:51-14:41 Waiting by the Windowsill 14:41-16:22 Charlotte's Theme 16:22-00:00 The White Box-Broken 00:00-34:12 A Warm Room 34:12-35:11 Charlotte's Theme-Wither 35:11-36:47 Wild Rose 36:47-38:16 Marie's Theme 38:16-39:38 Deep Forest 39:38-41:13 Mist 41:13-43:17 Night Carol 43:17-44:34 False Blue 44:34-46:07 Death of the Little Bird 46:07-47:16 Ophelia's Theme-Darkness 47:16-48:39 Hall of Silence 48:39-49:58 Downstairs 49:58-50:53 Black Insanity 50:53-52:16 Judgment "
-
The first field is empty, it should be "00:00-01:01 Cineris Somnia"
-
Songs are missing, especially the last ones in the list
-
The timing suddently resets "14:41-16:22 The White Box-Broken 16:22-00:00 A Warm Room"
-
- Assistant
The issues you're encountering with your one-liner script stem from how the silence detection results are being processed and mapped to your list of songs. Let's address each problem individually and provide a comprehensive solution.
1. First Field is Empty
Issue:
The first interval is missing the song name and appears as an empty field (e.g.,00:00-01:01instead of00:00-01:01 Cineris Somnia).Cause:
This typically happens if the first detectedsilence_endis incorrectly processed or if there's an initial silence detected at00:00, causing the first interval to be treated as silence rather than a song segment.Solution:
Ensure that the firstsilence_enddetected is not at00:00. Additionally, verify that the script correctly assigns the first song to the interval from00:00to the firstsilence_end.
2. Songs Are Missing, Especially the Last Ones
Issue:
Not all songs from yoursongsarray are being mapped, particularly the last few entries.Cause:
The number ofsilence_endevents detected might be fewer than the number of songs, causing the loop to terminate before all songs are assigned. Additionally, without appending the total duration of the video, the last song may not have a defined end time.Solution:
Append the total duration of the video to theendsarray. This ensures that the final song segment is captured even if there are no trailing silences.
3. Timing Suddenly Resets
Issue:
The timing resets unexpectedly, e.g.,16:22-00:00 A Warm Room, indicating that the end time is set to00:00.Cause:
This occurs when theendsarray includes a0, possibly from an incorrect silence detection or if the script fails to handle the end of the video properly.Solution:
By appending the total duration to theendsarray and ensuring that allsilence_endvalues are processed correctly, you can prevent the timing from resetting.
Comprehensive Solution
Here's a revised version of your one-liner script that addresses all the aforementioned issues:
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" ) # Get the total duration of the video in seconds, rounded to the nearest integer duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 output.mp4) end_total=$(printf "%.0f" "$duration") # Detect silence ends that are at least 2 seconds long ends=( $(ffmpeg -i output.mp4 -af silencedetect=n=-50dB:d=1 -f null - 2>&1 \ | grep "silence_end" \ | awk '{gsub(",",".",$5); if($5 >=2) print int($5 + 0.5)}') ) # Append the total duration to ensure the last song is captured ends+=( "$end_total" ) start=0 i=0 for end in "${ends[@]}"; do # Break the loop if all songs have been assigned if [ "$i" -ge "${#songs[@]}" ]; then break fi # Skip any end times that are not greater than the current start time if [ "$end" -le "$start" ]; then continue fi # Calculate minutes and seconds for start and end times s_m=$((start / 60)) s_s=$((start % 60)) e_m=$((end / 60)) e_s=$((end % 60)) # Print the formatted interval with the corresponding song printf "%02d:%02d-%02d:%02d %s\n" "$s_m" "$s_s" "$e_m" "$e_s" "${songs[i]}" # Update the start time for the next interval start=$end i=$((i + 1)) done
Explanation of the Revised Script
-
Appending Total Duration:
- The script retrieves the total duration of
output.mp4usingffprobeand appends it to theendsarray. This ensures that the final song segment from the last detected silence to the end of the video is captured.
- The script retrieves the total duration of
-
Filtering Silence Ends:
- The
ffmpegcommand detects silences with a noise level below-50dBlasting at least1second. - Using
awk, it filters out anysilence_endevents that are less than2seconds and rounds them to the nearest integer.
- The
-
Looping Through Silence Ends:
- The loop iterates through each
endtime, assigns the corresponding song from thesongsarray, and prints the interval inMM:SS-MM:SS Song Nameformat. - It ensures that the
endtime is always greater than thestarttime to avoid invalid intervals. - The loop breaks once all songs have been assigned, preventing any undefined behavior if there are more
silence_endevents than songs.
- The loop iterates through each
-
Handling Edge Cases:
- The script skips any
endtimes that are not greater than the currentstarttime, preventing anomalies like16:22-00:00. - By ensuring that the total duration is included, it prevents the last song from missing its end time.
- The script skips any
Sample Output
After running the revised script, your output should correctly map each song to its respective time interval without missing entries or resetting timings. An example portion of the output might look like this:
00:00-01:01 Cineris Somnia 01:01-03:40 Ivy 03:40-05:15 Daydream 05:15-08:14 Dust and Light 08:14-09:31 Swaying Daisy 09:31-11:10 Garden of Light 11:10-12:44 The White Box 12:44-12:51 Waiting by the Windowsill 12:51-14:41 Charlotte's Theme 14:41-16:22 The White Box-Broken ... (continues for all songs)
Final Notes
-
Ensure Accurate Silence Detection:
The effectiveness of this script heavily relies on accurate silence detection. Make sure that the audio inoutput.mp4has clear silences between songs to allowffmpegto detect them properly. -
Adjust Silence Parameters if Needed:
If you find that silences are not being detected as expected, consider tweaking then(noise threshold) andd(duration) parameters in thesilencedetectfilter to better suit your audio files. -
Script Maintenance:
If the number of songs changes frequently, consider dynamically generating thesongsarray or sourcing it from an external file to make the script more maintainable.
By implementing these changes, your one-liner should function as expected, correctly mapping all songs with accurate timing without missing entries or encountering timing resets.
-