Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: How to split one huge music file by detecting silence in ffmpeg (Read 6275 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

How to split one huge music file by detecting silence in ffmpeg

How to split one huge music file by detecting silence in ffmpeg by creating a batch script, i know there are a python scripts but im noob and do not know how to use them, is there an option to do it all using just ffmpeg?

Re: How to split one huge music file by detecting silence in ffmpeg

Reply #1
I have this little bash thing stored which could be turned into real bash script with some work
Code: [Select]
awk_round () {
awk 'BEGIN{printf "%."'$1'"f\n", "'$2'"}'
}
round () {
ssvar="$(awk_round "5" "$ssvar")"
tovar="$(awk_round "5" "$tovar")"
}

c="1"
quiet=(-hide_banner -loglevel info -nostats -nostdin)
cat detect.log | while read _ ssvar; read _ tovar; do
    #pad name
    printf -v name "%03d" "$c"
    # action

    echo; echo "-ss $ssvar -to $tovar"
    echo "--------------------------------------"
    if [ -z "$tovar" ]; then
        round && ffmpeg "${quiet[@]}" -i "${input}" -ss "$ssvar" -c:a copy "${name}".m4a
    else
        round && ffmpeg "${quiet[@]}" -i "${input}" -ss "$ssvar" -to "$tovar" -c:a copy "${name}".m4a
    fi
    (( c++ ))
done
This is what chatgpt thinks about this snippet:
Quote
This script appears to be using awk and ffmpeg to process a file called detect.log and extract pairs of values (ssvar and tovar). It then rounds these values to 5 decimal places using the round function, which calls the awk_round function.

The awk_round function takes two arguments: the number of decimal places to round to, and the value to round. It then uses awk to print the rounded value with the specified number of decimal places.

The script then reads the input file detect.log line by line, using the read command in a while loop. For each line, it extracts the two values (ssvar and tovar) and rounds them to 5 decimal places using the round function.

Next, the script increments a counter (c) and formats it as a three-digit number with leading zeros (e.g. 001, 002, etc.). It then uses ffmpeg to process the input file input, using the -ss and -to options to specify the start and end times of the output file. The output file is named using the formatted counter value, with a .m4a extension. If tovar is not set, then ffmpeg will only use the -ss option and process the input file from the specified start time until the end. The -c:a option specifies that the audio stream should be copied to the output file without any further processing.
PANIC: CPU 1: Cache Error (unrecoverable - dcache data) Eframe = 0x90000000208cf3b8
NOTICE - cpu 0 didn't dump TLB, may be hung

Re: How to split one huge music file by detecting silence in ffmpeg

Reply #2
ok so how to use it?  :)) sorry im noob ^^ can i create a batch file using this? im on windows 10 pro

Re: How to split one huge music file by detecting silence in ffmpeg

Reply #3
You could (I forgot all about batch), or install WSL/Debian on win10 and start writing bash scripts. As already mentioned this is just a snippet. The way it can be tested is to run Linux via wsl and say
Code: [Select]
input="/path/to/my/example.m4a"
# And then pasting the snippet
PANIC: CPU 1: Cache Error (unrecoverable - dcache data) Eframe = 0x90000000208cf3b8
NOTICE - cpu 0 didn't dump TLB, may be hung

Re: How to split one huge music file by detecting silence in ffmpeg

Reply #4
i tried to make batch scripts using awk but they simply do not work on windows


Re: How to split one huge music file by detecting silence in ffmpeg

Reply #6
but does it support wma? the file i want to split is a wma file

Re: How to split one huge music file by detecting silence in ffmpeg

Reply #7
it doesnt support wma, i want to split a large wma file to single ones and then convert to dsd, i care about as little convertion processes as possible, i just want to use ffmpeg and some awk script for that if needed, can someone tell me step by step how to do it?

Re: How to split one huge music file by detecting silence in ffmpeg

Reply #8
What about SoX?
http://madskjeldgaard.dk/posts/sox-tutorial-split-by-silence/
You would have to convert to wav and back to wma though.
So, ffmpeg to decode, SoX to split, ffmpeg to encode back. I don't have the time now to put this into a batch script for you.
lame -V 0

Re: How to split one huge music file by detecting silence in ffmpeg

Reply #9
i want to split a large wma file to single ones and then convert to dsd
If it's just one file to address, load the big WMA file into Audacity and mark the split points manually.  Then Export-Multiple to WAV and use a converter to get DSD.

Re: How to split one huge music file by detecting silence in ffmpeg

Reply #10
If you have something that writes a cuesheet for the split points but only accepts WAVE, then you can apply that cuesheet to the wma afterwards? Beware though that timings with wma has been a fishy matter sometimes.

(I cannot help myself asking what is the reason to convert wma to DSD?!?)

Re: How to split one huge music file by detecting silence in ffmpeg

Reply #11
i would like to use audacity for that but its too much fatigue i think. the file i want to split contains at least 30 tracks

Re: How to split one huge music file by detecting silence in ffmpeg

Reply #12
Do any of the tracks have random silence in them, or do any tracks overlap?  That could cause a script to not output the desired results. 

Edit:  I think ffmpeg supports wma, so you can use that to split up the file without needing to re-encode.  Using Sox or audacity would output to wave.

Re: How to split one huge music file by detecting silence in ffmpeg

Reply #13
Put this together.  Not sure how well ffmpeg's silence detection is.  YMMV.

Usage:  ./script.sh <inputfile>
Code: [Select]
#!/bin/bash

# Detect silence.  Change dB value to adjust silence detection.
ffmpeg -i "${1}" -af silencedetect=noise=-25dB:d=1 -f null - 2> silence_detected.txt

# Filter detected results for end times. 
awk '/silence_end/ {print $5}' silence_detected.txt > silence_filtered.txt

# Get total file duration (in seconds) and append to filtered text file.
ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${1}" >> silence_filtered.txt

# Loop through the silence_filtered.txt file, use those times to split the file.
# New files are named with their starting time from original file.
S_TIME="0"
while read p; do
E_TIME="${p}"
ffmpeg -v error -nostdin -i "${1}" -ss ${S_TIME} -t ${E_TIME} -c:a copy ${S_TIME}.wav
S_TIME="${E_TIME}"
done < silence_filtered.txt

Re: How to split one huge music file by detecting silence in ffmpeg

Reply #14
...but that's not going to work in Windows.
It's your privilege to disagree, but that doesn't make you right and me wrong.

Re: How to split one huge music file by detecting silence in ffmpeg

Reply #15
...but that's not going to work in Windows.

True.  But I'm sure it could be adapted to a batch file.  Or there's options to run bash scripts with Cygwin or WSL under Windows.

 

Re: How to split one huge music file by detecting silence in ffmpeg

Reply #16
But I'm sure it could be adapted to a batch file.
I'm not sure about the "loop through the silence_filtered.txt file" part, and even so the OP would need to find a "DOS" version of AWK.

Or there's options to run bash scripts with Cygwin or WSL under Windows.
But doesn't the availability of AWK remain a problem?

I think it would be simpler to actually run that in Linux, using a virtual machine or live boot.
It's your privilege to disagree, but that doesn't make you right and me wrong.

Re: How to split one huge music file by detecting silence in ffmpeg

Reply #17
i would like to use audacity for that but its too much fatigue i think. the file i want to split contains at least 30 tracks
If you load the file into Audacity, select 'Analyze > Label Sounds', choose 'Region around sounds' for the label type (and modify the other settings according to your needs) and then click Apply it should generate a new track containing labels for each part of your main track split by silence. You can then export them all from 'File > Export > Export Multiple'.

Re: How to split one huge music file by detecting silence in ffmpeg

Reply #18
But I'm sure it could be adapted to a batch file.
I'm not sure about the "loop through the silence_filtered.txt file" part, and even so the OP would need to find a "DOS" version of AWK.

Or there's options to run bash scripts with Cygwin or WSL under Windows.
But doesn't the availability of AWK remain a problem?

I think it would be simpler to actually run that in Linux, using a virtual machine or live boot.

I suppose awk could be replaced with grep and cut, but I'm not sure if they're available on Windows or behave the same.

Linux in a VM might be the easiest method.


Re: How to split one huge music file by detecting silence in ffmpeg

Reply #20
WSL is Linux, so you can be certain that any program you run in Linux will work the same in WSL. The only thing I can think of that might be different is saving the script in the first place - Windows text editors tend to assume you want Windows (CRLF) line endings, but Linux programs don't always handle that very gracefully.

Cygwin (and Cygwin derivatives like MSYS2) sometimes provide special Windows-specific versions of some programs. Occasionally the name of the Linux version will be changed to prevent a conflict, so you may need to modify the script to use the correct program version if you're using Cygwin.

Whether you choose WSL or Cygwin, common programs like AWK are available and can be installed through the package manager if they're not installed by default.

WSL has one major advantage over the other options: you can hold shift and right-click in a folder (or on the desktop) to open a Linux shell with that folder already set as the current directory.

Re: How to split one huge music file by detecting silence in ffmpeg

Reply #21
WSL is Linux, so you can be certain that any program you run in Linux will work the same in WSL.
Interesting.

The only thing I can think of that might be different is saving the script in the first place - Windows text editors tend to assume you want Windows (CRLF) line endings, but Linux programs don't always handle that very gracefully.
Easily solved by using Notepad++.  If creating a new file you can choose what style of line ending you want, and for an existing file it auto-detects.

GnuWin32 saved me more than once when I migrated to Windows. Packages: https://gnuwin32.sourceforge.net/packages.html
Also interesting.
It's your privilege to disagree, but that doesn't make you right and me wrong.

Re: How to split one huge music file by detecting silence in ffmpeg

Reply #22
^For changing line endings, there is also dos2unix on the linux side (should be in repos).

Back to OP: sox example on the bottom of this page https://madskjeldgaard.dk/posts/sox-tutorial-split-by-silence/ (no idea if that will work directly on wma).
PANIC: CPU 1: Cache Error (unrecoverable - dcache data) Eframe = 0x90000000208cf3b8
NOTICE - cpu 0 didn't dump TLB, may be hung