]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/ffmpeg-utils.ts
Set port if not specified in webfinger
[github/Chocobozzz/PeerTube.git] / server / helpers / ffmpeg-utils.ts
1 import * as ffmpeg from 'fluent-ffmpeg'
2 import { VideoResolution } from '../../shared/models/videos'
3 import { CONFIG } from '../initializers'
4
5 function getVideoFileHeight (path: string) {
6 return new Promise<number>((res, rej) => {
7 ffmpeg.ffprobe(path, (err, metadata) => {
8 if (err) return rej(err)
9
10 const videoStream = metadata.streams.find(s => s.codec_type === 'video')
11 return res(videoStream.height)
12 })
13 })
14 }
15
16 function getDurationFromVideoFile (path: string) {
17 return new Promise<number>((res, rej) => {
18 ffmpeg.ffprobe(path, (err, metadata) => {
19 if (err) return rej(err)
20
21 return res(Math.floor(metadata.format.duration))
22 })
23 })
24 }
25
26 function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: string) {
27 const options = {
28 filename: imageName,
29 count: 1,
30 folder
31 }
32
33 if (size !== undefined) {
34 options['size'] = size
35 }
36
37 return new Promise<string>((res, rej) => {
38 ffmpeg(fromPath)
39 .on('error', rej)
40 .on('end', () => res(imageName))
41 .thumbnail(options)
42 })
43 }
44
45 type TranscodeOptions = {
46 inputPath: string
47 outputPath: string
48 resolution?: VideoResolution
49 }
50
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')
59
60 if (options.resolution !== undefined) {
61 const size = `?x${options.resolution}` // '?x720' for example
62 command = command.size(size)
63 }
64
65 command.on('error', rej)
66 .on('end', res)
67 .run()
68 })
69 }
70
71 // ---------------------------------------------------------------------------
72
73 export {
74 getVideoFileHeight,
75 getDurationFromVideoFile,
76 generateImageFromVideoFile,
77 transcode
78 }