rtsp向rtmp推流

package com.awifi.video.media.test;

import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.OpenCVFrameConverter;

/**
 * 版权所有: 爱WiFi无线运营中心
 * 创建日期: 2018/9/27 9:15
 * 创建作者:hw
 * 文件名称:mediaStream
 * 版本:  v1.0
 * 功能: rtsp向rtmp推流
 * 修改记录:
 */
public class TestRTMPGrabberRecorder {
    static boolean exit  = false;
    public static void main(String[] args) throws Exception {
        System.out.println("start...");
        String rtmpPath = "rtmp://192.168.1.91:1935/hls/demo";
//        String rtmpPath = "udp://@192.168.1.110:1234";

        //String rtspPath = "rtmp://live.hkstv.hk.lxdns.com/live/hks"; // 香港收视
        //String rtspPath = "rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov";
        String rtspPath = "rtsp://admin:[email protected]:554";
//        String rtspPath = "C:\\Users\\Administrator\\Desktop\\test2.h264";

        //ffmpeg -f rtsp -rtsp_transport tcp -i rtsp://admin:[email protected]:554/h264/ch1/main/av_stream rtmp://casic207-pc1/live360p/ss1
        // ffmpeg -i  rtsp://admin:[email protected]:554/h264/ch1/main/av_stream -vcodec copy -acodec copy -f flv rtmp://casic207-pc1/live360p/ss1
        int audioRecord =0; // 0 = 不录制,1=录制
        boolean saveVideo = false;
        test(rtmpPath,rtspPath,audioRecord,saveVideo);
        System.out.println("end...");
    }

    public static void test(String rtmpPath,String rtspPath,int audioRecord,boolean saveVideo ) throws Exception  {
        //FrameGrabber grabber = FrameGrabber.createDefault(0); // 本机摄像头 默认
        // 使用rtsp的时候需要使用 FFmpegFrameGrabber,不能再用 FrameGrabber
        int width = 640,height = 480;
        FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(rtspPath);
//        grabber.setOption("rtsp_transport", "tcp"); // 使用tcp的方式,不然会丢包很严重
        // 一直报错的原因!!!就是因为是 2560 * 1440的太大了。。
        grabber.setImageWidth(width);
        grabber.setImageHeight(height);
        System.out.println("grabber start");
        grabber.start();
        //FrameRecorder recorder = FrameRecorder.createDefault(rtmpPath, 640,480,0);
        // 流媒体输出地址,分辨率(长,高),是否录制音频(0:不录制/1:录制)
        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(rtmpPath, 1280, 720,audioRecord);
        recorder.setInterleaved(true);
        recorder.setVideoOption("crf","28");
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264); // 28
        recorder.setFormat("flv"); // rtmp的类型
        recorder.setFrameRate(25);
        recorder.setPixelFormat(0); // yuv420p
        System.out.println("recorder start");
        recorder.start();
        //
        OpenCVFrameConverter.ToIplImage conveter = new OpenCVFrameConverter.ToIplImage();
        System.out.println("all start!!");
        int count = 0;
        while(!exit){
            count++;
            Frame frame = grabber.grabImage();
            if(frame == null){
                continue;
            }
            if(count % 100 == 0){
                System.out.println("count="+count);
            }
            recorder.record(frame);
        }
        grabber.stop();
        grabber.release();
        recorder.stop();
        recorder.release();
    }
}

 

转载于:https://www.cnblogs.com/weihuang6620/p/11024630.html

你可能感兴趣的:(rtsp向rtmp推流)