4、Java调用FFmpeg推流到SRS

4.1、搭建环境

4.1.1、IDEA创建SpringBoot工程

4.1.2、引入依赖

        <!-- hutool-all -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.5</version>
        </dependency>

4.1.3、在resources下新建ffmpeg文件夹,并且创建win,并将windows下的ffmpeg.exe放入其中

4.1.3、application.yml配置

# 应用名称
spring:
  application:
    name: java-push-stream-by-ffmpeg

# 应用服务 WEB 访问端口
server:
  port: 8989

#=================================================
#源地址和目标地址
ae:
  url:
    sourceAddress: rtsp://admin:zd199611@192.168.0.109:554/h264/1/main/av_stream
    targetAddress: rtmp://192.168.145.201:1935/live/livestream
  resources:
    ffmpegPath: ffmpeg

4.1.4、编写推流代码

package com.ae.pushstream;

import cn.hutool.core.io.resource.ClassPathResource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * ffmpeg推流到SRS
 * @author AldrichEugene
 * @since 2018-07-15
 */
@Component
public class PushCameraStreamByFFmpeg implements ApplicationRunner {

    @Value("${ae.url.sourceAddress}")
    private String sourceAddress;

    @Value("${ae.url.targetAddress}")
    private String targetAddress;

    @Value("${ae.resources.ffmpegPath}")
    private String ffmpegPath;

    ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
            2,
            5,
            60,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(3),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.DiscardPolicy());

    /**
     * springboot容器启动之后执行
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) {
        threadPool.execute(() -> {
            startPushCameraStream(sourceAddress,targetAddress);
        });
    }

    /**
     * 推流
     * 完整地址 ffmpeg -re -i rtsp://admin:zd199611@192.168.0.109:554/h264/1/main/av_stream -vcodec copy -acodec copy -f flv -y rtmp://192.168.145.201:1935/live/livestream
     * 播放地址 http://192.168.145.201:8080/live/livestream.flv
     * @param sourceAddress 摄像头地址 rtsp://admin:zd199611@192.168.0.109:554/h264/1/main/av_stream
     * @param targetAddress SRS地址   rtmp://192.168.145.201:1935/live/livestream
     */
    public void startPushCameraStream(String sourceAddress, String targetAddress){
        try {
            String path = getFFmpegPath();
            String command = path + "ffmpeg -re -i " + sourceAddress + " -vcodec copy -acodec copy -f flv -y " + targetAddress;
            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 + "]");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取ffmpeg.exe的路径
     * @return
     */
    private String getFFmpegPath() throws IOException {
        String os = null;
        String osName = System.getProperties().getProperty("os.name");
        System.out.println("current system :"+osName);
        if(osName.contains("Windows")){
            os = "win";
        }else if(osName.contains("Linux")){
            os = "linux";
        }else {
            throw new RuntimeException("This operating system is not supported!");
        }
        if(os == null){
            throw new RuntimeException("ffmpeg.exe path not found!");
        }

        ClassPathResource classPathResource = new ClassPathResource(ffmpegPath + File.separator + os + File.separator);
        String path = classPathResource.getAbsolutePath();
        return path;
    }
}

4.1.5、启动并推流

(1)启动SRS

(2)运行代码

(3)使用VLC播放

http://192.168.145.201:8080/live/livestream.flv

4.2、源码地址

GitHub:https://github.com/AldirchEugene

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

了凡啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值