bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.4k stars 1.57k forks source link

how to pass fps_mode option when using javacv #2156

Open deepaksawant opened 6 months ago

deepaksawant commented 6 months ago

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?

deepaksawant commented 6 months ago

@saudet any idea how can I set fps_mode to cfr using javacv?

saudet commented 6 months ago

I would guess like you tried, but if that doesn't work, I'd check the source code