bytedeco / javacv

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

The sound and picture cannot be synchronized #1973

Closed redoriental closed 1 year ago

redoriental commented 1 year ago

long interval = 1_000 / 30;

            long startTime = System.currentTimeMillis();

            Frame frame;
            while ((frame = grabber.grab()) != null) {

                recorder.record(frame);
                long currentTime = 1_000 * (System.currentTimeMillis() - startTime);
                long frameTime = frame.timestamp;
                long sleepTime = (frameTime - currentTime) / 1_000;
                try {
                    if (sleepTime > 500 + interval) {
                        Thread.sleep(interval);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

When I use this code, I will have the problem that the sound and picture are not synchronized

redoriental commented 1 year ago

If I use ffmpeg.exe directly, this problem will not occur

saudet commented 1 year ago

Try to call setTimestamp(frame.timestamp) before record(). Please do not open duplicate issues.

saudet commented 1 year ago

Duplicate of #1972