Search code examples
javascriptnode.jsaudioffmpeg

Node js fluent-ffmpeg silencedetect info to CSV


I am trying get timestamps of in and out points of silent parts of an audio file. The duration of the silent part is 5-7sec, sometimes the part have some random single noises. Ideally as a final result I want get JSON or csv file with all info

I am new to nodeJS and I am not sure how to access the outputs of the ffmpeg 'silencedetect' filter. I have been trying to console.log output of the code below but it didn't work

var ffmpeg = require('fluent-ffmpeg');
ffmpeg(audioPath)
  .audioFilters('silencedetect=n=-50dB:d=5');

Audio wave example


Solution

  • in typescript:

        const silenceInfo: { stSec: number, endSec: number, duration: number }[] = []
    
        const silence = '-40dB'
        const sildur = '1'
        const results = ffmpeg(file, { logger: console })
          .audioFilters([
            {
              filter: 'silencedetect',
              options: { n: silence, d: sildur }
            },
            {
              filter: 'silenceremove',
              options: { stop_threshold: silence, stop_periods: -1, stop_duration: sildur,  }
            }
          ])
          .on('codecData', function (data: any) {
            console.log('Input is ' + data.audio + ' audio ' + 'with ' + data.video + ' video');
          })
          .on('stderr', function (stderrLine) {
            if (stderrLine.includes(' silence_end: ')) {
              const endInfo: string[] = stderrLine.split(' silence_end: ')[1].split(' | silence_duration: ')
              const [endSec, duration] = endInfo.map(s => +s)
              // console.log('Stderr output: ' + stderrLine)
              // console.log(endSec, duration)
              silenceInfo.push({ stSec: endSec - duration, endSec, duration })
            }
          })
          .on('error', function (error) {
            console.error('Error:', error, silence);
          })
          .on('end', function (stdout, stderr) {
            console.log('Transcoding succeeded,  silenceInfo:', silenceInfo);
          })
          .save(`${file}-silenceRemoved.mp3`)
    

    this will give you "json" to the console and in the silenceInfo variable

    silenceInfo: [
      { stSec: 23.959, endSec: 25.1835, duration: 1.2245 },
      { stSec: 68.51565000000001, endSec: 69.6354, duration: 1.11975 },
      { stSec: 99.38719, endSec: 100.808, duration: 1.42081 },
      { stSec: 136.76575, endSec: 138.307, duration: 1.54125 },
      { stSec: 146.7394, endSec: 148.059, duration: 1.3196 },
      { stSec: 176.83463, endSec: 178.419, duration: 1.58437 },
      { stSec: 190.16585, endSec: 191.876, duration: 1.71015 },
      { stSec: 193.69758, endSec: 195.385, duration: 1.68742 },
      { stSec: 198.92425, endSec: 200.326, duration: 1.40175 },
      { stSec: 204.13166999999999, endSec: 205.551, duration: 1.41933 },
      { stSec: 208.01035, endSec: 209.92, duration: 1.90965 }
    ]