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 // ---------------------------------------------------------------------------
22 // ---------------------------------------------------------------------------
24 async function isAudioFile (path: string, existingProbe?: FfprobeData) {
25 const videoStream = await getVideoStream(path, existingProbe)
30 async function hasAudioStream (path: string, existingProbe?: FfprobeData) {
31 const { audioStream } = await getAudioStream(path, existingProbe)
36 async function getAudioStream (videoPath: string, existingProbe?: FfprobeData) {
37 // without position, ffprobe considers the last input only
38 // we make it consider the first input only
39 // if you pass a file path to pos, then ffprobe acts on that file directly
40 const data = existingProbe || await ffprobePromise(videoPath)
42 if (Array.isArray(data.streams)) {
43 const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio')
47 absolutePath: data.format.filename,
49 bitrate: parseInt(audioStream['bit_rate'] + '', 10)
54 return { absolutePath: data.format.filename }
57 function getMaxAudioBitrate (type: 'aac' | 'mp3' | string, bitrate: number) {
58 const maxKBitrate = 384
59 const kToBits = (kbits: number) => kbits * 1000
61 // If we did not manage to get the bitrate, use an average value
62 if (!bitrate) return 256
66 case bitrate > kToBits(maxKBitrate):
70 return -1 // we interpret it as a signal to copy the audio stream as is
75 a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac.
76 That's why, when using aac, we can go to lower kbit/sec. The equivalences
77 made here are not made to be accurate, especially with good mp3 encoders.
80 case bitrate <= kToBits(192):
83 case bitrate <= kToBits(384):
91 // ---------------------------------------------------------------------------
93 // ---------------------------------------------------------------------------
95 async function getVideoStreamDimensionsInfo (path: string, existingProbe?: FfprobeData) {
96 const videoStream = await getVideoStream(path, existingProbe)
97 if (!videoStream) return undefined
100 width: videoStream.width,
101 height: videoStream.height,
102 ratio: Math.max(videoStream.height, videoStream.width) / Math.min(videoStream.height, videoStream.width),
103 resolution: Math.min(videoStream.height, videoStream.width),
104 isPortraitMode: videoStream.height > videoStream.width
108 async function getVideoStreamFPS (path: string, existingProbe?: FfprobeData) {
109 const videoStream = await getVideoStream(path, existingProbe)
110 if (!videoStream) return 0
112 for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) {
113 const valuesText: string = videoStream[key]
114 if (!valuesText) continue
116 const [ frames, seconds ] = valuesText.split('/')
117 if (!frames || !seconds) continue
119 const result = parseInt(frames, 10) / parseInt(seconds, 10)
120 if (result > 0) return Math.round(result)
126 async function buildFileMetadata (path: string, existingProbe?: FfprobeData) {
127 const metadata = existingProbe || await ffprobePromise(path)
129 return new VideoFileMetadata(metadata)
132 async function getVideoStreamBitrate (path: string, existingProbe?: FfprobeData): Promise<number> {
133 const metadata = await buildFileMetadata(path, existingProbe)
135 let bitrate = metadata.format.bit_rate as number
136 if (bitrate && !isNaN(bitrate)) return bitrate
138 const videoStream = await getVideoStream(path, existingProbe)
139 if (!videoStream) return undefined
141 bitrate = videoStream?.bit_rate
142 if (bitrate && !isNaN(bitrate)) return bitrate
147 async function getVideoStreamDuration (path: string, existingProbe?: FfprobeData) {
148 const metadata = await buildFileMetadata(path, existingProbe)
150 return Math.round(metadata.format.duration)
153 async function getVideoStream (path: string, existingProbe?: FfprobeData) {
154 const metadata = await buildFileMetadata(path, existingProbe)
156 return metadata.streams.find(s => s.codec_type === 'video')
159 // ---------------------------------------------------------------------------
162 getVideoStreamDimensionsInfo,
166 getVideoStreamDuration,
171 getVideoStreamBitrate,