SharpAvi icon indicating copy to clipboard operation
SharpAvi copied to clipboard

Screen recording as video, audio on SharpAvi - Audio not recording

Open Gopichandar opened this issue 4 years ago • 4 comments

What has been achieved: I have successfully integrated that with the sample provided and I'm trying to capture the audio through NAudio with SharpAPI video stream for the video to record along with audio implementation.

Issue: Whatever I write the audio stream in SharpAvi video. On output, It was recorded only with video and audio is empty.

Checking audio alone to make sure: But When I try capture the audio as separate file called "output.wav" and It was recorded with audio as expected and can able to hear the recorded audio. So, I'm concluding for now that the issue is only on integration with video via SharpApi

writterx = new WaveFileWriter("Out.wav", audioSource.WaveFormat);

Full code to reproduce the issue: https://drive.google.com/open?id=1H7Ziy_yrs37hdpYriWRF-nuRmmFbsfe-

Code glimpse from Recorder.cs NAudio Initialization:

audioSource = new WasapiLoopbackCapture();

audioStream = CreateAudioStream(audioSource.WaveFormat, encodeAudio, audioBitRate);

audioSource.DataAvailable += audioSource_DataAvailable;

Capturing audio bytes and write it on SharpAvi Audio Stream:

private void audioSource_DataAvailable(object sender, WaveInEventArgs e)
{
    var signalled = WaitHandle.WaitAny(new WaitHandle[] { videoFrameWritten, stopThread });
    if (signalled == 0)
    {
        audioStream.WriteBlock(e.Buffer, 0, e.BytesRecorded);               
        audioBlockWritten.Set();
        Debug.WriteLine("Bytes: " + e.BytesRecorded);
    }
}

Can you please suggest a Solution

Asked the same in SO https://stackoverflow.com/questions/60238492/screen-recording-as-video-audio-on-sharpavi-audio-not-recording?noredirect=1

Gopichandar avatar Feb 25 '20 04:02 Gopichandar

I know this is old, but I'm stuck at the same place with this.

@Gopichandar, did you even get your solution working?

BranigansLaw avatar Apr 14 '21 19:04 BranigansLaw

Just to follow up on this, I believe the issue is using the JPEG Motion format which might not allow embedding of audio. I tried switching to DivX format but SharpAVI kept throwing an exception I didn't have that codec (even though DivX is installed on my system).

In the end, I decided to switch to recording the sections separately and then recombining with ffmpeg.

If anyone found a way around this, let me know! I'd much rather stream into on file then saving and using ffmpeg to recombine.

BranigansLaw avatar Apr 15 '21 22:04 BranigansLaw

I doing the same thing and I get part of the sound, but it is in extremely bad quality, too many silent pieces: like I'm saying 1-2-3-4-5-6, 1 can go properly, then start of the 2, then silence or some noise and then I get the end of three and no 4-5-6 at all.

cytivrat avatar Nov 06 '21 09:11 cytivrat

As I found out, in my case adding new frame took 0.2-0.3 seconds, when I set frame rate to 10. So I was getting a new frame data longer than I tried to write it. After I optimized my code that creates a new frame, I was able to get clear sound inside recorded avi file

cytivrat avatar Nov 06 '21 14:11 cytivrat