1 import * as ffmpeg from 'fluent-ffmpeg'
3 import { CONFIG } from '../initializers'
4 import { VideoResolution } from '../../shared/models/videos/video-resolution.enum'
6 function getVideoFileHeight (path: string) {
7 return new Promise<number>((res, rej) => {
8 ffmpeg.ffprobe(path, (err, metadata) => {
9 if (err) return rej(err)
11 const videoStream = metadata.streams.find(s => s.codec_type === 'video')
12 return res(videoStream.height)
17 function getDurationFromVideoFile (path: string) {
18 return new Promise<number>((res, rej) => {
19 ffmpeg.ffprobe(path, (err, metadata) => {
20 if (err) return rej(err)
22 return res(Math.floor(metadata.format.duration))
27 function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: string) {
34 if (size !== undefined) {
35 options['size'] = size
38 return new Promise<string>((res, rej) => {
41 .on('end', () => res(imageName))
46 type TranscodeOptions = {
49 resolution?: VideoResolution
52 function transcode (options: TranscodeOptions) {
53 return new Promise<void>((res, rej) => {
54 let command = ffmpeg(options.inputPath)
55 .output(options.outputPath)
56 .videoCodec('libx264')
57 .outputOption('-threads ' + CONFIG.TRANSCODING.THREADS)
58 .outputOption('-movflags faststart')
59 // .outputOption('-crf 18')
61 if (options.resolution !== undefined) {
62 const size = `?x${options.resolution}` // '?x720' for example
63 command = command.size(size)
66 command.on('error', rej)
72 // ---------------------------------------------------------------------------
76 getDurationFromVideoFile,
77 generateImageFromVideoFile,