1 import { ffprobe, FfprobeData } from 'fluent-ffmpeg'
2 import { forceNumber } from '@shared/core-utils'
3 import { VideoFileMetadata, VideoResolution } from '@shared/models/videos'
7 * Helpers to run ffprobe and extract data from the JSON output
11 function ffprobePromise (path: string) {
12 return new Promise<FfprobeData>((res, rej) => {
13 ffprobe(path, (err, data) => {
14 if (err) return rej(err)
21 // ---------------------------------------------------------------------------
23 // ---------------------------------------------------------------------------
25 const imageCodecs = new Set([
26 'ansi', 'apng', 'bintext', 'bmp', 'brender_pix', 'dpx', 'exr', 'fits', 'gem', 'gif', 'jpeg2000', 'jpgls', 'mjpeg', 'mjpegb', 'msp2',
27 'pam', 'pbm', 'pcx', 'pfm', 'pgm', 'pgmyuv', 'pgx', 'photocd', 'pictor', 'png', 'ppm', 'psd', 'sgi', 'sunrast', 'svg', 'targa', 'tiff',
28 'txd', 'webp', 'xbin', 'xbm', 'xface', 'xpm', 'xwd'
31 async function isAudioFile (path: string, existingProbe?: FfprobeData) {
32 const videoStream = await getVideoStream(path, existingProbe)
33 if (!videoStream) return true
35 if (imageCodecs.has(videoStream.codec_name)) return true
40 async function hasAudioStream (path: string, existingProbe?: FfprobeData) {
41 const { audioStream } = await getAudioStream(path, existingProbe)
46 async function getAudioStream (videoPath: string, existingProbe?: FfprobeData) {
47 // without position, ffprobe considers the last input only
48 // we make it consider the first input only
49 // if you pass a file path to pos, then ffprobe acts on that file directly
50 const data = existingProbe || await ffprobePromise(videoPath)
52 if (Array.isArray(data.streams)) {
53 const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio')
57 absolutePath: data.format.filename,
59 bitrate: forceNumber(audioStream['bit_rate'])
64 return { absolutePath: data.format.filename }
67 function getMaxAudioBitrate (type: 'aac' | 'mp3' | string, bitrate: number) {
68 const maxKBitrate = 384
69 const kToBits = (kbits: number) => kbits * 1000
71 // If we did not manage to get the bitrate, use an average value
72 if (!bitrate) return 256
76 case bitrate > kToBits(maxKBitrate):
80 return -1 // we interpret it as a signal to copy the audio stream as is
85 a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac.
86 That's why, when using aac, we can go to lower kbit/sec. The equivalences
87 made here are not made to be accurate, especially with good mp3 encoders.
90 case bitrate <= kToBits(192):
93 case bitrate <= kToBits(384):
101 // ---------------------------------------------------------------------------
103 // ---------------------------------------------------------------------------
105 async function getVideoStreamDimensionsInfo (path: string, existingProbe?: FfprobeData) {
106 const videoStream = await getVideoStream(path, existingProbe)
112 resolution: VideoResolution.H_NOVIDEO,
113 isPortraitMode: false
118 width: videoStream.width,
119 height: videoStream.height,
120 ratio: Math.max(videoStream.height, videoStream.width) / Math.min(videoStream.height, videoStream.width),
121 resolution: Math.min(videoStream.height, videoStream.width),
122 isPortraitMode: videoStream.height > videoStream.width
126 async function getVideoStreamFPS (path: string, existingProbe?: FfprobeData) {
127 const videoStream = await getVideoStream(path, existingProbe)
128 if (!videoStream) return 0
130 for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) {
131 const valuesText: string = videoStream[key]
132 if (!valuesText) continue
134 const [ frames, seconds ] = valuesText.split('/')
135 if (!frames || !seconds) continue
137 const result = parseInt(frames, 10) / parseInt(seconds, 10)
138 if (result > 0) return Math.round(result)
144 async function buildFileMetadata (path: string, existingProbe?: FfprobeData) {
145 const metadata = existingProbe || await ffprobePromise(path)
147 return new VideoFileMetadata(metadata)
150 async function getVideoStreamBitrate (path: string, existingProbe?: FfprobeData): Promise<number> {
151 const metadata = await buildFileMetadata(path, existingProbe)
153 let bitrate = metadata.format.bit_rate as number
154 if (bitrate && !isNaN(bitrate)) return bitrate
156 const videoStream = await getVideoStream(path, existingProbe)
157 if (!videoStream) return undefined
159 bitrate = videoStream?.bit_rate
160 if (bitrate && !isNaN(bitrate)) return bitrate
165 async function getVideoStreamDuration (path: string, existingProbe?: FfprobeData) {
166 const metadata = await buildFileMetadata(path, existingProbe)
168 return Math.round(metadata.format.duration)
171 async function getVideoStream (path: string, existingProbe?: FfprobeData) {
172 const metadata = await buildFileMetadata(path, existingProbe)
174 return metadata.streams.find(s => s.codec_type === 'video')
177 // ---------------------------------------------------------------------------
180 getVideoStreamDimensionsInfo,
184 getVideoStreamDuration,
189 getVideoStreamBitrate,