[Libav-user] Trouble writing a java screen recording application on MacOS

Aa Bb spectacularcrackers at gmail.com
Fri Mar 25 06:20:14 EET 2022


Hello,

I am attempting to write a java application that uses FFmpeg to record the
screen of a Macbook.

If this is the incorrect place to be asking this question, are there any
examples of accomplishing the screen recording with libav?

My approach was to use a while loop to continuously capture the frames.

However, only one frame gets captured and the recording stops. Do you have
any ideas on what is going wrong?

I am getting the following warnings but could not figure out how to resolve
them:

> [AVFoundation indev @ 0x12de64c00] Configuration of video device failed,
> falling back to default.
> [avfoundation @ 0x12de64980] Stream #0: not enough frames to estimate
> rate; consider increasing probesize
> [aac @ 0x103506660] 1 frames left in the queue on closing


Here is the code I have:

import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.*;
import org.bytedeco.opencv.opencv_java;

import java.awt.*;
import java.io.IOException;

public class ScreenCaptureTrial {
    private static final String DEVICE_INDEX = "1:0"; //1 for mac's screen
    private static final int FRAME_RATE = 60;

    public static void main(String[] args) throws IOException {

        Loader.load(opencv_java.class);

        Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
        final int captureWidth = (int) size.getWidth();
        final int captureHeight = (int) size.getHeight();

        FFmpegLogCallback.set();
        FFmpegFrameGrabber.tryLoad();
        FrameGrabber grabber = new FFmpegFrameGrabber(DEVICE_INDEX);
        grabber.setFormat("avfoundation");
        grabber.setPixelFormat(avutil.AV_PIX_FMT_0RGB);


        grabber.setFrameRate(FRAME_RATE);
        grabber.start();

        final FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(
                "screen_recording_trial.mp4",
                captureWidth, captureHeight, 2);

        //Start recording
        recorder.start();


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

        recorder.stop();
        grabber.stop();
    }
}


Thank you
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20220325/fda8dada/attachment.htm>


More information about the Libav-user mailing list