0

I am using Node.js for backend to use ffmpeg. To use ffmpeg, I adopted fluent-ffmpeg. This is working perfectly except for one problem. That is when I send video from client side SPA(single page application) to the server-side for the "second" time, node application crashes.

What I did is saving the received video in the backend folder, which is made in the process, and taking a snapshot of the video. This code actually works everytime after restarting the server, but once I used this route and try another time, the error message "Invalid data found when processing input video ./received/${receivedName}" comes up.

app.post("/convert", fileUpload({ createParentPath: true }), async function (req, res) {
    makeDir(receivedVideoDirectory);
    const receivedName = Object.keys(req.files)[0];
    const directoryName = receivedName.substring(0, receivedName.indexOf("."));

    const receivedFile = req.files[receivedName];
    transcodedSegFolder = `./public/transcoded/${dirName}`;
    console.log("transcoded segment file folder is here", transcodedSegFolder);
    makeDir(transcodedSegFolder);

    fs.open(`./received/${receivedName}`, 'w', (err, fd) => {
        if (err) throw err;
        fs.writeFile(fd, receivedFile["data"], function (err) {
            if (err) {
                return console.log("Err in write file ", err);
            }
            console.log("The file was saved!", receivedName);
            fs.close(fd, (err) => {
                if (err) throw err;
            });
        });
    });

    ffmpeg(`./received/${receivedName}`)
        .takeScreenshots(
            {
                count: 1,
                timemarks: ['00:00:01.000'],
                folder: './public/thumbnails',
                filename: dirName
            }).on('error', function(err) {
                console.log('screenshot error happened: ' + err.message);
              }).on('end', function(err) {
                console.log('Screenshot process finished: ');
              });

Probably some program keeps working, but I cannot find it. Any help is appreciated. Thank you.

0 Answers0