用户正在我的网站上尝试将图像转换为 SVG 图像,下面我给出了该网站的链接,以及有关错误和处理媒体文件转换的代码的更多详细信息。
您可以在这里查看实时网站。
重现错误的步骤。
抛出错误。
下面是转换用户输入并根据用户偏好提供输入的代码。
// imports
import { createCanvas, loadImage } from "canvas";
import { Action } from "@/types";
import { FFmpeg } from "@ffmpeg/ffmpeg";
import { fetchFile } from "@ffmpeg/util";
function getFileExtension(file_name: string) {
const regex = /(?:\.([^.]+))?$/; // Matches the last dot and everything after it
const match = regex.exec(file_name);
if (match && match[1]) {
return match[1];
}
return ""; // No file extension found
}
function removeFileExtension(file_name: string) {
const lastDotIndex = file_name.lastIndexOf(".");
if (lastDotIndex !== -1) {
return file_name.slice(0, lastDotIndex);
}
return file_name; // No file extension found
}
export default async function convert(
ffmpeg: FFmpeg,
action: Action
): Promise<any> {
const { file, to, file_name, file_type } = action;
const input = getFileExtension(file_name);
const output = removeFileExtension(file_name) + "." + to;
ffmpeg.writeFile(input, await fetchFile(file));
// FFMPEG COMMANDS
let ffmpeg_cmd: any = [];
if (to === "svg") {
ffmpeg_cmd = [
"-i",
input,
"-vf",
"scale=trunc(iw/2)*2:trunc(ih/2)*2",
"-c:v",
"libvpx-vp9",
"-crf",
"30",
"-b:v",
"1M",
"-c:a",
"libopus",
"-b:a",
"128k",
output,
];
} else if (to === "3gp") {
ffmpeg_cmd = [
"-i",
input,
"-r",
"20",
"-s",
"352x288",
"-vb",
"400k",
"-acodec",
"aac",
"-strict",
"experimental",
"-ac",
"1",
"-ar",
"8000",
"-ab",
"24k",
output,
];
} else {
ffmpeg_cmd = ["-i", input, output];
}
// execute cmd
await ffmpeg.exec(ffmpeg_cmd);
const data = (await ffmpeg.readFile(output)) as any;
const blob = new Blob([data], { type: file_type.split("/")[0] });
const url = URL.createObjectURL(blob);
return { url, output };
}
感谢帮助,谢谢
据我所知,您无法使用 FFmpeg 将光栅图形(png)转换为矢量图形(svg)。
如果这确实是您想要做的,您可能想要类似 potrace 的东西。