-/**
- * A toolbox to play with audio
- */
-namespace audio {
- export const get = (videoPath: string) => {
- // without position, ffprobe considers the last input only
- // we make it consider the first input only
- // if you pass a file path to pos, then ffprobe acts on that file directly
- return new Promise<{ absolutePath: string, audioStream?: any }>((res, rej) => {
-
- function parseFfprobe (err: any, data: ffmpeg.FfprobeData) {
- if (err) return rej(err)
-
- if ('streams' in data) {
- const audioStream = data.streams.find(stream => stream[ 'codec_type' ] === 'audio')
- if (audioStream) {
- return res({
- absolutePath: data.format.filename,
- audioStream
- })
- }
- }
-
- return res({ absolutePath: data.format.filename })
- }
-
- return ffmpeg.ffprobe(videoPath, parseFfprobe)
- })
- }
-
- export namespace bitrate {
- const baseKbitrate = 384
-
- const toBits = (kbits: number) => kbits * 8000
-
- export const aac = (bitrate: number): number => {
- switch (true) {
- case bitrate > toBits(baseKbitrate):
- return baseKbitrate
-
- default:
- return -1 // we interpret it as a signal to copy the audio stream as is
- }
- }
-
- export const mp3 = (bitrate: number): number => {
- /*
- a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac.
- That's why, when using aac, we can go to lower kbit/sec. The equivalences
- made here are not made to be accurate, especially with good mp3 encoders.
- */
- switch (true) {
- case bitrate <= toBits(192):
- return 128
-
- case bitrate <= toBits(384):
- return 256
-
- default:
- return baseKbitrate
- }
- }
- }
-}
-