5

I am trying to detect silence at the end of an audio file.
I have made some progress with ffmpeg library. Here I used silencedetect to list all the silences in an audio file.

ffmpeg -i audio.wav -af silencedetect=n=-50dB:d=0.5 -f null - 2> /home/aliakber/log.txt

Here is the output of the command:

--With silence at the front and end of the audio file--

[silencedetect @ 0x1043060] silence_start: 0.484979
[silencedetect @ 0x1043060] silence_end: 1.36898 | silence_duration: 0.884
[silencedetect @ 0x1043060] silence_start: 2.57298
[silencedetect @ 0x1043060] silence_end: 3.48098 | silence_duration: 0.908
[silencedetect @ 0x1043060] silence_start: 4.75698
size=N/A time=00:00:05.56 bitrate=N/A

--Without silence at the front and end of the audio file--

[silencedetect @ 0x106fd60] silence_start: 0.353333
[silencedetect @ 0x106fd60] silence_end: 1.25867 | silence_duration: 0.905333
[silencedetect @ 0x106fd60] silence_start: 2.46533
[silencedetect @ 0x106fd60] silence_end: 3.37067 | silence_duration: 0.905333
size=N/A time=00:00:04.61 bitrate=N/A

But I want something more flexible so that I can manipulate the output and do further task depending on the result.
I want to get the output something like true or false. If there is a certain period of silence exists at the end of the audio file it will return true and false otherwise.

Can someone suggest me an easy way to achieve this?

7
  • Are you sure, that ffmpeg is the best software for analyze? What are you going to do next with the result? You can write simple script (maybe on bash) that will analyze ffmpeg output and will return true or false. – Tarwirdur Turon Feb 28 '17 at 11:56
  • Also not clear what it means to "silence in the end." If at the end of the recording silent minute, and then at the very end - clicking the camera off button, if it is considered the silence? – Tarwirdur Turon Feb 28 '17 at 11:59
  • Is there an upper limit on how much silence there may be at the end? – Gyan Feb 28 '17 at 12:02
  • I have some audio recording of 2sec. I need to know if the recording is finished before user completes recording a sentence. If there is some silence at the end, I can assume that the sentence was finished. So all I need to know if that the audio file is completed or not. If there is silence for 0.05 sec i need false output and true otherwise. – Ali Akber Feb 28 '17 at 12:25
  • @TarwirdurTuron I'm not very good with bash programming. That's why I need to find an easy way. As you suggested using ffmpeg output, I need to parse the output to return the true/false result. But I want something short to do the task. – Ali Akber Feb 28 '17 at 12:28
10

Try this:

ffmpeg -i audio.wav -af silencedetect=n=-50dB:d=0.5 -f null - 2>&1 | grep -Eo "silence_(start|end)" | tail -n 1 | grep "start" | wc -l

Output:

  • 1 - there is silence at the end
  • 0 - there is no silence at the end

Explanation: As I see in the silence case there is no silence_end at the end of log.

  1. 2>&1 - redirect stderr to stdin
  2. grep -Eo "silence_(start|end)" - filter log and keep only silence_start and silence_end from log. Each by new line.
  3. tail -n 1 - get last line. (if it is. So now we there are 3 cases of state: 'silence_start', 'silence_end', <empty>)
  4. grep "start" - keep line only if it contains start (2 cases: 'silence_start', <empty>)
  5. wc -l - get number of lines. (1 in 'silence_start' and 0 in <empty> case)
5
  • The OP says, "* If there is silence for 0.05 sec i need false output and true otherwise.*" - so, you'll need to parse the start time and the total time and subtract. – Gyan Feb 28 '17 at 13:46
  • @Mulvya What is "silence"? :) See documentation for silencedetect: ffmpeg.org/ffmpeg-filters.html#toc-silencedetect . OP specified silence minimum duration in his command in post with d parameter. – Tarwirdur Turon Feb 28 '17 at 14:07
  • 1
    Oops. Skipped over the use of d. Although it should be 0.05 then. – Gyan Feb 28 '17 at 14:40
  • Thanks @TarwirdurTuron. It works. Can you please explain the code a little. Not sure what is happening behind the command :( – Ali Akber Mar 1 '17 at 4:41
  • Thanks :) I'm using this solution for now. – Ali Akber Mar 1 '17 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.