bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.55k stars 1.58k forks source link

Video and Audio in FFmpegFrameGrabber not same time #1058

Open fpt-mnes-linhtc1 opened 6 years ago

fpt-mnes-linhtc1 commented 6 years ago

Hi everyone,

I am recording Camera and Audio by FFmpegFrameGrabber and streaming to Facebook. FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("video=FaceTime HD Camera:audio=Headset Microphone (Cirrus Logic CS4208 (AB 128))"); Then I check video on my Facebook, Camera and Audio not same time. I tried setTimeStamp, but it doesn't work. This is setTimeStamp code:

while ((capturedFrame = grabber.grab()) != null)
{
            // Let's define our start time...
            // This needs to be initialized as close to when we'll use it as
            // possible,
            // as the delta from assignment to computed time could be too high
            if (startTime == 0)
                startTime = System.currentTimeMillis();

            // Create timestamp for this frame
            videoTS = 1000 * (System.currentTimeMillis() - startTime);

            // Check for AV drift
            if (videoTS != recorder.getTimestamp())
            {
                System.out.println(
                        "Lip-flap correction: " 
                        + videoTS + " : "
                        + recorder.getTimestamp() + " -> "
                        + (videoTS - recorder.getTimestamp()));

                // We tell the recorder to write this frame at this timestamp
                recorder.setTimestamp(videoTS);
            }
            recorder.record(capturedFrame);
}

Please help me fix this issue. Thank a lot of.

saudet commented 6 years ago

You're probably not grabbing frames fast enough. You'll need to do that to keep streams in sync.

fpt-mnes-linhtc1 commented 6 years ago

You're probably not grabbing frames fast enough. You'll need to do that to keep streams in sync.

How do grabbing frame fast enough? Please help me.

saudet commented 6 years ago

Don't call anything else like record() in the same thread.

fpt-mnes-linhtc1 commented 6 years ago

Don't call anything else like record() in the same thread.

Yes, only code setTimestamp and record(), but audio and video still not same time.

saudet commented 6 years ago

Right, so remove those calls and put them in another thread.

fpt-mnes-linhtc1 commented 6 years ago

Right, so remove those calls and put them in another thread.

Yes, I tried remove those and put them in another thread. But audio and video still not same time.

saudet commented 6 years ago

That sounds like an issue with FFmpeg, but in any case you'll probably have more help asking about that upstream.