File size: 1,708 Bytes
046b271 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
const fs = require('fs');
const path = require('path');
const { tmpdir } = require('os');
const Crypto = require('crypto');
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const ffmpeg = require('fluent-ffmpeg');
// Set ffmpeg path
ffmpeg.setFfmpegPath(ffmpegPath);
/**
* Converts a video or GIF buffer to a WebP sticker format.
* @param {Buffer} videoBuffer - The video or GIF buffer to convert.
* @returns {Promise<Buffer>} - The converted WebP sticker buffer.
*/
async function videoToWebp(videoBuffer) {
const outputPath = path.join(
tmpdir(),
Crypto.randomBytes(6).readUIntLE(0, 6).toString(36) + '.webp'
);
const inputPath = path.join(
tmpdir(),
Crypto.randomBytes(6).readUIntLE(0, 6).toString(36) + '.mp4'
);
// Save the video buffer to a file
fs.writeFileSync(inputPath, videoBuffer);
await new Promise((resolve, reject) => {
ffmpeg(inputPath)
.on('error', reject)
.on('end', () => resolve(true))
.addOutputOptions([
'-vcodec', 'libwebp',
'-vf', "scale='min(320,iw)':min'(320,ih)':force_original_aspect_ratio=decrease,fps=15,pad=320:320:-1:-1:color=white@0.0,split [a][b];[a] palettegen=reserve_transparent=on:transparency_color=ffffff [p];[b][p] paletteuse",
'-loop', '0', // Loop forever
'-ss', '00:00:00', // Start time (optional)
'-t', '00:00:05', // Duration (optional)
'-preset', 'default',
'-an', // No audio
'-vsync', '0'
])
.toFormat('webp')
.save(outputPath);
});
const webpBuffer = fs.readFileSync(outputPath);
fs.unlinkSync(outputPath);
fs.unlinkSync(inputPath);
return webpBuffer;
}
module.exports = {
videoToWebp
};
|