bytedeco/javacv

Video stream is earlier than audio stream when converting rtmp to rtmp

Open

#1,722 opened on Nov 21, 2021

View on GitHub
 (25 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

Hello,I have used JavaCV from several months. Recentlly, I wanna to use it to transfer stream from one rtmp source A to another rtmp source B. but I find when I play B on Potplayer, Video stream is earlier than Audio stream in most scenarios. and the latency time is random. In rate case, Video Steam is sync with Audio Stream. The information I have: I use OBS to push a stream( from A local file ) to rtmp://A Init FFmpegFrameGrabber with rtmp://A and grabFrame(audio:true,video:true,processing:true,keyframe:false,data:false) Init FFmpegFrameRecorder with _rtmp://B and record(Frame) Play rtmp://B on Potplayer or VLC. The key code: ` try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(rtmpA);) { grabber.setOption("s_timeout", String.valueOf(10 * 1000_000)); grabber.setOption("rw_timeout", String.valueOf(10 * 1000_000)); grabber.start();

        try (FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(rtmpB, grabber.getImageWidth(), grabber.getImageHeight(), grabber.getAudioChannels())) {
            recorder.setVideoOption("crf", "18");

// recorder.setGopSize(2); recorder.setFrameRate(grabber.getVideoFrameRate()); recorder.setVideoCodec(AV_CODEC_ID_H264); recorder.setFormat("flv"); recorder.setMaxDelay(0); recorder.setInterleaved(true); recorder.setVideoOption("tune", "zerolatency"); recorder.setVideoOption("preset", "ultrafast"); recorder.setVideoOption("rtsp_transport", "tcp"); recorder.start(); Frame frame; while ((frame = grabber.grabFrame(true,true,true,false,false)) != null) { slideWindow.add(frame.clone()); } The method I have Tried: have tried many combinations of parameters。such as recorder.setTimestamp(grabber.getTimestamp),recorder.setOption("vsync",xx). recorder.setFrameRate(a fixed number)but none of them worked. For the normal run of this basic function, I'm about to crash. Looking for your response.

Contributor guide