diff options
author | Chocobozzz <me@florianbigard.com> | 2022-02-11 10:51:33 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2022-02-28 10:42:19 +0100 |
commit | c729caf6cc34630877a0e5a1bda1719384cd0c8a (patch) | |
tree | 1d2e13722e518c73d2c9e6f0969615e29d51cf8c /server/helpers/image-utils.ts | |
parent | a24bf4dc659cebb65d887862bf21d7a35e9ec791 (diff) | |
download | PeerTube-c729caf6cc34630877a0e5a1bda1719384cd0c8a.tar.gz PeerTube-c729caf6cc34630877a0e5a1bda1719384cd0c8a.tar.zst PeerTube-c729caf6cc34630877a0e5a1bda1719384cd0c8a.zip |
Add basic video editor support
Diffstat (limited to 'server/helpers/image-utils.ts')
-rw-r--r-- | server/helpers/image-utils.ts | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/server/helpers/image-utils.ts b/server/helpers/image-utils.ts index b174ae436..6e4a2b000 100644 --- a/server/helpers/image-utils.ts +++ b/server/helpers/image-utils.ts | |||
@@ -1,9 +1,12 @@ | |||
1 | import { copy, readFile, remove, rename } from 'fs-extra' | 1 | import { copy, readFile, remove, rename } from 'fs-extra' |
2 | import Jimp, { read } from 'jimp' | 2 | import Jimp, { read } from 'jimp' |
3 | import { join } from 'path' | ||
3 | import { getLowercaseExtension } from '@shared/core-utils' | 4 | import { getLowercaseExtension } from '@shared/core-utils' |
4 | import { buildUUID } from '@shared/extra-utils' | 5 | import { buildUUID } from '@shared/extra-utils' |
5 | import { convertWebPToJPG, processGIF } from './ffmpeg-utils' | 6 | import { convertWebPToJPG, generateThumbnailFromVideo, processGIF } from './ffmpeg/ffmpeg-images' |
6 | import { logger } from './logger' | 7 | import { logger, loggerTagsFactory } from './logger' |
8 | |||
9 | const lTags = loggerTagsFactory('image-utils') | ||
7 | 10 | ||
8 | function generateImageFilename (extension = '.jpg') { | 11 | function generateImageFilename (extension = '.jpg') { |
9 | return buildUUID() + extension | 12 | return buildUUID() + extension |
@@ -33,10 +36,31 @@ async function processImage ( | |||
33 | if (keepOriginal !== true) await remove(path) | 36 | if (keepOriginal !== true) await remove(path) |
34 | } | 37 | } |
35 | 38 | ||
39 | async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) { | ||
40 | const pendingImageName = 'pending-' + imageName | ||
41 | const pendingImagePath = join(folder, pendingImageName) | ||
42 | |||
43 | try { | ||
44 | await generateThumbnailFromVideo(fromPath, folder, imageName) | ||
45 | |||
46 | const destination = join(folder, imageName) | ||
47 | await processImage(pendingImagePath, destination, size) | ||
48 | } catch (err) { | ||
49 | logger.error('Cannot generate image from video %s.', fromPath, { err, ...lTags() }) | ||
50 | |||
51 | try { | ||
52 | await remove(pendingImagePath) | ||
53 | } catch (err) { | ||
54 | logger.debug('Cannot remove pending image path after generation error.', { err, ...lTags() }) | ||
55 | } | ||
56 | } | ||
57 | } | ||
58 | |||
36 | // --------------------------------------------------------------------------- | 59 | // --------------------------------------------------------------------------- |
37 | 60 | ||
38 | export { | 61 | export { |
39 | generateImageFilename, | 62 | generateImageFilename, |
63 | generateImageFromVideoFile, | ||
40 | processImage | 64 | processImage |
41 | } | 65 | } |
42 | 66 | ||