1 import * as ffmpeg from 'fluent-ffmpeg'
2 import { VideoFileMetadata } from '@shared/models/videos/video-file-metadata'
3 import { getMaxBitrate, VideoResolution } from '../../shared/models/videos'
4 import { CONFIG } from '../initializers/config'
5 import { VIDEO_TRANSCODING_FPS } from '../initializers/constants'
6 import { logger } from './logger'
10 * Helpers to run ffprobe and extract data from the JSON output
14 function ffprobePromise (path: string) {
15 return new Promise<ffmpeg.FfprobeData>((res, rej) => {
16 ffmpeg.ffprobe(path, (err, data) => {
17 if (err) return rej(err)
24 async function getAudioStream (videoPath: string, existingProbe?: ffmpeg.FfprobeData) {
25 // without position, ffprobe considers the last input only
26 // we make it consider the first input only
27 // if you pass a file path to pos, then ffprobe acts on that file directly
28 const data = existingProbe || await ffprobePromise(videoPath)
30 if (Array.isArray(data.streams)) {
31 const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio')
35 absolutePath: data.format.filename,
37 bitrate: parseInt(audioStream['bit_rate'] + '', 10)
42 return { absolutePath: data.format.filename }
45 function getMaxAudioBitrate (type: 'aac' | 'mp3' | string, bitrate: number) {
46 const maxKBitrate = 384
47 const kToBits = (kbits: number) => kbits * 1000
49 // If we did not manage to get the bitrate, use an average value
50 if (!bitrate) return 256
54 case bitrate > kToBits(maxKBitrate):
58 return -1 // we interpret it as a signal to copy the audio stream as is
63 a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac.
64 That's why, when using aac, we can go to lower kbit/sec. The equivalences
65 made here are not made to be accurate, especially with good mp3 encoders.
68 case bitrate <= kToBits(192):
71 case bitrate <= kToBits(384):
79 async function getVideoStreamSize (path: string, existingProbe?: ffmpeg.FfprobeData) {
80 const videoStream = await getVideoStreamFromFile(path, existingProbe)
82 return videoStream === null
83 ? { width: 0, height: 0 }
84 : { width: videoStream.width, height: videoStream.height }
87 async function getVideoStreamCodec (path: string) {
88 const videoStream = await getVideoStreamFromFile(path)
90 if (!videoStream) return ''
92 const videoCodec = videoStream.codec_tag_string
94 const baseProfileMatrix = {
100 let baseProfile = baseProfileMatrix[videoStream.profile]
102 logger.warn('Cannot get video profile codec of %s.', path, { videoStream })
103 baseProfile = baseProfileMatrix['High'] // Fallback
106 let level = videoStream.level.toString(16)
107 if (level.length === 1) level = `0${level}`
109 return `${videoCodec}.${baseProfile}${level}`
112 async function getAudioStreamCodec (path: string, existingProbe?: ffmpeg.FfprobeData) {
113 const { audioStream } = await getAudioStream(path, existingProbe)
115 if (!audioStream) return ''
117 const audioCodec = audioStream.codec_name
118 if (audioCodec === 'aac') return 'mp4a.40.2'
120 logger.warn('Cannot get audio codec of %s.', path, { audioStream })
122 return 'mp4a.40.2' // Fallback
125 async function getVideoFileResolution (path: string, existingProbe?: ffmpeg.FfprobeData) {
126 const size = await getVideoStreamSize(path, existingProbe)
129 videoFileResolution: Math.min(size.height, size.width),
130 isPortraitMode: size.height > size.width
134 async function getVideoFileFPS (path: string, existingProbe?: ffmpeg.FfprobeData) {
135 const videoStream = await getVideoStreamFromFile(path, existingProbe)
136 if (videoStream === null) return 0
138 for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) {
139 const valuesText: string = videoStream[key]
140 if (!valuesText) continue
142 const [ frames, seconds ] = valuesText.split('/')
143 if (!frames || !seconds) continue
145 const result = parseInt(frames, 10) / parseInt(seconds, 10)
146 if (result > 0) return Math.round(result)
152 async function getMetadataFromFile (path: string, existingProbe?: ffmpeg.FfprobeData) {
153 const metadata = existingProbe || await ffprobePromise(path)
155 return new VideoFileMetadata(metadata)
158 async function getVideoFileBitrate (path: string, existingProbe?: ffmpeg.FfprobeData) {
159 const metadata = await getMetadataFromFile(path, existingProbe)
161 return metadata.format.bit_rate as number
164 async function getDurationFromVideoFile (path: string, existingProbe?: ffmpeg.FfprobeData) {
165 const metadata = await getMetadataFromFile(path, existingProbe)
167 return Math.round(metadata.format.duration)
170 async function getVideoStreamFromFile (path: string, existingProbe?: ffmpeg.FfprobeData) {
171 const metadata = await getMetadataFromFile(path, existingProbe)
173 return metadata.streams.find(s => s.codec_type === 'video') || null
176 function computeResolutionsToTranscode (videoFileResolution: number, type: 'vod' | 'live') {
177 const configResolutions = type === 'vod'
178 ? CONFIG.TRANSCODING.RESOLUTIONS
179 : CONFIG.LIVE.TRANSCODING.RESOLUTIONS
181 const resolutionsEnabled: number[] = []
183 // Put in the order we want to proceed jobs
184 const resolutions = [
185 VideoResolution.H_NOVIDEO,
186 VideoResolution.H_480P,
187 VideoResolution.H_360P,
188 VideoResolution.H_720P,
189 VideoResolution.H_240P,
190 VideoResolution.H_1080P,
194 for (const resolution of resolutions) {
195 if (configResolutions[resolution + 'p'] === true && videoFileResolution > resolution) {
196 resolutionsEnabled.push(resolution)
200 return resolutionsEnabled
203 async function canDoQuickTranscode (path: string): Promise<boolean> {
204 const probe = await ffprobePromise(path)
206 return await canDoQuickVideoTranscode(path, probe) &&
207 await canDoQuickAudioTranscode(path, probe)
210 async function canDoQuickVideoTranscode (path: string, probe?: ffmpeg.FfprobeData): Promise<boolean> {
211 const videoStream = await getVideoStreamFromFile(path, probe)
212 const fps = await getVideoFileFPS(path, probe)
213 const bitRate = await getVideoFileBitrate(path, probe)
214 const resolution = await getVideoFileResolution(path, probe)
216 // If ffprobe did not manage to guess the bitrate
217 if (!bitRate) return false
219 // check video params
220 if (videoStream == null) return false
221 if (videoStream['codec_name'] !== 'h264') return false
222 if (videoStream['pix_fmt'] !== 'yuv420p') return false
223 if (fps < VIDEO_TRANSCODING_FPS.MIN || fps > VIDEO_TRANSCODING_FPS.MAX) return false
224 if (bitRate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)) return false
229 async function canDoQuickAudioTranscode (path: string, probe?: ffmpeg.FfprobeData): Promise<boolean> {
230 const parsedAudio = await getAudioStream(path, probe)
232 if (!parsedAudio.audioStream) return true
234 if (parsedAudio.audioStream['codec_name'] !== 'aac') return false
236 const audioBitrate = parsedAudio.bitrate
237 if (!audioBitrate) return false
239 const maxAudioBitrate = getMaxAudioBitrate('aac', audioBitrate)
240 if (maxAudioBitrate !== -1 && audioBitrate > maxAudioBitrate) return false
245 function getClosestFramerateStandard (fps: number, type: 'HD_STANDARD' | 'STANDARD'): number {
246 return VIDEO_TRANSCODING_FPS[type].slice(0)
247 .sort((a, b) => fps % a - fps % b)[0]
250 function computeFPS (fpsArg: number, resolution: VideoResolution) {
254 // On small/medium resolutions, limit FPS
255 resolution !== undefined &&
256 resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN &&
257 fps > VIDEO_TRANSCODING_FPS.AVERAGE
259 // Get closest standard framerate by modulo: downsampling has to be done to a divisor of the nominal fps value
260 fps = getClosestFramerateStandard(fps, 'STANDARD')
264 if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = getClosestFramerateStandard(fps, 'HD_STANDARD')
265 else if (fps < VIDEO_TRANSCODING_FPS.MIN) fps = VIDEO_TRANSCODING_FPS.MIN
270 // ---------------------------------------------------------------------------
276 getVideoFileResolution,
279 getVideoStreamFromFile,
280 getDurationFromVideoFile,
285 getClosestFramerateStandard,
286 computeResolutionsToTranscode,
289 canDoQuickVideoTranscode,
290 canDoQuickAudioTranscode