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 const imageCodecs = new Set([
25 'ansi', 'apng', 'bintext', 'bmp', 'brender_pix', 'dpx', 'exr', 'fits', 'gem', 'gif', 'jpeg2000', 'jpgls', 'mjpeg', 'mjpegb', 'msp2',
26 'pam', 'pbm', 'pcx', 'pfm', 'pgm', 'pgmyuv', 'pgx', 'photocd', 'pictor', 'png', 'ppm', 'psd', 'sgi', 'sunrast', 'svg', 'targa', 'tiff',
27 'txd', 'webp', 'xbin', 'xbm', 'xface', 'xpm', 'xwd'
30 async function isAudioFile (path: string, existingProbe?: FfprobeData) {
31 const videoStream = await getVideoStream(path, existingProbe)
32 if (!videoStream) return true
34 if (imageCodecs.has(videoStream.codec_name)) return true
39 async function hasAudioStream (path: string, existingProbe?: FfprobeData) {
40 const { audioStream } = await getAudioStream(path, existingProbe)
45 async function getAudioStream (videoPath: string, existingProbe?: FfprobeData) {
46 // without position, ffprobe considers the last input only
47 // we make it consider the first input only
48 // if you pass a file path to pos, then ffprobe acts on that file directly
49 const data = existingProbe || await ffprobePromise(videoPath)
51 if (Array.isArray(data.streams)) {
52 const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio')
56 absolutePath: data.format.filename,
58 bitrate: parseInt(audioStream['bit_rate'] + '', 10)
63 return { absolutePath: data.format.filename }
66 function getMaxAudioBitrate (type: 'aac' | 'mp3' | string, bitrate: number) {
67 const maxKBitrate = 384
68 const kToBits = (kbits: number) => kbits * 1000
70 // If we did not manage to get the bitrate, use an average value
71 if (!bitrate) return 256
75 case bitrate > kToBits(maxKBitrate):
79 return -1 // we interpret it as a signal to copy the audio stream as is
84 a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac.
85 That's why, when using aac, we can go to lower kbit/sec. The equivalences
86 made here are not made to be accurate, especially with good mp3 encoders.
89 case bitrate <= kToBits(192):
92 case bitrate <= kToBits(384):
100 // ---------------------------------------------------------------------------
102 // ---------------------------------------------------------------------------
104 async function getVideoStreamDimensionsInfo (path: string, existingProbe?: FfprobeData) {
105 const videoStream = await getVideoStream(path, existingProbe)
106 if (!videoStream) return undefined
109 width: videoStream.width,
110 height: videoStream.height,
111 ratio: Math.max(videoStream.height, videoStream.width) / Math.min(videoStream.height, videoStream.width),
112 resolution: Math.min(videoStream.height, videoStream.width),
113 isPortraitMode: videoStream.height > videoStream.width
117 async function getVideoStreamFPS (path: string, existingProbe?: FfprobeData) {
118 const videoStream = await getVideoStream(path, existingProbe)
119 if (!videoStream) return 0
121 for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) {
122 const valuesText: string = videoStream[key]
123 if (!valuesText) continue
125 const [ frames, seconds ] = valuesText.split('/')
126 if (!frames || !seconds) continue
128 const result = parseInt(frames, 10) / parseInt(seconds, 10)
129 if (result > 0) return Math.round(result)
135 async function buildFileMetadata (path: string, existingProbe?: FfprobeData) {
136 const metadata = existingProbe || await ffprobePromise(path)
138 return new VideoFileMetadata(metadata)
141 async function getVideoStreamBitrate (path: string, existingProbe?: FfprobeData): Promise<number> {
142 const metadata = await buildFileMetadata(path, existingProbe)
144 let bitrate = metadata.format.bit_rate as number
145 if (bitrate && !isNaN(bitrate)) return bitrate
147 const videoStream = await getVideoStream(path, existingProbe)
148 if (!videoStream) return undefined
150 bitrate = videoStream?.bit_rate
151 if (bitrate && !isNaN(bitrate)) return bitrate
156 async function getVideoStreamDuration (path: string, existingProbe?: FfprobeData) {
157 const metadata = await buildFileMetadata(path, existingProbe)
159 return Math.round(metadata.format.duration)
162 async function getVideoStream (path: string, existingProbe?: FfprobeData) {
163 const metadata = await buildFileMetadata(path, existingProbe)
165 return metadata.streams.find(s => s.codec_type === 'video')
168 // ---------------------------------------------------------------------------
171 getVideoStreamDimensionsInfo,
175 getVideoStreamDuration,
180 getVideoStreamBitrate,