1 import { ffprobe, FfprobeData } from 'fluent-ffmpeg'
2 import { VideoFileMetadata } from '@shared/models/videos'
6 * Helpers to run ffprobe and extract data from the JSON output
10 function ffprobePromise (path: string) {
11 return new Promise<FfprobeData>((res, rej) => {
12 ffprobe(path, (err, data) => {
13 if (err) return rej(err)
20 async function getAudioStream (videoPath: string, existingProbe?: FfprobeData) {
21 // without position, ffprobe considers the last input only
22 // we make it consider the first input only
23 // if you pass a file path to pos, then ffprobe acts on that file directly
24 const data = existingProbe || await ffprobePromise(videoPath)
26 if (Array.isArray(data.streams)) {
27 const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio')
31 absolutePath: data.format.filename,
33 bitrate: parseInt(audioStream['bit_rate'] + '', 10)
38 return { absolutePath: data.format.filename }
41 function getMaxAudioBitrate (type: 'aac' | 'mp3' | string, bitrate: number) {
42 const maxKBitrate = 384
43 const kToBits = (kbits: number) => kbits * 1000
45 // If we did not manage to get the bitrate, use an average value
46 if (!bitrate) return 256
50 case bitrate > kToBits(maxKBitrate):
54 return -1 // we interpret it as a signal to copy the audio stream as is
59 a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac.
60 That's why, when using aac, we can go to lower kbit/sec. The equivalences
61 made here are not made to be accurate, especially with good mp3 encoders.
64 case bitrate <= kToBits(192):
67 case bitrate <= kToBits(384):
75 async function getVideoStreamSize (path: string, existingProbe?: FfprobeData): Promise<{ width: number, height: number }> {
76 const videoStream = await getVideoStreamFromFile(path, existingProbe)
78 return videoStream === null
79 ? { width: 0, height: 0 }
80 : { width: videoStream.width, height: videoStream.height }
83 async function getVideoFileResolution (path: string, existingProbe?: FfprobeData) {
84 const size = await getVideoStreamSize(path, existingProbe)
89 ratio: Math.max(size.height, size.width) / Math.min(size.height, size.width),
90 resolution: Math.min(size.height, size.width),
91 isPortraitMode: size.height > size.width
95 async function getVideoFileFPS (path: string, existingProbe?: FfprobeData) {
96 const videoStream = await getVideoStreamFromFile(path, existingProbe)
97 if (videoStream === null) return 0
99 for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) {
100 const valuesText: string = videoStream[key]
101 if (!valuesText) continue
103 const [ frames, seconds ] = valuesText.split('/')
104 if (!frames || !seconds) continue
106 const result = parseInt(frames, 10) / parseInt(seconds, 10)
107 if (result > 0) return Math.round(result)
113 async function getMetadataFromFile (path: string, existingProbe?: FfprobeData) {
114 const metadata = existingProbe || await ffprobePromise(path)
116 return new VideoFileMetadata(metadata)
119 async function getVideoFileBitrate (path: string, existingProbe?: FfprobeData): Promise<number> {
120 const metadata = await getMetadataFromFile(path, existingProbe)
122 let bitrate = metadata.format.bit_rate as number
123 if (bitrate && !isNaN(bitrate)) return bitrate
125 const videoStream = await getVideoStreamFromFile(path, existingProbe)
126 if (!videoStream) return undefined
128 bitrate = videoStream?.bit_rate
129 if (bitrate && !isNaN(bitrate)) return bitrate
134 async function getDurationFromVideoFile (path: string, existingProbe?: FfprobeData) {
135 const metadata = await getMetadataFromFile(path, existingProbe)
137 return Math.round(metadata.format.duration)
140 async function getVideoStreamFromFile (path: string, existingProbe?: FfprobeData) {
141 const metadata = await getMetadataFromFile(path, existingProbe)
143 return metadata.streams.find(s => s.codec_type === 'video') || null
146 async function canDoQuickAudioTranscode (path: string, probe?: FfprobeData): Promise<boolean> {
147 const parsedAudio = await getAudioStream(path, probe)
149 if (!parsedAudio.audioStream) return true
151 if (parsedAudio.audioStream['codec_name'] !== 'aac') return false
153 const audioBitrate = parsedAudio.bitrate
154 if (!audioBitrate) return false
156 const maxAudioBitrate = getMaxAudioBitrate('aac', audioBitrate)
157 if (maxAudioBitrate !== -1 && audioBitrate > maxAudioBitrate) return false
159 const channelLayout = parsedAudio.audioStream['channel_layout']
160 // Causes playback issues with Chrome
161 if (!channelLayout || channelLayout === 'unknown') return false
166 // ---------------------------------------------------------------------------
170 getVideoFileResolution,
173 getVideoStreamFromFile,
174 getDurationFromVideoFile,
179 canDoQuickAudioTranscode