如何将PNG格式的图像转换为GIF格式
How to Convert an image in PNG format to GIF Format
我需要在 JavaScript 或 C# 中将 PNG 图片 Base 64 转换为 GIF Base 64 格式。
如果您可以将 ffmpeg 与 nodejs 一起使用,您可以在 node.js 中生成一个 ffmpeg 进程,这将允许您将 apng/animated-webp 转换为动画 gif。虽然这个解决方案不是纯粹的 node.js 解决方案,但它可以与 node.js 结合并用于 node.js 可以运行的任何环境。
[编辑]
根据this ffmpeg ticket的状态,您无法使用ffmpeg解码动画WEBP图像。
使用 ffmpeg 可以将 apng 文件转换为 .gif 文件。
我提供了一个示例脚本,它假设您已经在 PATH 环境变量中安装和配置了 ffmpeg。
这个函数的用法很简单。只需将“/path/to/file”替换为您的文件所在的位置(即“./input.apng”),它将在同一目录中输出一个名为:“input.apng.gif”的文件。
var gifLocation = await convertToGif("/path/to/file")
.catch((err) =>
{
console.error("[Error]", err);
});
if (gifLocation)
console.log("Location:", gifLocation);
这是一个工作脚本(没有文件路径)
const { exec } = require('child_process');
const fs = require("fs");
async function convertToGif(inputFile)
{
return new Promise(function(resolve, reject)
{
if (!fs.existsSync(inputFile))
{
console.error("[ffmpeg] File doesn't exist");
reject(false);
return;
}
const ls = exec(`ffmpeg -i "${inputFile}" -f gif "${inputFile}.gif"`, function (error, stdout, stderr)
{
if (error)
{
console.log(error.stack);
console.log('[ffmpeg] Error code: '+error.code);
console.log('[ffmpeg] Signal received: '+error.signal);
}
/*console.log('[ffmpeg] Child Process STDOUT: '+stdout);
console.log('[ffmpeg] Child Process STDERR: '+stderr);*/
});
ls.on('exit', function (code)
{
//console.log('[ffmpeg] Child process exited with exit code ' + code);
if (code != 0)
{
reject(false);
}
else
{
resolve(`${inputFile}.gif`);
}
});
});
}
async function run()
{
var gifLocation = await convertToGif("/path/to/file")
.catch((err) =>
{
console.error("[Error]", err);
});
if (gifLocation)
console.log("Location:", gifLocation);
}
run();
我需要在 JavaScript 或 C# 中将 PNG 图片 Base 64 转换为 GIF Base 64 格式。
如果您可以将 ffmpeg 与 nodejs 一起使用,您可以在 node.js 中生成一个 ffmpeg 进程,这将允许您将 apng/animated-webp 转换为动画 gif。虽然这个解决方案不是纯粹的 node.js 解决方案,但它可以与 node.js 结合并用于 node.js 可以运行的任何环境。
[编辑]
根据this ffmpeg ticket的状态,您无法使用ffmpeg解码动画WEBP图像。
使用 ffmpeg 可以将 apng 文件转换为 .gif 文件。
我提供了一个示例脚本,它假设您已经在 PATH 环境变量中安装和配置了 ffmpeg。
这个函数的用法很简单。只需将“/path/to/file”替换为您的文件所在的位置(即“./input.apng”),它将在同一目录中输出一个名为:“input.apng.gif”的文件。
var gifLocation = await convertToGif("/path/to/file")
.catch((err) =>
{
console.error("[Error]", err);
});
if (gifLocation)
console.log("Location:", gifLocation);
这是一个工作脚本(没有文件路径)
const { exec } = require('child_process');
const fs = require("fs");
async function convertToGif(inputFile)
{
return new Promise(function(resolve, reject)
{
if (!fs.existsSync(inputFile))
{
console.error("[ffmpeg] File doesn't exist");
reject(false);
return;
}
const ls = exec(`ffmpeg -i "${inputFile}" -f gif "${inputFile}.gif"`, function (error, stdout, stderr)
{
if (error)
{
console.log(error.stack);
console.log('[ffmpeg] Error code: '+error.code);
console.log('[ffmpeg] Signal received: '+error.signal);
}
/*console.log('[ffmpeg] Child Process STDOUT: '+stdout);
console.log('[ffmpeg] Child Process STDERR: '+stderr);*/
});
ls.on('exit', function (code)
{
//console.log('[ffmpeg] Child process exited with exit code ' + code);
if (code != 0)
{
reject(false);
}
else
{
resolve(`${inputFile}.gif`);
}
});
});
}
async function run()
{
var gifLocation = await convertToGif("/path/to/file")
.catch((err) =>
{
console.error("[Error]", err);
});
if (gifLocation)
console.log("Location:", gifLocation);
}
run();