nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.44k stars 276 forks source link

audio recording by the use of "node audiorecoder" #4368

Closed Jyotissss closed 3 months ago

Jyotissss commented 3 months ago

hey , i have a query actually i want to record screen and audio both, screen is recording perfect but audio is not, means file is generating for audio and video but audio file is empty so how can i solve this issue . please check this once

 async startRecording(url, videoOutputFile, audioOutputFile) {
    try {
      const page = this.page[url];

      // Check if outputFile paths are valid strings
      if (
        typeof videoOutputFile !== "string" ||
        !videoOutputFile.trim() ||
        typeof audioOutputFile !== "string" ||
        !audioOutputFile.trim()
      ) {
        throw new Error("Invalid outputFile paths");
      }

      // Resolve absolute paths for outputFiles
      const absoluteVideoPath = path.resolve(__dirname, videoOutputFile);
      const absoluteAudioPath = path.resolve(__dirname, audioOutputFile);
      console.log("Video output file:", absoluteVideoPath);
      console.log("Audio output file:", absoluteAudioPath);

      // Create a writable stream for video recording
      const videoStream = fs.createWriteStream(absoluteVideoPath);

      // Start video recording
      const recorderOptions = {
        outputStream: videoStream,
      };
      this.videoRecorder = new PuppeteerScreenRecorder(page, recorderOptions);
      const videoPromise = this.videoRecorder.start(videoOutputFile);
      console.log("Video recording started");

      // Create a writable stream for audio recording
      const audioOutput = fs.createWriteStream(absoluteAudioPath);
      this.audioRecorder = new AudioRecorder({
        program: "ffmpeg", // Use 'ffmpeg' for Windows, Linux, or macOS
        sampleRate: 44100,
        channels: 2,
        bitsPerSample: 16,
        format: "wav",
        bitrate: "192k",
        silence: 0,
        thresholdStart: 0.5, // Silence threshold to start recording.
        thresholdStop: 0.5,
        keepSilence: true,
      });

      // Listen for data event of the audio recorder
      this.audioRecorder.on("data", (data) => {
        audioOutput.write(data);
      });
      // Listen for error event of the audio recorder
      this.audioRecorder.on("error", (error) => {
        console.error("Audio recording error:", error);
      });
      this.audioRecorder
        .start()
        .stream()
        .on("data", (data) => {
          console.log("Received audio data:", data);
        })
        .pipe(audioOutput);

      // Start audio recording
      await this.audioRecorder.start();
      console.log("Audio recording started");

      await Promise.all([videoPromise]);

      console.log("Video and audio recording finished");
    } catch (error) {
      console.error("Error starting recording:", error);
    }
  }

  async stopRecording() {
    try {
      // Stop video recording
      await this.videoRecorder.stop();
      console.log("Video recording stopped");
    } catch (error) {
      console.error("Error stopping video recording:", error);
    }

    try {
      // Stop audio recording if the audio recorder exists
      if (this.audioRecorder) {
        await this.audioRecorder.stop();
        console.log("Audio recording stopped");
      } else {
        console.log(
          "No audio recorder found or audio recording already stopped"
        );
      }
    } catch (error) {
      console.error("Error stopping audio recording:", error);
    }
    setTimeout(() => {}, 10000);
  }
tniessen commented 3 months ago

This doesn't seem related to Node.js core. You might want to check the documentation of whatever libraries you are using, or perhaps ask the maintainers of those libraries.