使用JAVA CV接收RTP流,并通过rtsp转发示例代码

1,209 阅读1分钟

主要实现接收PJSIP中推送过来的RTP媒体流,通过rtsp协议转发到流媒体服务器进行转码,浏览器中可通过http-flv查看视频直播。通过sdp描述文件作为流输入,把第一帧数据取出来,并提取宽高参数。使用java cv接收rtp流的实现代码:

/**
  * sourcePath:SDP描述文件的路径
  * targetPath: 推送的目标地址
  **/
  @Override
    public String startCV(String sourcePath,String targetPath) throws FrameGrabber.Exception, FrameRecorder.Exception {

        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(sourcePath);
        grabber.setOption("protocol_whitelist","file,rtp,udp");
        //grabber.setVideoOption("f","rtsp");
        grabber.setVideoOption("vcodec","copy");
        grabber.setOption("thread_queue_size","200");
        // grabber.delayedGrab(5000000);
        // grabber.setPixelFormat(AV_PIX_FMT_YUV420P);
        // grabber.setVideoCodec(avcodec.AV_CODEC_ID_H264);

        try {
            System.out.println("获取 grabber的start:"+grabber);
            grabber.start();
        } catch (Exception e) {
            try {
                System.out.println("获取 grabber的restart:"+grabber);
                grabber.restart();
            } catch (Exception e1) {
                System.out.println("出错了:"+grabber);
                throw e;
            }
        }
        //转换器
        OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
        //抓取一帧视频并将其转换为图像,至于用这个图像用来做什么?加水印,人脸识别等等自行添加
        opencv_core.IplImage grabbedImage = converter.convert(grabber.grab());
        int width = 1024;
        int height = 768;
        if(grabbedImage!=null){
            width = grabbedImage.width();
            height = grabbedImage.height();
        }


        grabber.flush(); 
        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(targetPath, width, height);
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
        recorder.setFormat("rtsp");
        //recorder.setVideoOption("vcodec","copy");
        recorder.setFrameRate(grabber.getFrameRate());
        recorder.setPixelFormat(AV_PIX_FMT_YUV420P); 
        recorder.setOption("thread_queue_size","200");
        //recorder.start(grabber.getFormatContext());
        recorder.start();
        Frame grabframe =null;
        long startTime=0;
        while ((grabframe=grabber.grab()) != null) {
            System.out.println("推流...");
            // frame.showImage(grabframe);
            try
            {
                //grabbedImage = converter.convert(grabframe);
                // Frame rotatedFrame = converter.convert(grabbedImage);
                Frame rotatedFrame = grabframe;


                //时间戳
                // recorder.setTimestamp(1000 * (System.currentTimeMillis() - startTime));

                if(rotatedFrame!=null &&  rotatedFrame.imageHeight > 0 && rotatedFrame.imageWidth > 0){
                    recorder.record(rotatedFrame);
                }
            }catch (FrameRecorder.Exception ex){
                ex.printStackTrace();;
            }
        }

        recorder.stop();
        grabber.stop();
        return null;
    }
————————————————
版权声明:本文为CSDN博主「爱的不落叶」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wangsui99/article/details/115006497