]>
Commit | Line | Data |
---|---|---|
1 | import * as ffmpeg from 'fluent-ffmpeg' | |
2 | import { join } from 'path' | |
3 | import { VideoResolution } from '../../shared/models/videos' | |
4 | import { CONFIG, 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 getVideoFileResolution (path: string) { | |
10 | const videoStream = await getVideoFileStream(path) | |
11 | ||
12 | return { | |
13 | videoFileResolution: Math.min(videoStream.height, videoStream.width), | |
14 | isPortraitMode: videoStream.height > videoStream.width | |
15 | } | |
16 | } | |
17 | ||
18 | async function getVideoFileFPS (path: string) { | |
19 | const videoStream = await getVideoFileStream(path) | |
20 | ||
21 | for (const key of [ 'r_frame_rate' , 'avg_frame_rate' ]) { | |
22 | const valuesText: string = videoStream[key] | |
23 | if (!valuesText) continue | |
24 | ||
25 | const [ frames, seconds ] = valuesText.split('/') | |
26 | if (!frames || !seconds) continue | |
27 | ||
28 | const result = parseInt(frames, 10) / parseInt(seconds, 10) | |
29 | if (result > 0) return result | |
30 | } | |
31 | ||
32 | return 0 | |
33 | } | |
34 | ||
35 | function getDurationFromVideoFile (path: string) { | |
36 | return new Promise<number>((res, rej) => { | |
37 | ffmpeg.ffprobe(path, (err, metadata) => { | |
38 | if (err) return rej(err) | |
39 | ||
40 | return res(Math.floor(metadata.format.duration)) | |
41 | }) | |
42 | }) | |
43 | } | |
44 | ||
45 | async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) { | |
46 | const pendingImageName = 'pending-' + imageName | |
47 | ||
48 | const options = { | |
49 | filename: pendingImageName, | |
50 | count: 1, | |
51 | folder | |
52 | } | |
53 | ||
54 | const pendingImagePath = join(folder, pendingImageName) | |
55 | ||
56 | try { | |
57 | await new Promise<string>((res, rej) => { | |
58 | ffmpeg(fromPath) | |
59 | .on('error', rej) | |
60 | .on('end', () => res(imageName)) | |
61 | .thumbnail(options) | |
62 | }) | |
63 | ||
64 | const destination = join(folder, imageName) | |
65 | await processImage({ path: pendingImagePath }, destination, size) | |
66 | } catch (err) { | |
67 | logger.error('Cannot generate image from video %s.', fromPath, { err }) | |
68 | ||
69 | try { | |
70 | await unlinkPromise(pendingImagePath) | |
71 | } catch (err) { | |
72 | logger.debug('Cannot remove pending image path after generation error.', { err }) | |
73 | } | |
74 | } | |
75 | } | |
76 | ||
77 | type TranscodeOptions = { | |
78 | inputPath: string | |
79 | outputPath: string | |
80 | resolution?: VideoResolution | |
81 | isPortraitMode?: boolean | |
82 | } | |
83 | ||
84 | function transcode (options: TranscodeOptions) { | |
85 | return new Promise<void>(async (res, rej) => { | |
86 | const fps = await getVideoFileFPS(options.inputPath) | |
87 | ||
88 | let command = ffmpeg(options.inputPath) | |
89 | .output(options.outputPath) | |
90 | .videoCodec('libx264') | |
91 | .outputOption('-threads ' + CONFIG.TRANSCODING.THREADS) | |
92 | .outputOption('-movflags faststart') | |
93 | // .outputOption('-crf 18') | |
94 | ||
95 | // Our player has some FPS limits | |
96 | if (fps > VIDEO_TRANSCODING_FPS.MAX) command = command.withFPS(VIDEO_TRANSCODING_FPS.MAX) | |
97 | else if (fps < VIDEO_TRANSCODING_FPS.MIN) command = command.withFPS(VIDEO_TRANSCODING_FPS.MIN) | |
98 | ||
99 | if (options.resolution !== undefined) { | |
100 | // '?x720' or '720x?' for example | |
101 | const size = options.isPortraitMode === true ? `${options.resolution}x?` : `?x${options.resolution}` | |
102 | command = command.size(size) | |
103 | } | |
104 | ||
105 | command | |
106 | .on('error', (err, stdout, stderr) => { | |
107 | logger.error('Error in transcoding job.', { stdout, stderr }) | |
108 | return rej(err) | |
109 | }) | |
110 | .on('end', res) | |
111 | .run() | |
112 | }) | |
113 | } | |
114 | ||
115 | // --------------------------------------------------------------------------- | |
116 | ||
117 | export { | |
118 | getVideoFileResolution, | |
119 | getDurationFromVideoFile, | |
120 | generateImageFromVideoFile, | |
121 | transcode, | |
122 | getVideoFileFPS | |
123 | } | |
124 | ||
125 | // --------------------------------------------------------------------------- | |
126 | ||
127 | function getVideoFileStream (path: string) { | |
128 | return new Promise<any>((res, rej) => { | |
129 | ffmpeg.ffprobe(path, (err, metadata) => { | |
130 | if (err) return rej(err) | |
131 | ||
132 | const videoStream = metadata.streams.find(s => s.codec_type === 'video') | |
133 | if (!videoStream) throw new Error('Cannot find video stream of ' + path) | |
134 | ||
135 | return res(videoStream) | |
136 | }) | |
137 | }) | |
138 | } |