diff options
author | Chocobozzz <me@florianbigard.com> | 2020-11-25 09:50:12 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-11-25 10:07:51 +0100 |
commit | f619de0e435f7ac3abad2ec772397486358b56e7 (patch) | |
tree | 991040ca204e95a3322e02fdc9867a8f6b69c1e8 /server/helpers | |
parent | 6b67897e2eab96978daee40aeaf716835856d65d (diff) | |
download | PeerTube-f619de0e435f7ac3abad2ec772397486358b56e7.tar.gz PeerTube-f619de0e435f7ac3abad2ec772397486358b56e7.tar.zst PeerTube-f619de0e435f7ac3abad2ec772397486358b56e7.zip |
Add tests to gif resizer
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/ffmpeg-utils.ts | 33 | ||||
-rw-r--r-- | server/helpers/image-utils.ts | 34 |
2 files changed, 26 insertions, 41 deletions
diff --git a/server/helpers/ffmpeg-utils.ts b/server/helpers/ffmpeg-utils.ts index 69defccc4..9755dd67c 100644 --- a/server/helpers/ffmpeg-utils.ts +++ b/server/helpers/ffmpeg-utils.ts | |||
@@ -64,35 +64,14 @@ function convertWebPToJPG (path: string, destination: string): Promise<void> { | |||
64 | function processGIF ( | 64 | function processGIF ( |
65 | path: string, | 65 | path: string, |
66 | destination: string, | 66 | destination: string, |
67 | newSize: { width: number, height: number }, | 67 | newSize: { width: number, height: number } |
68 | keepOriginal = false | ||
69 | ): Promise<void> { | 68 | ): Promise<void> { |
70 | return new Promise<void>(async (res, rej) => { | 69 | const command = ffmpeg(path) |
71 | if (path === destination) { | 70 | .fps(20) |
72 | throw new Error('FFmpeg needs an input path different that the output path.') | 71 | .size(`${newSize.width}x${newSize.height}`) |
73 | } | 72 | .output(destination) |
74 | |||
75 | logger.debug('Processing gif %s to %s.', path, destination) | ||
76 | 73 | ||
77 | try { | 74 | return runCommand(command) |
78 | const command = ffmpeg(path) | ||
79 | .fps(20) | ||
80 | .size(`${newSize.width}x${newSize.height}`) | ||
81 | .output(destination) | ||
82 | |||
83 | command.on('error', (err, stdout, stderr) => { | ||
84 | logger.error('Error in ffmpeg gif resizing process.', { stdout, stderr }) | ||
85 | return rej(err) | ||
86 | }) | ||
87 | .on('end', async () => { | ||
88 | if (keepOriginal !== true) await remove(path) | ||
89 | res() | ||
90 | }) | ||
91 | .run() | ||
92 | } catch (err) { | ||
93 | return rej(err) | ||
94 | } | ||
95 | }) | ||
96 | } | 75 | } |
97 | 76 | ||
98 | async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) { | 77 | async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) { |
diff --git a/server/helpers/image-utils.ts b/server/helpers/image-utils.ts index fdf06e848..3ebf07305 100644 --- a/server/helpers/image-utils.ts +++ b/server/helpers/image-utils.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | import { extname } from 'path' | ||
2 | import { remove, rename } from 'fs-extra' | 1 | import { remove, rename } from 'fs-extra' |
2 | import { extname } from 'path' | ||
3 | import { convertWebPToJPG, processGIF } from './ffmpeg-utils' | 3 | import { convertWebPToJPG, processGIF } from './ffmpeg-utils' |
4 | import { logger } from './logger' | 4 | import { logger } from './logger' |
5 | 5 | ||
@@ -13,17 +13,31 @@ async function processImage ( | |||
13 | ) { | 13 | ) { |
14 | const extension = extname(path) | 14 | const extension = extname(path) |
15 | 15 | ||
16 | if (path === destination) { | ||
17 | throw new Error('Jimp/FFmpeg needs an input path different that the output path.') | ||
18 | } | ||
19 | |||
20 | logger.debug('Processing image %s to %s.', path, destination) | ||
21 | |||
16 | // Use FFmpeg to process GIF | 22 | // Use FFmpeg to process GIF |
17 | if (extension === '.gif') { | 23 | if (extension === '.gif') { |
18 | return processGIF(path, destination, newSize, keepOriginal) | 24 | await processGIF(path, destination, newSize) |
25 | } else { | ||
26 | await jimpProcessor(path, destination, newSize) | ||
19 | } | 27 | } |
20 | 28 | ||
21 | if (path === destination) { | 29 | if (keepOriginal !== true) await remove(path) |
22 | throw new Error('Jimp needs an input path different that the output path.') | 30 | } |
23 | } | ||
24 | 31 | ||
25 | logger.debug('Processing image %s to %s.', path, destination) | 32 | // --------------------------------------------------------------------------- |
26 | 33 | ||
34 | export { | ||
35 | processImage | ||
36 | } | ||
37 | |||
38 | // --------------------------------------------------------------------------- | ||
39 | |||
40 | async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }) { | ||
27 | let jimpInstance: any | 41 | let jimpInstance: any |
28 | 42 | ||
29 | try { | 43 | try { |
@@ -44,12 +58,4 @@ async function processImage ( | |||
44 | .resize(newSize.width, newSize.height) | 58 | .resize(newSize.width, newSize.height) |
45 | .quality(80) | 59 | .quality(80) |
46 | .writeAsync(destination) | 60 | .writeAsync(destination) |
47 | |||
48 | if (keepOriginal !== true) await remove(path) | ||
49 | } | ||
50 | |||
51 | // --------------------------------------------------------------------------- | ||
52 | |||
53 | export { | ||
54 | processImage | ||
55 | } | 61 | } |