sskodje / ScreenRecorderLib

A .NET library for screen recording in Windows, using native Microsoft Media Foundation for realtime encoding to h264 video or PNG images.
MIT License
414 stars 94 forks source link

Screen recording Audio Sync issue #224

Open ranwer-dev opened 1 year ago

ranwer-dev commented 1 year ago

I have tried recording google meet calls with it. In the beginning, audio and video remain in sync but after some time, the audio goes out of sync. Mostly Audio is ahead of the video. In the most recent recording, I did a 20-minute recording. The audio video was fine for the first 3 minutes and then the audio was ahead of the video.

These are the options that I use for recording

RecorderOptions.OutputOptions.RecorderMode = RecorderMode.Video;

RecorderOptions.VideoEncoderOptions.Framerate = 30;
RecorderOptions.VideoEncoderOptions.IsFixedFramerate = true;
RecorderOptions.VideoEncoderOptions.Quality = 80;
RecorderOptions.VideoEncoderOptions.Encoder = new H264VideoEncoder
{
    BitrateMode = H264BitrateControlMode.Quality,
    EncoderProfile = H264Profile.High
};

RecorderOptions.AudioOptions.IsAudioEnabled = true;
RecorderOptions.AudioOptions.IsInputDeviceEnabled = true;
RecorderOptions.AudioOptions.IsOutputDeviceEnabled = true;
RecorderOptions.AudioOptions.AudioInputDevice = (AudioDeviceComboBox.SelectedItem as AudioDevice).DeviceName;
RecorderOptions.AudioOptions.AudioOutputDevice = (SystemAudioDeviceComboBox.SelectedItem as AudioDevice).DeviceName;
RecorderOptions.AudioOptions.InputVolume = 1;
RecorderOptions.AudioOptions.OutputVolume = 1;

RecorderOptions.MouseOptions.IsMousePointerEnabled = true;

RecorderOptions.SourceOptions.RecordingSources = new List<RecordingSourceBase>
    {
        new DisplayRecordingSource(monitor)
        {
            OutputSize = monitor.OutputSize,
            Position = monitor.Position,
            RecorderApi = RecorderApi.WindowsGraphicsCapture,
            IsBorderRequired = false
        }
    };
}
sskodje commented 1 year ago

Was this with one of the releases, or compiled from the main branch? I pushed some fixes for audio drift issues a few days ago, but haven't made a new release yet. If the issues was with an older release, you can try the new changes from one of the automatic builds here if you wish.

ranwer-dev commented 1 year ago

Yeah, I noticed that recent commit. I was using latest branch code.

sskodje commented 1 year ago

Do you know the sample rate and bit rate of your microphone and output device in windows audio settings? I'm guessing it has to be something about that, as i'm unable to reproduce.

ranwer-dev commented 1 year ago

I was using a USB mic image

And Bluetooth headphone image

But I have experienced this out-of-sync issue with simple wired hands-free too when I was not recording System Audio.

sskodje commented 1 year ago

Thanks, that's very helpful. I can probably guess it's due to mixing input and output with different samle rates, as i've had some bugs with that before. I'll try to reproduce .

ranwer-dev commented 1 year ago

Were you able to reproduce/fix this?

sskodje commented 1 year ago

Unfortunately i wasn't able to reproduce it on any of my hardware, so the issue is kind of stuck.

SID9-HoneyBee commented 1 year ago

I have a similar issue. Could this issue have been resolved? For the first 5 minutes after recording, the video and audio input are in sync. However, as the recording continues, it is out of sync. I am using version 3.1.2.

sskodje commented 1 year ago

I fixed some audio drift issues in version 5.2.0. You see if it fixes your issues.

https://github.com/sskodje/ScreenRecorderLib/releases/tag/v5.2.0

ranwer-dev commented 1 year ago

No, the issue is still there. It goes fine for the first few minutes, but later System's Audio gets delayed from the video.

I fixed some audio drift issues in version 5.2.0. You see if it fixes your issues.

https://github.com/sskodje/ScreenRecorderLib/releases/tag/v5.2.0

SID9-HoneyBee commented 1 year ago

I fixed some audio drift issues in version 5.2.0. You see if it fixes your issues.

https://github.com/sskodje/ScreenRecorderLib/releases/tag/v5.2.0

I was afraid to migrate to version 5 while using 3.1.2. But I got it working yesterday. As a result of testing by applying version 5.2.0, it is confirmed that the audio drift issue has been resolved. Let's do a little more testing. Thank you very much.

I would like to ask an additional question regarding WebCam Overlay.

ranwer-dev commented 1 year ago

As a result of testing by applying version 5.2.0, it is confirmed that the audio drift issue has been resolved.

@SID9-HoneyBee your issue was with microphone audio or System Audio? My System audio issue is still there.

SID9-HoneyBee commented 1 year ago

As a result of testing by applying version 5.2.0, it is confirmed that the audio drift issue has been resolved.

@SID9-HoneyBee your issue was with microphone audio or System Audio? My System audio issue is still there.

Haven't tested in many environments. However, in testing in four environments, all out-of-sync issues were resolved.

Migrated to version 5.2.0. That's it.

ranwer-dev commented 1 year ago

I have observed the System Audio recording lag issue happens with Wiresless headphone. With builtin laptop speaker, it works fine.

ranwer-dev commented 3 months ago

It's still a valid issue. When we are recording both microphone and system audio at the same time.