javacv rtsp rtmp

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;

引入这些开发包实现转码跟推流服务
 

Java code?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

static boolean exit  = false;

    public static void main(String[] args) throws Exception {

        System.out.println("start...");

        String rtmpPath = "rtmp://111.230.***.***:1936/hls/YFZX2";

        String rtspPath = "rtsp://admin:admin88888@192.168.100.200:554/h264/ch34/sub/av_stream";

         

        int audioRecord =0// 0 = 不录制,1=录制

        boolean saveVideo = false;

        push(rtmpPath,rtspPath,audioRecord,saveVideo);

         

        System.out.println("end...");

    }

  

    public static void push(String rtmpPath,String rtspPath,int audioRecord,boolean saveVideo ) throws Exception  {

        // 使用rtsp的时候需要使用 FFmpegFrameGrabber,不能再用 FrameGrabber

        int width = 640,height = 480;

        FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(rtspPath); 

        grabber.setOption("rtsp_transport""tcp"); // 使用tcp的方式,不然会丢包很严重

         

        grabber.setImageWidth(width);

        grabber.setImageHeight(height);

        System.out.println("grabber start");

        grabber.start();

        // 流媒体输出地址,分辨率(长,高),是否录制音频(0:不录制/1:录制)

        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(rtmpPath,width,height, audioRecord);

        recorder.setInterleaved(true);

        //recorder.setVideoOption("crf","28");

        recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264); // 28

        recorder.setFormat("flv"); // rtmp的类型

        recorder.setFrameRate(25);

        recorder.setImageWidth(width);recorder.setImageHeight(height);

         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();

    }



[swscaler @ 0x7fd84fd04c00] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 0x7fd851513600] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 0x7fd8500cfa00] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 0x7fd851513600] deprecated pixel format used, make sure you did set range correctly

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值