bytedeco / javacv

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

org.bytedeco.javacv.FFmpegFrameRecorder$Exception: avformat_write_header error() error -40: Could not write header to 'rtmp://192.168.0.104/live/livestream' #1839

Open qingzhang0 opened 2 years ago

qingzhang0 commented 2 years ago

Please how to solve this problem:

Input #0, rtsp, from 'rtsp://admin:H2@33c66@192.168.0.67:554/Streaming/Channels/102': Metadata: title : Media Presentation Duration: N/A, start: 0.000000, bitrate: N/A Stream #0:0: Video: hevc (Main), yuvj420p(pc), 640x480 [SAR 1:1 DAR 4:3], 25.08 tbr, 90k tbn, 90k tbc Stream #0:1: Audio: aac (LC), 16000 Hz, mono, fltp x265 [info]: HEVC encoder version 0.0 x265 [info]: build info [Windows][GCC 10.3.0][64 bit] 8bit+10bit+12bit x265 [info]: using cpu capabilities: MMX2 SSE2Fast LZCNT SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2 x265 [info]: Main profile, Level-3 (Main tier) x265 [info]: Thread pool created using 12 threads x265 [info]: Slices : 1 x265 [info]: frame threads / pool features : 3 / wpp(8 rows) x265 [warning]: Source height < 720p; disabling lookahead-slices x265 [info]: Coding QT: max CU size, min CU size : 64 / 8 x265 [info]: Residual QT: max TU size, max depth : 32 / 1 inter / 1 intra x265 [info]: ME / range / subpel / merge : hex / 57 / 2 / 3 x265 [info]: Keyframe min / max / scenecut / bias : 25 / 250 / 40 / 5.00 x265 [info]: Lookahead / bframes / badapt : 20 / 4 / 2 x265 [info]: b-pyramid / weightp / weightb : 1 / 1 / 0 x265 [info]: References / ref-limit cu / depth : 3 / off / on x265 [info]: AQ: mode / str / qg-size / cu-tree : 2 / 1.0 / 32 / 1 x265 [info]: Rate Control / qCompress : CRF-28.0 / 0.60 x265 [info]: tools: rd=3 psy-rd=2.00 early-skip rskip mode=1 signhide tmvp x265 [info]: tools: b-intra strong-intra-smoothing deblock sao Exception in thread "Thread-11" java.lang.RuntimeException: org.bytedeco.javacv.FFmpegFrameRecorder$Exception: avformat_write_header error() error -40: Could not write header to 'rtmp://192.168.0.104/live/livestream' (For more details, make sure FFmpegLogCallback.set() has been called.) at com.icdio.building.service.impl.IPCServiceImpl.lambda$playVideo$0(IPCServiceImpl.java:91) at java.lang.Thread.run(Thread.java:748) Caused by: org.bytedeco.javacv.FFmpegFrameRecorder$Exception: avformat_write_header error() error -40: Could not write header to 'rtmp://192.168.0.104/live/livestream' (For more details, make sure FFmpegLogCallback.set() has been called.) at org.bytedeco.javacv.FFmpegFrameRecorder.startUnsafe(FFmpegFrameRecorder.java:907) at org.bytedeco.javacv.FFmpegFrameRecorder.start(FFmpegFrameRecorder.java:413) at com.icdio.building.util.video.VideoUtils.getFFmpegFrameRecorder(VideoUtils.java:169) at com.icdio.building.util.video.VideoUtils.readRTSPForEventByGrabber(VideoUtils.java:78) at com.icdio.building.service.impl.IPCServiceImpl.lambda$playVideo$0(IPCServiceImpl.java:89) ... 1 more

saudet commented 2 years ago

What is your platform?

qingzhang0 commented 2 years ago

What is your platform?

My computer operating system is window10 home edition, I run Java project on Intellj idea2022.1.3, using JavaCV1.5.6. I have been using the Dahua camera before, and it has been working normally, but now I don't have the Dahua camera, I use the Hikvision camera.

this is my code

public static void readRTSPByGrabber(String inPath, String outPath) throws FFmpegFrameRecorder.Exception, FFmpegFrameGrabber.Exception {
        Frame captured_frame = null;
        FFmpegFrameGrabber grabber = getFFmpegFrameGrabber(inPath);
        FFmpegFrameRecorder recorder = getFFmpegFrameRecorder(grabber, outPath);
        while (true) {
            try {
                captured_frame = grabber.grabFrame();

                if (captured_frame == null) {
                    System.out.println("!!! Failed cvQueryFrame");
                    break;
                }
                recorder.record(captured_frame);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        closeGrabberRecorder(grabber, recorder);
    }

static FFmpegFrameGrabber getFFmpegFrameGrabber(String inPath) throws FFmpegFrameGrabber.Exception {
        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inPath);
        grabber.setOption("rtsp_transport", "tcp");
        grabber.setMaxDelay(550);
//        grabber.getFormatContext().max_delay(550);
//        grabber.getFormatContext().max_analyze_duration(5 * avutil.AV_TIME_BASE);
//        grabber.getFormatContext().flags(AVFormatContext.AVFMT_FLAG_NOBUFFER);
        grabber.start();
        return grabber;
    }

    static FFmpegFrameRecorder getFFmpegFrameRecorder(FFmpegFrameGrabber grabber, String outPath) throws FFmpegFrameRecorder.Exception {
        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(outPath, grabber.getImageWidth(), grabber.getImageHeight(), grabber.getAudioChannels());
        recorder.setVideoCodec(grabber.getVideoCodec());
        //recorder.setPixelFormat(avutil.AV_PIX_FMT_YUVJ420P);
        recorder.setFormat("flv");
        recorder.setFrameRate(grabber.getFrameRate());
        recorder.setVideoBitrate(grabber.getVideoBitrate());
        recorder.setAudioBitrate(192000);
        recorder.setAudioOptions(grabber.getAudioOptions());
        recorder.setAudioQuality(0);
        recorder.setSampleRate(44100);
        recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC);
        recorder.start();
        return recorder;

    }
saudet commented 2 years ago

On Windows, error 40 is "Function not implemented", so it sounds like a problem with your network settings...

qingzhang0 commented 2 years ago

Can FFmpegFrameRecorder push x265 video streams? MP4 can be pushed to streaming server in virtual, my camera is h265 encoded, I can play camera video stream with ffmpeg, so I doubt it is FFmpegFrameRecorder can push h265 video stream, my camera has audio.

saudet commented 2 years ago

There's no reason why it couldn't.