一、创建SpringBoot工程
二、引入依赖
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.11</version>
</dependency>
三、application.yml配置
srs:
# 目标地址
targetAddress: rtmp://localhost/live/livestream
resources:
# ffmpeg目录名
ffmpegPath: ffmpeg
四、集成ffmpeg工具
在resources目录下新建ffmpeg文件夹,并且创建win文件夹,把ffmpeg.exe等工具放到里面。
五、编写推流代码
package com.example.ffmpeg.util;
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("${srs.targetAddress}")
private String targetAddress;
@Value("${srs.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(targetAddress);
});
}
/**
* 推流
* 完整地址 ffmpeg -re -i source.flv -c copy -f flv rtmp://localhost/live/livestream
* 播放地址 http://localhost:8080/live/livestream.flv
*
* @param targetAddress SRS地址 rtmp://localhost/live/livestream
*/
public void startPushCameraStream(String targetAddress) {
try {
String path = getFFmpegPath();
// 视频来源于绝对路径 E:\m4a\source.flv
String command = path + "ffmpeg -re -i E:\\m4a\\source.flv -c copy -f flv " + 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);
return classPathResource.getAbsolutePath();
}
}
六、启动SRS服务器和启动应用程序
启动SRS服务,我们上一张节已经实现。https://blog.51cto.com/u_13312531/6675258
然后我们在SRS里使用播放器播放。