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 isAudioFile (path: string, existingProbe?: FfprobeData) {
21 const videoStream = await getVideoStreamFromFile(path, existingProbe)
26 async function getAudioStream (videoPath: string, existingProbe?: FfprobeData) {
27 // without position, ffprobe considers the last input only
28 // we make it consider the first input only
29 // if you pass a file path to pos, then ffprobe acts on that file directly
30 const data = existingProbe || await ffprobePromise(videoPath)
32 if (Array.isArray(data.streams)) {
33 const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio')
37 absolutePath: data.format.filename,
39 bitrate: parseInt(audioStream['bit_rate'] + '', 10)
44 return { absolutePath: data.format.filename }
47 function getMaxAudioBitrate (type: 'aac' | 'mp3' | string, bitrate: number) {
48 const maxKBitrate = 384
49 const kToBits = (kbits: number) => kbits * 1000
51 // If we did not manage to get the bitrate, use an average value
52 if (!bitrate) return 256
56 case bitrate > kToBits(maxKBitrate):
60 return -1 // we interpret it as a signal to copy the audio stream as is
65 a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac.
66 That's why, when using aac, we can go to lower kbit/sec. The equivalences
67 made here are not made to be accurate, especially with good mp3 encoders.
70 case bitrate <= kToBits(192):
73 case bitrate <= kToBits(384):
81 async function getVideoStreamSize (path: string, existingProbe?: FfprobeData): Promise<{ width: number, height: number }> {
82 const videoStream = await getVideoStreamFromFile(path, existingProbe)
84 return videoStream === null
85 ? { width: 0, height: 0 }
86 : { width: videoStream.width, height: videoStream.height }
89 async function getVideoFileResolution (path: string, existingProbe?: FfprobeData) {
90 const size = await getVideoStreamSize(path, existingProbe)
95 ratio: Math.max(size.height, size.width) / Math.min(size.height, size.width),
96 resolution: Math.min(size.height, size.width),
97 isPortraitMode: size.height > size.width
101 async function getVideoFileFPS (path: string, existingProbe?: FfprobeData) {
102 const videoStream = await getVideoStreamFromFile(path, existingProbe)
103 if (videoStream === null) return 0
105 for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) {
106 const valuesText: string = videoStream[key]
107 if (!valuesText) continue
109 const [ frames, seconds ] = valuesText.split('/')
110 if (!frames || !seconds) continue
112 const result = parseInt(frames, 10) / parseInt(seconds, 10)
113 if (result > 0) return Math.round(result)
119 async function getMetadataFromFile (path: string, existingProbe?: FfprobeData) {
120 const metadata = existingProbe || await ffprobePromise(path)
122 return new VideoFileMetadata(metadata)
125 async function getVideoFileBitrate (path: string, existingProbe?: FfprobeData): Promise<number> {
126 const metadata = await getMetadataFromFile(path, existingProbe)
128 let bitrate = metadata.format.bit_rate as number
129 if (bitrate && !isNaN(bitrate)) return bitrate
131 const videoStream = await getVideoStreamFromFile(path, existingProbe)
132 if (!videoStream) return undefined
134 bitrate = videoStream?.bit_rate
135 if (bitrate && !isNaN(bitrate)) return bitrate
140 async function getDurationFromVideoFile (path: string, existingProbe?: FfprobeData) {
141 const metadata = await getMetadataFromFile(path, existingProbe)
143 return Math.round(metadata.format.duration)
146 async function getVideoStreamFromFile (path: string, existingProbe?: FfprobeData) {
147 const metadata = await getMetadataFromFile(path, existingProbe)
149 return metadata.streams.find(s => s.codec_type === 'video') || null
152 async function canDoQuickAudioTranscode (path: string, probe?: FfprobeData): Promise<boolean> {
153 const parsedAudio = await getAudioStream(path, probe)
155 if (!parsedAudio.audioStream) return true
157 if (parsedAudio.audioStream['codec_name'] !== 'aac') return false
159 const audioBitrate = parsedAudio.bitrate
160 if (!audioBitrate) return false
162 const maxAudioBitrate = getMaxAudioBitrate('aac', audioBitrate)
163 if (maxAudioBitrate !== -1 && audioBitrate > maxAudioBitrate) return false
165 const channelLayout = parsedAudio.audioStream['channel_layout']
166 // Causes playback issues with Chrome
167 if (!channelLayout || channelLayout === 'unknown') return false
172 // ---------------------------------------------------------------------------
176 getVideoFileResolution,
179 getVideoStreamFromFile,
180 getDurationFromVideoFile,
186 canDoQuickAudioTranscode