fluent-ffmpeg / node-fluent-ffmpeg

A fluent API to FFMPEG (http://www.ffmpeg.org)
MIT License
7.85k stars 874 forks source link

How to replicate Screen Recording command? Then combine it with ExpressJS? #1100

Open UserMust8 opened 3 years ago

UserMust8 commented 3 years ago

Version information

Code to reproduce

const express = require('express');
const app = express();
const ffmpeg = require('fluent-ffmpeg');
const os = require("os");
const port = 3212;
var currentScriptPath = process.cwd();

let ffmpegCommand = () => {
  if(os.platform == "win32") {
    //Return the ffmpeg command
    return ffmpeg().inputOptions([
      '-f gdigrab',
      '-framerate 30',
      '-i desktop',
    ])
    .save("output.mkv");

  } else {
    return "not windows" //handle this later
  }
};

app.get('/stopRec', (req, res) => {
  try {
    ffmpegCommand.kill('SIGSTOP');
    res.send("Recording stopped")
  } catch (e) {
    res.send(`Error trying to stop recording: ${e}`)
  }
})

app.get('/startRec', (req, res) => {
  try {
    ffmpegCommand.run();
    res.send("Recording started")
  } catch (e) {
    res.send(`Error trying to start recording: ${e}`)
  }
})

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

My Issue explained

I have a goal to run ffmpeg from an ExpressJS server. The idea is to replace this command here:

https://trac.ffmpeg.org/wiki/Capture/Desktop (In the 'Use built-in GDI screengrabber')

This command

ffmpeg -f gdigrab -framerate 30 -i desktop output.mkv

It works no problem on my Windows 10 but the issue is that when I run it on command line, I would have to stop it using Cntrl+C.

I am new to this library so I am trying to understand it. The idea is that I send a request to "/startRec" and it starts recording to "output.mkv" and when the "/startRec" is received, it stops recording (does not need to return the file, just save it to the file system)

Attempting to visit "/startRec" gives me this:

Error trying to start recording: TypeError: ffmpegCommand.run is not a function

Yet the command is here: https://github.com/fluent-ffmpeg/node-fluent-ffmpeg#outputtarget-options-add-an-output-to-the-command

I think im not understanding the library correctly so Im hoping to get some advice. I know this will run on linux so I am trying to set options depending on different OSes but how do I get ffmpeg to stop & start recording via ExpressJS?

Checklist

sheunglaili commented 3 years ago

I think you hadn't invoke that function. should be ffmpegCommand().run