]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/ffmpeg-utils.ts
Don't fail on upload if we cannot generate thumbnail
[github/Chocobozzz/PeerTube.git] / server / helpers / ffmpeg-utils.ts
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'
8
9 async function getVideoFileHeight (path: string) {
10 const videoStream = await getVideoFileStream(path)
11 return videoStream.height
12 }
13
14 async function getVideoFileFPS (path: string) {
15 const videoStream = await getVideoFileStream(path)
16
17 for (const key of [ 'r_frame_rate' , 'avg_frame_rate' ]) {
18 const valuesText: string = videoStream[key]
19 if (!valuesText) continue
20
21 const [ frames, seconds ] = valuesText.split('/')
22 if (!frames || !seconds) continue
23
24 const result = parseInt(frames, 10) / parseInt(seconds, 10)
25 if (result > 0) return result
26 }
27
28 return 0
29 }
30
31 function getDurationFromVideoFile (path: string) {
32 return new Promise<number>((res, rej) => {
33 ffmpeg.ffprobe(path, (err, metadata) => {
34 if (err) return rej(err)
35
36 return res(Math.floor(metadata.format.duration))
37 })
38 })
39 }
40
41 async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
42 const pendingImageName = 'pending-' + imageName
43
44 const options = {
45 filename: pendingImageName,
46 count: 1,
47 folder
48 }
49
50 const pendingImagePath = join(folder, pendingImageName)
51
52 try {
53 await new Promise<string>((res, rej) => {
54 ffmpeg(fromPath)
55 .on('error', rej)
56 .on('end', () => res(imageName))
57 .thumbnail(options)
58 })
59
60 const destination = join(folder, imageName)
61 await processImage({ path: pendingImagePath }, destination, size)
62 } catch (err) {
63 logger.error('Cannot generate image from video %s.', fromPath, err)
64
65 try {
66 await unlinkPromise(pendingImagePath)
67 } catch (err) {
68 logger.debug('Cannot remove pending image path after generation error.', err)
69 }
70 }
71 }
72
73 type TranscodeOptions = {
74 inputPath: string
75 outputPath: string
76 resolution?: VideoResolution
77 }
78
79 function transcode (options: TranscodeOptions) {
80 return new Promise<void>(async (res, rej) => {
81 const fps = await getVideoFileFPS(options.inputPath)
82
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')
89
90 if (fps > MAX_VIDEO_TRANSCODING_FPS) command = command.withFPS(MAX_VIDEO_TRANSCODING_FPS)
91
92 if (options.resolution !== undefined) {
93 const size = `?x${options.resolution}` // '?x720' for example
94 command = command.size(size)
95 }
96
97 command.on('error', rej)
98 .on('end', res)
99 .run()
100 })
101 }
102
103 // ---------------------------------------------------------------------------
104
105 export {
106 getVideoFileHeight,
107 getDurationFromVideoFile,
108 generateImageFromVideoFile,
109 transcode,
110 getVideoFileFPS
111 }
112
113 // ---------------------------------------------------------------------------
114
115 function getVideoFileStream (path: string) {
116 return new Promise<any>((res, rej) => {
117 ffmpeg.ffprobe(path, (err, metadata) => {
118 if (err) return rej(err)
119
120 const videoStream = metadata.streams.find(s => s.codec_type === 'video')
121 if (!videoStream) throw new Error('Cannot find video stream of ' + path)
122
123 return res(videoStream)
124 })
125 })
126 }