Video Conversion API Landing But Request Type Invalid
Unanswered
American black bear posted this in #help-forum
American black bearOP
I built an API for converting any uploaded videos to WebM format using HandBrake-js. The selected file renders perfectly fine in the video player, but I run an API before the video is sent to the backend that runs a signedUrl function and also this video conversion API. The API lands just fine but I get a 405 error for the POST request, which means I have done something wrong here.
import Handbrake from 'handbrake-js';
import formidable from 'formidable';
import fs from 'fs';
import { NextResponse } from 'next/server';
// Function to convert video
const convertVideo = (inputPath, outputPath) => {
return new Promise((resolve, reject) => {
Handbrake.spawn({ input: inputPath, output: outputPath, preset: 'Web' })
.on('error', (err) => reject(err))
.on('end', () => resolve(outputPath));
});
};
// POST method to handle video conversion
export async function POST(request) {
try {
const form = new formidable.IncomingForm();
form.uploadDir = './uploads';
form.keepExtensions = true;
return new Promise((resolve, reject) => {
form.parse(request, async (err, fields, files) => {
if (err) {
resolve(NextResponse.json({ error: 'Error parsing file' }, { status: 500 }));
return;
}
const inputPath = files.file.path;
const outputPath =
try {
await convertVideo(inputPath, outputPath);
resolve(NextResponse.json({ success: true, outputPath }, { status: 200 }));
} catch (error) {
resolve(NextResponse.json({ error: 'Error converting video' }, { status: 500 }));
} finally {
fs.unlink(inputPath, (err) => {
if (err) console.error('Error removing input file:', err);
});
}
});
});
} catch (error) {
return NextResponse.json({ error:
}
}
import Handbrake from 'handbrake-js';
import formidable from 'formidable';
import fs from 'fs';
import { NextResponse } from 'next/server';
// Function to convert video
const convertVideo = (inputPath, outputPath) => {
return new Promise((resolve, reject) => {
Handbrake.spawn({ input: inputPath, output: outputPath, preset: 'Web' })
.on('error', (err) => reject(err))
.on('end', () => resolve(outputPath));
});
};
// POST method to handle video conversion
export async function POST(request) {
try {
const form = new formidable.IncomingForm();
form.uploadDir = './uploads';
form.keepExtensions = true;
return new Promise((resolve, reject) => {
form.parse(request, async (err, fields, files) => {
if (err) {
resolve(NextResponse.json({ error: 'Error parsing file' }, { status: 500 }));
return;
}
const inputPath = files.file.path;
const outputPath =
${form.uploadDir}/${files.file.name}.webm;try {
await convertVideo(inputPath, outputPath);
resolve(NextResponse.json({ success: true, outputPath }, { status: 200 }));
} catch (error) {
resolve(NextResponse.json({ error: 'Error converting video' }, { status: 500 }));
} finally {
fs.unlink(inputPath, (err) => {
if (err) console.error('Error removing input file:', err);
});
}
});
});
} catch (error) {
return NextResponse.json({ error:
Unexpected error: ${error.message} }, { status: 500 });}
}
12 Replies
American black bearOP
Yep, the endpoint specifically is '/api/convert' and the api file is named route.js. It is also deployed through AWS Amplify if that helps at all but like I said it lands, so I don't think the AWS configuration is the issue I feel like the code is wrong lol.
does it work locally
American black bearOP
Same issue when ran locally for me, it lands but gives an invalid request type. I tried FFmpeg before this but couldn't figure it out either. I suck at video conversion it seems lol.
I even tried a Python function man lol
before u invoke formidable, can u place a early return and check if that triggers
American black bearOP
That is one thing I haven't tried yet so I don't see why not lol.
If three lines fixes this I will be happy af lol
it justs to check if your other logic works fine
then we can further debug this
American black bearOP
Getting 405 method not allowed... SAD FACE
This was the early return I added:
export async function POST(request) {
try {
if (!request) {
return NextResponse.json({ error: 'Request is missing' }, { status: 400 });
}
export async function POST(request) {
try {
if (!request) {
return NextResponse.json({ error: 'Request is missing' }, { status: 400 });
}