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; /** * Copyright: love WiFi wireless operations center * Creation Date: 2018/9/27 9:15 * Create Author: hw * File name: mediaStream * Version: v1.0 * Function: rtsp rtmp to plug flow * Records: */ 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 (rtmpPath String, String rtspPath, int AudioRecord, Boolean saveVideo) throws Exception { // framegrabber = FrameGrabber.createDefault in Glover (0); // native camera default // use when required rtsp FFmpegFrameGrabber, can not with framegrabber int width = 640, height = 480 ; Grabber FFmpegFrameGrabber = FFmpegFrameGrabber.createDefault (rtspPath); // grabber.setOption ( "rtsp_transport", "tcp"); // use tcp way, or will be a very serious loss // reason has been given of! ! ! That is because it is too big of a 2560 * 1440. . grabber.setImageWidth (width); grabber.setImageHeight(height); System.out.println("grabber start"); grabber.start(); // FrameRecorder = FrameRecorder.createDefault Recorder (rtmpPath, 640,480,0); // streaming output address resolution (length, height), whether the recorded audio (0: not record / 1: record) FFmpegFrameRecorder Recorder = new 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(); } }