Home > database >  String is showing undefined
String is showing undefined

Time:10-30

I am trying to create video using ffmpeg and returning its path to index.js but path is showing undefined.

index.js

app.get('/content/:myfact', async (req, res) => {
  let myfactdata = req.params.myfact;
  let myfact = myfactdata.replace("_", " ");
  const video = await generateVideo(myfact);
  console.log(video)
});

This is my contentVideo.js

const generateImage = require('./contentimage.js');
const { spawn } = require('child_process');

const generateVideo = async (myfact) => {
  await generateImage(myfact);
  const child = spawn('ffmpeg', [
    '-i', 'pictures/download/image.png', 'pictures/download/video.mp4'
  ]);
  child.on('close', function (code, signal) {
    const video = 'pictures/download/video.mp4'
    return video
  });
}

module.exports = generateVideo

CodePudding user response:

Should be something like this:

const generateVideo = async (myfact) => {
  await generateImage(myfact);
  const child = spawn('ffmpeg', [
    '-i', 'pictures/download/image.png', 'pictures/download/video.mp4'
  ]);

  return new Promise((resolve, reject) => {
    child.on('close', function (code, signal) {
      const video = 'pictures/download/video.mp4'
      resolve(video);
    });

    child.on('error', function(err){
      reject(err);
    });
  })
}
  • Related