1 import * as ffmpeg from 'fluent-ffmpeg'
2 import { join } from 'path'
3 import { VideoResolution } from '../../shared/models/videos'
4 import { CONFIG, MAX_VIDEO_TRANSCODING_FPS } from '../initializers'
5 import { unlinkPromise } from './core-utils'
6 import { processImage } from './image-utils'
7 import { logger } from './logger'
9 async function getVideoFileHeight (path: string) {
10 const videoStream = await getVideoFileStream(path)
11 return videoStream.height
14 async function getVideoFileFPS (path: string) {
15 const videoStream = await getVideoFileStream(path)
17 for (const key of [ 'r_frame_rate' , 'avg_frame_rate' ]) {
18 const valuesText: string = videoStream[key]
19 if (!valuesText) continue
21 const [ frames, seconds ] = valuesText.split('/')
22 if (!frames || !seconds) continue
24 const result = parseInt(frames, 10) / parseInt(seconds, 10)
25 if (result > 0) return result
31 function getDurationFromVideoFile (path: string) {
32 return new Promise<number>((res, rej) => {
33 ffmpeg.ffprobe(path, (err, metadata) => {
34 if (err) return rej(err)
36 return res(Math.floor(metadata.format.duration))
41 async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
42 const pendingImageName = 'pending-' + imageName
45 filename: pendingImageName,
50 const pendingImagePath = join(folder, pendingImageName)
53 await new Promise<string>((res, rej) => {
56 .on('end', () => res(imageName))
60 const destination = join(folder, imageName)
61 await processImage({ path: pendingImagePath }, destination, size)
63 logger.error('Cannot generate image from video %s.', fromPath, err)
66 await unlinkPromise(pendingImagePath)
68 logger.debug('Cannot remove pending image path after generation error.', err)
73 type TranscodeOptions = {
76 resolution?: VideoResolution
79 function transcode (options: TranscodeOptions) {
80 return new Promise<void>(async (res, rej) => {
81 const fps = await getVideoFileFPS(options.inputPath)
83 let command = ffmpeg(options.inputPath)
84 .output(options.outputPath)
85 .videoCodec('libx264')
86 .outputOption('-threads ' + CONFIG.TRANSCODING.THREADS)
87 .outputOption('-movflags faststart')
88 // .outputOption('-crf 18')
90 if (fps > MAX_VIDEO_TRANSCODING_FPS) command = command.withFPS(MAX_VIDEO_TRANSCODING_FPS)
92 if (options.resolution !== undefined) {
93 const size = `?x${options.resolution}` // '?x720' for example
94 command = command.size(size)
97 command.on('error', rej)
103 // ---------------------------------------------------------------------------
107 getDurationFromVideoFile,
108 generateImageFromVideoFile,
113 // ---------------------------------------------------------------------------
115 function getVideoFileStream (path: string) {
116 return new Promise<any>((res, rej) => {
117 ffmpeg.ffprobe(path, (err, metadata) => {
118 if (err) return rej(err)
120 const videoStream = metadata.streams.find(s => s.codec_type === 'video')
121 if (!videoStream) throw new Error('Cannot find video stream of ' + path)
123 return res(videoStream)