bytedeco/javacv

how to pass fps_mode option when using javacv

Open

#2,156 opened on Dec 28, 2023

View on GitHub
 (2 comments) (0 reactions) (0 assignees)Java (1,583 forks)batch import
help wantedquestion

Repository metrics

Stars
 (6,985 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

I am trying to record RTSP stream and store it into hls so that I can stream it to UI as and when it is required. But the stream is having a variable frame rate and as per requirement I have to record it into constant frame rate, so using -fps_mode option in following ffmpeg command I was able to achieve that:

ffmpeg -use_wallclock_as_timestamps 1 -rtsp_transport tcp -i <rtsp-url> -fps_mode cfr -vf "fps=8.0" -max_delay 0 -f hls -fflags nobuffer -fflags flush_packets -vcodec libx264 -hls_flags program_date_time+split_by_time+append_list -hls_list_size 0 -hls_time 5 <output-file>

Now I am trying to implement the same in JavaCV. I am using following dependencies
javacv version: 1.5.9
javacpp version: 1.5.9
ffmpeg version: 6.0-1.5.9

// FrameGrabber creation
FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(streamURL);
frameGrabber.setOption("timeout", timeout);
frameGrabber.setOption("rtsp_transport", rtspTransportProtocol);
frameGrabber.setOption("loglevel", "debug");
frameGrabber.setOption("fflags", "nobuffer");
frameGrabber.setOption("use_wallclock_as_timestamps", "1");
frameGrabber.setOption("fps_mode", "cfr");
// frameGrabber.setOption("vsync", "1");
frameGrabber.setFormat("rtsp");
frameGrabber.setTimeout(3 * 1000000);
frameGrabber.start();

//FrameRecorder creation
FFmpegFrameRecorder hlsRecorder = new FFmpegFrameRecorder(outputFile, frameWidth, frameHeight, audioChannel);
hlsRecorder.setInterleaved(true);
hlsRecorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
hlsRecorder.setFrameRate(frameGrabber.getFrameRate());
hlsRecorder.setFormat("hls");
hlsRecorder.setGopSize(gopSize);
hlsRecorder.setOption("hls_flags", "program_date_time+split_by_time+append_list");
hlsRecorder.setOption("hls_list_size", "0"); // This option sets the playlist to an infinite size
hlsRecorder.setOption("hls_time", "5"); // This option sets the duration of each segment
// hlsRecorder.setVideoOption("fps_mode", "cfr");
// hlsRecorder.setVideoOption("vsync", "1");
hlsRecorder.start();

I have tried setting fps_mode to cfr in grabber as well as recorder it did not work. Can someone please guide me
on how to correctly set this option?

Contributor guide