# overwriting video when start and stop video producer

**URL:** <https://mediasoup.discourse.group/t/overwriting-video-when-start-and-stop-video-producer/5725>\
**Category:** Integration\
**Created:** [December 13, 2023, 6:41am UTC](https://mediasoup.discourse.group/t/overwriting-video-when-start-and-stop-video-producer/5725 "2023-12-13T06:41:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rezahabibi1001](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@rezahabibi1001](https://mediasoup.discourse.group/u/rezahabibi1001)\
**Post date:** [December 13, 2023, 6:41am UTC](https://mediasoup.discourse.group/t/overwriting-video-when-start-and-stop-video-producer/5725/1 "2023-12-13T06:41:31Z")

</div>

i am using ffmpeg for recording , it works fine  
but after user on/off video camera , the new video stream is recording , but old video parts exist in old video is losting, please give me a solution for that

my expectation is to append new video part at continue of existence video

startRecordingFfmpeg(role) {  
// Return a Promise that can be awaited  
let recResolve;  
const promise = new Promise((res, \_rej) =\> {  
recResolve = res;  
});

```
const useAudio = this.audioEnabled();
const useVideo = this.videoEnabled();
// const useH264 = h264Enabled();

// const cmdProgram = "ffmpeg"; // Found through $PATH
const cmdProgram = FFmpegStatic; // From package "ffmpeg-static"
let cmdInputPath = `${__dirname}/recording/${this.user_id}.sdp`;
let cmdOutputPath = `${__dirname}/recording/${this.user_id}.webm`;
let cmdCodec = "";
let cmdFormat = "-f webm -flags global_header";

// Check if the output file already exists

```

const outputFileExists = fs.existsSync(cmdOutputPath);

// Update the cmdFormat for concatenation  
if (outputFileExists) {  
cmdOutputPath = `-movflags +faststart -flags global_header ${cmdOutputPath}`;  
}

```
// Ensure correct FFmpeg version is installed
const ffmpegOut = Process.execSync(cmdProgram + " -version", {
  encoding: "utf8",
});
const ffmpegVerMatch = /ffmpeg version (\d+)\.(\d+)\.(\d+)/.exec(ffmpegOut);
let ffmpegOk = false;
if (ffmpegOut.startsWith("ffmpeg version git")) {
  // Accept any Git build (it's up to the developer to ensure that a recent
  // enough version of the FFmpeg source code has been built)
  ffmpegOk = true;
} else if (ffmpegVerMatch) {
  const ffmpegVerMajor = parseInt(ffmpegVerMatch[1], 10);
  if (ffmpegVerMajor >= 4) {
    ffmpegOk = true;
  }
}

if (!ffmpegOk) {
  console.error("FFmpeg >= 4.0.0 not found in $PATH; please install it");
  process.exit(1);
}

if (useAudio) {
  cmdCodec += " -map 0:a:0 -c:a copy";
}
if (useVideo) {
  cmdCodec += " -map 0:v:0 -c:v copy";

  // if (useH264) {
  // cmdInputPath = `${__dirname}/recording/input-h264.sdp`;
  // cmdOutputPath = `${__dirname}/recording/output-ffmpeg-h264.mp4`;

  // // "-strict experimental" is required to allow storing
  // // OPUS audio into MP4 container
  // cmdFormat = "-f mp4 -strict experimental";
  // }
}

// Run process

```

// Run process  
const cmdArgStr = [  
“-nostdin”,  
“-protocol\_whitelist file,rtp,udp”,  
“-loglevel debug”,  
// “-analyzeduration 5M”,  
// “-probesize 5M”,  
“-fflags +genpts”,  
`-i ${cmdInputPath}`,  
cmdCodec,  
cmdFormat,  
“-muxdelay 0.1”, // Adjust the value as needed  
`${cmdOutputPath}`,  
].join(" ").trim();

```
// console.log(`Run command: ${cmdProgram} ${cmdArgStr}`);

let recProcess = Process.spawn(cmdProgram, cmdArgStr.split(/\s+/));
this.recProcess = recProcess;

recProcess.on("error", (err) => {
  console.error("Recording process error:", err);
});

recProcess.on("exit", (code, signal) => {
  console.log("Recording process exit, code: %d, signal: %s", code, signal);

  this.recProcess = null;
  // this.stopMediasoupRtp();

  if (!signal || signal === "SIGINT") {
    console.log("Recording stopped");
  } else {
    console.warn(
      "Recording process didn't exit cleanly, output file might be corrupt"
    );
  }
});

// FFmpeg writes its logs to stderr
recProcess.stderr.on("data", (chunk) => {
  // console.log("chuck", chunk.toString());
  console.log("called","audio:",useAudio,"video:",useVideo);
  chunk
    .toString()
    .split(/\r?\n/g)
    .filter(Boolean) // Filter out empty strings
    .forEach((line) => {
      if (line.startsWith("ffmpeg version")) {
        setTimeout(() => {
          recResolve();
        }, 1000);
      }
    });
});

return promise;

```

}
