Last active
March 9, 2020 22:26
-
-
Save wilforlan/a6d13510fd8a67406a69925b349d4437 to your computer and use it in GitHub Desktop.
Get the Silence Information of an audio file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const readline = require('readline'); | |
const { exec } = require('child_process'); | |
let MATCH_REGEX = /silence_end.*|silence_start.*/gm | |
const findSilenceParts = (stream, callback, options = {}) => { | |
return new Promise((resolve, reject) => { | |
let { overrideRegex } = options | |
let matches = []; | |
let duration = null; | |
var lineReader = readline.createInterface({ | |
input: stream | |
}); | |
lineReader.on('line', function (line) { | |
let match = line.match(overrideRegex || MATCH_REGEX); | |
if(!duration) duration = line.match(/Duration: (\d\d):(\d\d):(\d\d(\.\d\d)?)/g); | |
if(match) matches.push(match[0]); | |
}); | |
lineReader.on('close', () => { | |
resolve({ matches, duration: (duration && duration[0] && duration[0].split('Duration: '))[1]}); // find a better way get duration | |
}); | |
}) | |
}; | |
const getLogPath = (file, find, replace) => { | |
let lk = file.split('/'); | |
let logPath = lk[lk.length - 1]; | |
return logPath.replace(find, replace); | |
}; | |
const rm = (file) => { | |
fs.unlink(file, (err) => { | |
if(err) { console.log(err) } | |
else { console.log(`Remove file: ${file}`)} | |
}); | |
}; | |
const getSilenceInFile = (inputFile, useLog = null) => { | |
// ffmpeg -i /Users/wilforlan/Documents/Fireflies/voice-puppet-ff/store/TestWebSocketsFF-de07a7c5.mp3 -filter_complex "[0:a]silencedetect=n=-50dB:d=1[outa]" -map [outa] call-3.mp3 -y 2>&1 | cut -d 'l' -f 3 | cut -d ']' -f 2 | cut -d "|" -f 1 > log-2.txt | |
let logPath = getLogPath(inputFile, '.mp3', '-silencelog.log'); | |
let tempReWritePath = getLogPath(inputFile, '.mp3', '-temp.mp3'); | |
let cmd = `ffmpeg -i ${inputFile} -filter_complex "[0:a]silencedetect=n=-50dB:d=1[outa]" -map [outa] ${tempReWritePath} -y 2>&1 | cut -d ']' -f 2 | cut -d "|" -f 1 > ${logPath}`; | |
console.log({ cmd }); | |
return new Promise((resolve, reject) => { | |
if(useLog) return resolve(useLog); // testing purposes if you already have a log file | |
exec(cmd, (err, stdout) => { | |
if(err) reject(err); | |
rm(tempReWritePath); | |
resolve(logPath); | |
}); | |
}); | |
}; | |
const chunkArray = (arr, n) => { | |
return new Array(Math.ceil(arr.length / n)).fill("") | |
.map(function() { return this.splice(0, n) }, arr.slice()); | |
} | |
function sec2time(timeInSeconds) { | |
var pad = function(num, size) { return ('000' + num).slice(size * -1); }, | |
time = parseFloat(timeInSeconds).toFixed(3), | |
hours = Math.floor(time / 60 / 60), | |
minutes = Math.floor(time / 60) % 60, | |
seconds = Math.floor(time - minutes * 60), | |
milliseconds = time.slice(-3); | |
return pad(hours, 2) + ':' + pad(minutes, 2) + ':' + pad(seconds, 2) + '.' + pad(milliseconds, 2); | |
} | |
const buildFinalResults = (match) => { | |
return new Promise((resolve, reject) => { | |
let result = chunkArray(match, 2).map( innerArray => { | |
let silence_start = parseFloat(innerArray[0].split("silence_start: ")[1]); | |
let silence_end = parseFloat(innerArray[1].split("silence_end: ")[1]); | |
let diff = (silence_end - silence_start).toFixed(3); | |
return { | |
diff, | |
parsed: { | |
silence_start: sec2time(silence_start), | |
silence_end: sec2time(silence_end) | |
}, | |
raw: { | |
silence_start, | |
silence_end | |
} | |
} | |
}); | |
let totalSilenceDuration = result.reduce((acc, obj) => { | |
return acc + parseFloat(obj.diff) | |
}, 0.0); // in seconds | |
resolve({ result, totalSilenceDuration, totalSilenceDurationInMins: sec2time(totalSilenceDuration) }); | |
}) | |
}; | |
// Usage | |
(async () => { | |
try { | |
let log = await getSilenceInFile("https://somelink.mp3", "path-to-log/call-67243012-silencelog.log"); | |
let { matches, duration } = await findSilenceParts(fs.createReadStream(log)) | |
let response = await buildFinalResults(matches); | |
if(response) response.duration = duration | |
// rm(log); | |
console.log("%j", response) | |
// Check the comment section for a sample of the JSON Log and a sample log file. | |
} catch (error) { | |
} | |
})(); |
Author
wilforlan
commented
Mar 9, 2020
Sample Audio Log
ffmpeg version 4.1.1 Copyright (c) 2000-2019 the FFmpeg developers
built with Apple LLVM version 10.0.0 (clang-1000.11.45.5)
configuration: --prefix=/usr/local/Cellar/ffmpeg/4.1.1 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home/include/darwin' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-videotoolbox --disable-libjack --disable-indev=jack --enable-libaom --enable-libsoxr
libavutil 56. 22.100 / 56. 22.100
libavcodec 58. 35.100 / 58. 35.100
libavformat 58. 20.100 / 58. 20.100
libavdevice 58. 5.100 / 58. 5.100
libavfilter 7. 40.101 / 7. 40.101
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 3.100 / 5. 3.100
libswresample 3. 3.100 / 3. 3.100
libpostproc 55. 3.100 / 55. 3.100
Input #0, mp3, from 'https://somelink.mp3':
Metadata:
encoder : Lavf58.29.100
Duration: 00:29:24.75, start: 0.025057, bitrate: 64 kb/s
Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 64 kb/s
Metadata:
encoder : Lavc58.54
Stream mapping:
Stream #0:0 (mp3float) -> silencedetect
silencedetect -> Stream #0:0 (libmp3lame)
to stop, [?
Output #0, mp3, to 'call-67243012-temp.mp3':
Metadata:
TSSE : Lavf58.20.100
Stream #0:0: Audio: mp3 (libmp3lame), 44100 Hz, stereo, fltp
Metadata:
encoder : Lavc58.35.100 libmp3lame
silence_start: 198.897
silence_end: 199.989
silence_start: 203.915
silence_end: 204.981
silence_start: 206.111
silence_end: 207.398
silence_start: 233.476
silence_end: 234.508
silence_start: 243.236
silence_end: 244.333
silence_start: 251.319
silence_end: 252.519
silence_start: 323.123
silence_end: 324.404
silence_start: 381.242
silence_end: 382.251
silence_start: 508.828
silence_end: 510.489
silence_start: 520.775
silence_end: 522.018
silence_start: 622.635
silence_end: 623.741
silence_start: 637.885
silence_end: 639.564
silence_start: 740.868
silence_end: 742.218
silence_start: 766.365
silence_end: 767.373
silence_start: 787.332
silence_end: 788.821
silence_start: 842.353
silence_end: 844.004
silence_start: 869.676
silence_end: 871.361
silence_start: 874.452
silence_end: 876.29
silence_start: 1123.16
silence_end: 1125.4
silence_start: 1125.4
size= 17664kB time=00:18:51.31 bitrate= 127.9kbits/s speed=10.2x
size= 17664kB time=00:18:55.64 bitrate= 127.4kbits/s speed=10.2x
size= 17664kB time=00:18:59.61 bitrate= 127.0kbits/s speed=10.2x
size= 17664kB time=00:19:04.34 bitrate= 126.5kbits/s speed=10.2x
size= 17920kB time=00:19:10.87 bitrate= 127.6kbits/s speed=10.2x
size= 17920kB time=00:19:17.27 bitrate= 126.8kbits/s speed=10.2x
size= 17920kB time=00:19:19.60 bitrate= 126.6kbits/s speed=10.2x
size= 18176kB time=00:19:26.10 bitrate= 127.7kbits/s speed=10.1x
size= 18176kB time=00:19:32.63 bitrate= 127.0kbits/s speed=10.1x
size= 18176kB time=00:19:37.00 bitrate= 126.5kbits/s speed=10.1x
size= 18432kB time=00:19:43.53 bitrate= 127.6kbits/s speed= 10x
size= 18432kB time=00:19:47.86 bitrate= 127.1kbits/s speed= 10x
size= 18432kB time=00:19:52.22 bitrate= 126.6kbits/s speed=9.97x
size= 18432kB time=00:19:54.39 bitrate= 126.4kbits/s speed=9.95x
size= 18688kB time=00:20:00.79 bitrate= 127.5kbits/s speed=9.95x
size= 18688kB time=00:20:03.12 bitrate= 127.2kbits/s speed=9.93x
size= 18688kB time=00:20:07.45 bitrate= 126.8kbits/s speed=9.92x
size= 18944kB time=00:20:13.98 bitrate= 127.8kbits/s speed=9.92x
size= 18944kB time=00:20:20.52 bitrate= 127.2kbits/s speed=9.93x
size= 18944kB time=00:20:27.05 bitrate= 126.5kbits/s speed=9.94x
size= 19200kB time=00:20:33.58 bitrate= 127.5kbits/s speed=9.93x
size= 19200kB time=00:20:42.28 bitrate= 126.6kbits/s speed=9.96x
size= 19456kB time=00:20:53.14 bitrate= 127.2kbits/s speed=9.99x
size= 19712kB time=00:21:02.10 bitrate= 127.9kbits/s speed= 10x
size= 19712kB time=00:21:08.40 bitrate= 127.3kbits/s speed= 10x
size= 19712kB time=00:21:17.10 bitrate= 126.4kbits/s speed= 10x
size= 19968kB time=00:21:25.66 bitrate= 127.2kbits/s speed=10.1x
size= 20224kB time=00:21:34.49 bitrate= 128.0kbits/s speed=10.1x
size= 20224kB time=00:21:41.02 bitrate= 127.3kbits/s speed=10.1x
size= 20224kB time=00:21:47.42 bitrate= 126.7kbits/s speed=10.1x
size= 20480kB time=00:21:51.92 bitrate= 127.9kbits/s speed=10.1x
size= 20480kB time=00:21:56.25 bitrate= 127.5kbits/s speed=10.1x
size= 20480kB time=00:22:02.16 bitrate= 126.9kbits/s speed=10.1x
size= 20480kB time=00:22:04.95 bitrate= 126.6kbits/s speed= 10x
size= 20736kB time=00:22:11.48 bitrate= 127.6kbits/s speed= 10x
size= 20736kB time=00:22:15.85 bitrate= 127.2kbits/s speed= 10x
size= 20736kB time=00:22:22.38 bitrate= 126.5kbits/s speed= 10x
size= 20992kB time=00:22:28.78 bitrate= 127.5kbits/s speed= 10x
size= 20992kB time=00:22:33.24 bitrate= 127.1kbits/s speed= 10x
size= 20992kB time=00:22:37.61 bitrate= 126.7kbits/s speed= 10x
size= 21248kB time=00:22:45.52 bitrate= 127.5kbits/s speed= 10x
size= 21248kB time=00:22:48.47 bitrate= 127.2kbits/s speed= 10x
size= 21248kB time=00:22:52.84 bitrate= 126.8kbits/s speed=9.98x
size= 21504kB time=00:22:59.37 bitrate= 127.7kbits/s speed=9.98x
size= 21504kB time=00:23:03.70 bitrate= 127.3kbits/s speed=9.98x
size= 21504kB time=00:23:08.06 bitrate= 126.9kbits/s speed=9.97x
size= 21760kB time=00:23:14.60 bitrate= 127.8kbits/s speed=9.98x
size= 21760kB time=00:23:18.96 bitrate= 127.4kbits/s speed=9.97x
size= 21760kB time=00:23:27.66 bitrate= 126.6kbits/s speed=9.98x
size= 22016kB time=00:23:36.09 bitrate= 127.4kbits/s speed= 10x
size= 22016kB time=00:23:40.72 bitrate= 126.9kbits/s speed= 10x
size= 22272kB time=00:23:47.22 bitrate= 127.8kbits/s speed= 10x
size= 22272kB time=00:23:55.95 bitrate= 127.1kbits/s speed= 10x
size= 22272kB time=00:24:00.28 bitrate= 126.7kbits/s speed= 10x
size= 22528kB time=00:24:11.57 bitrate= 127.1kbits/s speed=10.1x
size= 22784kB time=00:24:19.88 bitrate= 127.9kbits/s speed=10.1x
size= 22784kB time=00:24:32.94 bitrate= 126.7kbits/s speed=10.1x
size= 23040kB time=00:24:46.00 bitrate= 127.0kbits/s speed=10.2x
size= 23296kB time=00:24:52.50 bitrate= 127.9kbits/s speed=10.2x
size= 23296kB time=00:25:01.23 bitrate= 127.1kbits/s speed=10.2x
size= 23552kB time=00:25:14.16 bitrate= 127.4kbits/s speed=10.3x
size= 23552kB time=00:25:20.48 bitrate= 126.9kbits/s speed=10.3x
size= 23808kB time=00:25:25.16 bitrate= 127.9kbits/s speed=10.2x
size= 23808kB time=00:25:31.69 bitrate= 127.3kbits/s speed=10.2x
size= 23808kB time=00:25:38.22 bitrate= 126.8kbits/s speed=10.2x
size= 24064kB time=00:25:44.75 bitrate= 127.6kbits/s speed=10.3x
size= 24064kB time=00:25:51.28 bitrate= 127.1kbits/s speed=10.3x
size= 24320kB time=00:25:57.78 bitrate= 127.9kbits/s speed=10.3x
size= 24320kB time=00:26:04.31 bitrate= 127.4kbits/s speed=10.3x
size= 24576kB time=00:26:13.04 bitrate= 128.0kbits/s speed=10.3x
size= 24576kB time=00:26:19.54 bitrate= 127.5kbits/s speed=10.3x
size= 24576kB time=00:26:28.14 bitrate= 126.8kbits/s speed=10.3x
size= 24832kB time=00:26:32.60 bitrate= 127.7kbits/s speed=10.3x
size= 24832kB time=00:26:39.13 bitrate= 127.2kbits/s speed=10.3x
size= 25088kB time=00:26:45.66 bitrate= 128.0kbits/s speed=10.3x
size= 25088kB time=00:27:00.89 bitrate= 126.8kbits/s speed=10.3x
size= 25344kB time=00:27:11.79 bitrate= 127.2kbits/s speed=10.4x
size= 25600kB time=00:27:24.82 bitrate= 127.5kbits/s speed=10.4x
size= 25600kB time=00:27:31.35 bitrate= 127.0kbits/s speed=10.4x
size= 25856kB time=00:27:40.08 bitrate= 127.6kbits/s speed=10.4x
size= 25856kB time=00:27:45.07 bitrate= 127.2kbits/s speed=10.4x
size= 25856kB time=00:27:50.94 bitrate= 126.8kbits/s speed=10.4x
size= 26112kB time=00:27:57.48 bitrate= 127.5kbits/s speed=10.4x
size= 26112kB time=00:28:06.17 bitrate= 126.9kbits/s speed=10.5x
size= 26368kB time=00:28:14.77 bitrate= 127.5kbits/s speed=10.5x
size= 26368kB time=00:28:19.24 bitrate= 127.1kbits/s speed=10.5x
size= 26624kB time=00:28:25.79 bitrate= 127.9kbits/s speed=10.5x
size= 26624kB time=00:28:34.46 bitrate= 127.2kbits/s speed=10.5x
size= 26880kB time=00:28:45.36 bitrate= 127.6kbits/s speed=10.5x
size= 26880kB time=00:28:54.06 bitrate= 127.0kbits/s speed=10.5x
size= 27136kB time=00:28:58.39 bitrate= 127.9kbits/s speed=10.5x
size= 27136kB time=00:29:05.18 bitrate= 127.4kbits/s speed=10.5x
size= 27136kB time=00:29:12.79 bitrate= 126.8kbits/s speed=10.5x
size= 27392kB time=00:29:20.15 bitrate= 127.5kbits/s speed=10.5x
size= 27575kB time=00:29:24.72 bitrate= 128.0kbits/s speed=10.5x
video:0kB audio:27574kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000896%
silence_end: 1764.72
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment