1 import * as ffmpeg from 'fluent-ffmpeg'
2 import { VideoResolution } from '../../shared/models/videos'
3 import { CONFIG } from '../initializers'
5 function getVideoFileHeight (path: string) {
6 return new Promise<number>((res, rej) => {
7 ffmpeg.ffprobe(path, (err, metadata) => {
8 if (err) return rej(err)
10 const videoStream = metadata.streams.find(s => s.codec_type === 'video')
11 return res(videoStream.height)
16 function getDurationFromVideoFile (path: string) {
17 return new Promise<number>((res, rej) => {
18 ffmpeg.ffprobe(path, (err, metadata) => {
19 if (err) return rej(err)
21 return res(Math.floor(metadata.format.duration))
26 function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: string) {
33 if (size !== undefined) {
34 options['size'] = size
37 return new Promise<string>((res, rej) => {
40 .on('end', () => res(imageName))
45 type TranscodeOptions = {
48 resolution?: VideoResolution
51 function transcode (options: TranscodeOptions) {
52 return new Promise<void>((res, rej) => {
53 let command = ffmpeg(options.inputPath)
54 .output(options.outputPath)
55 .videoCodec('libx264')
56 .outputOption('-threads ' + CONFIG.TRANSCODING.THREADS)
57 .outputOption('-movflags faststart')
58 // .outputOption('-crf 18')
60 if (options.resolution !== undefined) {
61 const size = `?x${options.resolution}` // '?x720' for example
62 command = command.size(size)
65 command.on('error', rej)
71 // ---------------------------------------------------------------------------
75 getDurationFromVideoFile,
76 generateImageFromVideoFile,