0
点赞
收藏
分享

微信扫一扫

Java 搭建srs流媒体服务器,并使用ffmpeg推流

互联网码农 2022-04-29 阅读 197

前言:

最近项目需要把监控摄像机的RTSP流在放在浏览器上播放,但由于目前浏览器并不支持RTSP,需要转码才行。之前试过nginx作为流媒体服务器,这次尝试使用ffmpeg将码流推送srs流媒体服务器,实现浏览器端播放,特此记录一下。

SRS 是一个简单高效的实时视频服务器,支持RTMP/WebRTC/HLS/HTTP-FLV/SRT

源码地址:

githup: https://github.com/ossrs/srs

gitee:  https://gitee.com/ossrs/srs

1.在Liunx上编译安装SRS流媒体服务器

 

//先把分支4.0release clone下来
git clone -b 4.0release https://gitee.com/ossrs/srs.git
//然后编译
cd srs/trunk
./configure && make

使用配置文件启动(这样启动需要在目录trunk中才行)

#使用配置文件srs.conf启动
./objs/srs -c conf/srs.conf

# 查看SRS的状态
./etc/init.d/srs status

#停止SRS
./etc/init.d/srs stop

#重启SRS
./etc/init.d/srs restart

# 或者看SRS的日志
tail -n 30 -f ./objs/srs.log

打开浏览器可以访问(我这个服务器的ip是192.168.13.170),有个控制台页面

http://192.168.13.170:8080/

 2.Java使用ffmpeg推流

首先需要一个ffmpeg程序才行,没有的可以去ffmpeg官网下载

package com.ffmpeg.service;

import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.util.IdUtil;
import lombok.SneakyThrows;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class FFmpegUtil {

    {
        /** ffmpeg 命令详解
         *  ffmpeg [[options][`-i’ input_file]] {[options] output_file}
         *  options:
         *   -i: url 输入文件网址
         *   -vcodec: 设置视频编解码器 -codec:v  -c:v
         *   (强制使用codec编解码方式,如-vcodec xvid 使用xvid压缩 如果用copy表示原始编解码数据必须被拷贝)
         *   -acodec: 设置音频编解码器 -codec:a  -c:a
         *   -f: fmt 强制输入或输出文件格式
         */
    }


    @SneakyThrows
    public static void srs(){
        String osName = System.getProperties().getProperty("os.name");
        System.out.println(osName);
        ClassPathResource classPathResource = new ClassPathResource("ffmpeg"+ File.separator+"win64"+ File.separator);
        String basePath = classPathResource.getAbsolutePath();
        String myStream = IdUtil.simpleUUID();
        String command = basePath + "ffmpeg -re " +
                "-rtsp_transport tcp " +
                "-i rtsp://admin:123456@192.168.13.199:554 " +
                "-c:v copy -c:a copy " +
                "-f flv rtmp://192.168.13.170/live/"+myStream;
        System.out.println("rtmp://192.168.13.170/live/"+myStream);
        Process process = Runtime.getRuntime().exec(command);
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        String line = null;
        while((line = br.readLine()) != null) {
            System.out.println("视频推流信息[" + line + "]");
        }
    }

    public static void main(String[] args) {
        srs();
    }

}

 启动

可以进入浏览器控制台查看对应的视频了

 

 要在web浏览器上播放,可以进入预览页面查看保存对应的js代码

附上SRS的Wiki

举报

相关推荐

搭建SRS视频服务器

0 条评论