-function ffprobePromise (path: string) {
- return new Promise<ffmpeg.FfprobeData>((res, rej) => {
- ffmpeg.ffprobe(path, (err, data) => {
- if (err) return rej(err)
-
- return res(data)
- })
- })
-}
-
-async function getAudioStream (videoPath: string, existingProbe?: ffmpeg.FfprobeData) {
- // 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
- const data = existingProbe || await ffprobePromise(videoPath)
-
- if (Array.isArray(data.streams)) {
- const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio')
-
- if (audioStream) {
- return {
- absolutePath: data.format.filename,
- audioStream,
- bitrate: parseInt(audioStream['bit_rate'] + '', 10)
- }
- }
- }
-
- return { absolutePath: data.format.filename }
-}
-
-function getMaxAudioBitrate (type: 'aac' | 'mp3' | string, bitrate: number) {
- const maxKBitrate = 384
- const kToBits = (kbits: number) => kbits * 1000
-
- // If we did not manage to get the bitrate, use an average value
- if (!bitrate) return 256
-
- if (type === 'aac') {
- switch (true) {
- case bitrate > kToBits(maxKBitrate):
- return maxKBitrate
-
- default:
- return -1 // we interpret it as a signal to copy the audio stream as is
- }
- }
-
- /*
- 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 <= kToBits(192):
- return 128
-
- case bitrate <= kToBits(384):
- return 256
-
- default:
- return maxKBitrate
- }
-}
-
-async function getVideoStreamSize (path: string, existingProbe?: ffmpeg.FfprobeData) {
- const videoStream = await getVideoStreamFromFile(path, existingProbe)
-
- return videoStream === null
- ? { width: 0, height: 0 }
- : { width: videoStream.width, height: videoStream.height }
-}
-