Users are trying to convert images to SVG image on my website, Below I’ve given a link to the site, more detail on error and code that handles the conversion of media files.
You can check out the live site Here.
Steps to reproduce the error.
- Upload the png file,
- Select SVG as output file
- Click convert.
It is throwing error.
Below is the code for that converts users input and gives input based on user preference.
// 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 };
}
Help appreciated, Thank You