|
|
const axios = require('axios'); |
|
|
const fs = require('fs'); |
|
|
const path = require('path'); |
|
|
const { tmpdir } = require('os'); |
|
|
const Crypto = require('crypto'); |
|
|
const ffmpeg = require('fluent-ffmpeg'); |
|
|
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path; |
|
|
|
|
|
ffmpeg.setFfmpegPath(ffmpegPath); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function fetchGif(url) { |
|
|
try { |
|
|
const response = await axios.get(url, { responseType: 'arraybuffer' }); |
|
|
return response.data; |
|
|
} catch (error) { |
|
|
console.error("β Error fetching GIF:", error); |
|
|
throw new Error("Could not fetch GIF."); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function gifToVideo(gifBuffer) { |
|
|
const filename = Crypto.randomBytes(6).toString('hex'); |
|
|
const gifPath = path.join(tmpdir(), `${filename}.gif`); |
|
|
const mp4Path = path.join(tmpdir(), `${filename}.mp4`); |
|
|
|
|
|
fs.writeFileSync(gifPath, gifBuffer); |
|
|
|
|
|
await new Promise((resolve, reject) => { |
|
|
ffmpeg(gifPath) |
|
|
.outputOptions([ |
|
|
"-movflags faststart", |
|
|
"-pix_fmt yuv420p", |
|
|
"-vf scale=trunc(iw/2)*2:trunc(ih/2)*2" |
|
|
]) |
|
|
.on("error", (err) => { |
|
|
console.error("β ffmpeg conversion error:", err); |
|
|
reject(new Error("Could not process GIF to video.")); |
|
|
}) |
|
|
.on("end", resolve) |
|
|
.save(mp4Path); |
|
|
}); |
|
|
|
|
|
const videoBuffer = fs.readFileSync(mp4Path); |
|
|
fs.unlinkSync(gifPath); |
|
|
fs.unlinkSync(mp4Path); |
|
|
|
|
|
return videoBuffer; |
|
|
} |
|
|
|
|
|
module.exports = { fetchGif, gifToVideo }; |
|
|
|
|
|
|