1 import * as Promise from 'bluebird'
2 import * as ffmpeg from 'fluent-ffmpeg'
4 import { CONFIG } from '../initializers'
5 import { VideoResolution } from '../../shared/models/videos/video-resolution.enum'
7 function getVideoFileHeight (path: string) {
8 return new Promise<number>((res, rej) => {
9 ffmpeg.ffprobe(path, (err, metadata) => {
10 if (err) return rej(err)
12 const videoStream = metadata.streams.find(s => s.codec_type === 'video')
13 return res(videoStream.height)
18 function getDurationFromVideoFile (path: string) {
19 return new Promise<number>((res, rej) => {
20 ffmpeg.ffprobe(path, (err, metadata) => {
21 if (err) return rej(err)
23 return res(Math.floor(metadata.format.duration))
28 function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: string) {
35 if (size !== undefined) {
36 options['size'] = size
39 return new Promise<string>((res, rej) => {
42 .on('end', () => res(imageName))
47 type TranscodeOptions = {
50 resolution?: VideoResolution
53 function transcode (options: TranscodeOptions) {
54 return new Promise<void>((res, rej) => {
55 let command = ffmpeg(options.inputPath)
56 .output(options.outputPath)
57 .videoCodec('libx264')
58 .outputOption('-threads ' + CONFIG.TRANSCODING.THREADS)
59 .outputOption('-movflags faststart')
60 // .outputOption('-crf 18')
62 if (options.resolution !== undefined) {
63 const size = `?x${options.resolution}` // '?x720' for example
64 command = command.size(size)
67 command.on('error', rej)
73 // ---------------------------------------------------------------------------
77 getDurationFromVideoFile,
78 generateImageFromVideoFile,