-
-function computeResolutionsToTranscode (videoFileHeight: number) {
- const resolutionsEnabled: number[] = []
- const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
-
- // Put in the order we want to proceed jobs
- const resolutions = [
- VideoResolution.H_NOVIDEO,
- VideoResolution.H_480P,
- VideoResolution.H_360P,
- VideoResolution.H_720P,
- VideoResolution.H_240P,
- VideoResolution.H_1080P,
- VideoResolution.H_4K
- ]
-
- for (const resolution of resolutions) {
- if (configResolutions[ resolution + 'p' ] === true && videoFileHeight > resolution) {
- resolutionsEnabled.push(resolution)
- }
- }
-
- return resolutionsEnabled
-}
-
-async function getVideoStreamSize (path: string) {
- const videoStream = await getVideoStreamFromFile(path)
-
- return videoStream === null
- ? { width: 0, height: 0 }
- : { width: videoStream.width, height: videoStream.height }
-}
-
-async function getVideoStreamCodec (path: string) {
- const videoStream = await getVideoStreamFromFile(path)
-
- if (!videoStream) return ''
-
- const videoCodec = videoStream.codec_tag_string
-
- const baseProfileMatrix = {
- 'High': '6400',
- 'Main': '4D40',
- 'Baseline': '42E0'
- }
-
- let baseProfile = baseProfileMatrix[videoStream.profile]
- if (!baseProfile) {
- logger.warn('Cannot get video profile codec of %s.', path, { videoStream })
- baseProfile = baseProfileMatrix['High'] // Fallback
- }
-
- const level = videoStream.level.toString(16)
-
- return `${videoCodec}.${baseProfile}${level}`
-}
-
-async function getAudioStreamCodec (path: string) {
- const { audioStream } = await audio.get(path)
-
- if (!audioStream) return ''
-
- const audioCodec = audioStream.codec_name
- if (audioCodec === 'aac') return 'mp4a.40.2'
-
- logger.warn('Cannot get audio codec of %s.', path, { audioStream })
-
- return 'mp4a.40.2' // Fallback
-}
-
-async function getVideoFileResolution (path: string) {
- const size = await getVideoStreamSize(path)
-
- return {
- videoFileResolution: Math.min(size.height, size.width),
- isPortraitMode: size.height > size.width
- }
-}
-
-async function getVideoFileFPS (path: string) {
- const videoStream = await getVideoStreamFromFile(path)
- if (videoStream === null) return 0
-
- for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) {
- const valuesText: string = videoStream[ key ]
- if (!valuesText) continue
-
- const [ frames, seconds ] = valuesText.split('/')
- if (!frames || !seconds) continue
-
- const result = parseInt(frames, 10) / parseInt(seconds, 10)
- if (result > 0) return Math.round(result)
- }
-
- return 0
-}
-
-async function getVideoFileBitrate (path: string) {
- return new Promise<number>((res, rej) => {
- ffmpeg.ffprobe(path, (err, metadata) => {
- if (err) return rej(err)
-
- return res(metadata.format.bit_rate)
- })
- })
-}
-
-function getDurationFromVideoFile (path: string) {
- return new Promise<number>((res, rej) => {
- ffmpeg.ffprobe(path, (err, metadata) => {
- if (err) return rej(err)
-
- return res(Math.floor(metadata.format.duration))
- })
- })
-}