mirror of
https://github.com/theoleuthardt/werkzeugkiste.git
synced 2026-06-13 09:37:53 +00:00
fix: ffmpeg now uses right encoder for every supported input format
This commit is contained in:
parent
f16661d1fd
commit
21a68e87e7
2 changed files with 16 additions and 6 deletions
|
|
@ -11,9 +11,14 @@ interface ConversionOptions {
|
||||||
channels?: number;
|
channels?: number;
|
||||||
sampleRate?: number;
|
sampleRate?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
let options: ConversionOptions;
|
let options: ConversionOptions;
|
||||||
|
|
||||||
|
const ffmpegEncoder: { [key: string]: string } = {
|
||||||
|
".mp3": "libmp3lame",
|
||||||
|
".wav": "pcm_s16le",
|
||||||
|
".aac": "aac",
|
||||||
|
};
|
||||||
|
|
||||||
export async function videoToAudio(app: FastifyInstance) {
|
export async function videoToAudio(app: FastifyInstance) {
|
||||||
app.post(
|
app.post(
|
||||||
"/api/video-to-audio",
|
"/api/video-to-audio",
|
||||||
|
|
@ -30,7 +35,7 @@ export async function videoToAudio(app: FastifyInstance) {
|
||||||
|
|
||||||
let fileBuffer: Buffer | null = null;
|
let fileBuffer: Buffer | null = null;
|
||||||
options = {
|
options = {
|
||||||
format: "mp3",
|
format: "",
|
||||||
bitrate: "192k",
|
bitrate: "192k",
|
||||||
channels: 2,
|
channels: 2,
|
||||||
sampleRate: 44100,
|
sampleRate: 44100,
|
||||||
|
|
@ -42,7 +47,7 @@ export async function videoToAudio(app: FastifyInstance) {
|
||||||
} else if (part.type === "field") {
|
} else if (part.type === "field") {
|
||||||
const field = part as MultipartValue<string>;
|
const field = part as MultipartValue<string>;
|
||||||
switch (field.fieldname) {
|
switch (field.fieldname) {
|
||||||
case "format":
|
case "outputFormat":
|
||||||
const format = field.value.toLowerCase();
|
const format = field.value.toLowerCase();
|
||||||
options.format = format;
|
options.format = format;
|
||||||
break;
|
break;
|
||||||
|
|
@ -63,6 +68,8 @@ export async function videoToAudio(app: FastifyInstance) {
|
||||||
return reply.status(400).send({ error: "No file uploaded!" });
|
return reply.status(400).send({ error: "No file uploaded!" });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log("options", options);
|
||||||
|
|
||||||
await fs.writeFile(inputPath, fileBuffer);
|
await fs.writeFile(inputPath, fileBuffer);
|
||||||
|
|
||||||
await new Promise<void>((resolve, reject) => {
|
await new Promise<void>((resolve, reject) => {
|
||||||
|
|
@ -71,7 +78,7 @@ export async function videoToAudio(app: FastifyInstance) {
|
||||||
inputPath,
|
inputPath,
|
||||||
"-vn",
|
"-vn",
|
||||||
"-acodec",
|
"-acodec",
|
||||||
options.format === "mp3" ? "libmp3lame" : options.format,
|
ffmpegEncoder[options.format],
|
||||||
"-ab",
|
"-ab",
|
||||||
options.bitrate || "192k",
|
options.bitrate || "192k",
|
||||||
"-ac",
|
"-ac",
|
||||||
|
|
@ -83,8 +90,12 @@ export async function videoToAudio(app: FastifyInstance) {
|
||||||
|
|
||||||
const ffmpeg = spawn("ffmpeg", args);
|
const ffmpeg = spawn("ffmpeg", args);
|
||||||
|
|
||||||
|
ffmpeg.stdout.on("data", (data) => {
|
||||||
|
console.log(`ffmpeg stdout: ${data}`);
|
||||||
|
});
|
||||||
|
|
||||||
ffmpeg.stderr.on("data", (data) => {
|
ffmpeg.stderr.on("data", (data) => {
|
||||||
console.log(`ffmpeg stderr: ${data}`);
|
console.error(`ffmpeg stderr: ${data}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
ffmpeg.on("close", (code) => {
|
ffmpeg.on("close", (code) => {
|
||||||
|
|
|
||||||
|
|
@ -114,7 +114,6 @@ export default function DocConverter() {
|
||||||
const selectedFormat =
|
const selectedFormat =
|
||||||
event.currentTarget.textContent?.trim() || "";
|
event.currentTarget.textContent?.trim() || "";
|
||||||
setSelectedOutputFormat(selectedFormat);
|
setSelectedOutputFormat(selectedFormat);
|
||||||
console.log(selectedOutputFormat);
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue