不需要在本地或容器内安装ffmpeg,就直接引入依赖即可,实测在本地,linux上都正常使用
引入pom依赖
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-all-deps</artifactId>
<version>3.3.1</version>
<exclusions>
<!-- 排除windows 32位系统 -->
<exclusion>
<groupId>ws.schild</groupId>
<artifactId>jave-nativebin-win32</artifactId>
</exclusion>
<!-- 排除linux 32位系统 -->
<exclusion>
<groupId>ws.schild</groupId>
<artifactId>jave-nativebin-linux32</artifactId>
</exclusion>
<!-- 排除Mac系统-->
<exclusion>
<groupId>ws.schild</groupId>
<artifactId>jave-nativebin-osx64</artifactId>
</exclusion>
<exclusion>
<groupId>ws.schild</groupId>
<artifactId>jave-nativebin-osxm1</artifactId>
</exclusion>
<exclusion>
<groupId>ws.schild</groupId>
<artifactId>jave-nativebin-linux-arm32</artifactId>
</exclusion>
</exclusions>
</dependency>
接口调用
package com.example.rtsp.demos.test1;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ws.schild.jave.process.ProcessWrapper;
import ws.schild.jave.process.ffmpeg.DefaultFFMPEGLocator;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@Slf4j
@RestController
@RequestMapping("/a")
public class Acontroller {
private final Map<String, ProcessWrapper> map = new HashMap<>();
/**
* @param url rtsp://127.0.0.1:8554/a
* @param out D:\\opt\\a.mp4
* @return
*/
@GetMapping(value = "/start")
public String Start(@RequestParam String url,
@RequestParam String out) {
ProcessWrapper ffmpeg = new DefaultFFMPEGLocator().createExecutor();
ffmpeg.addArgument("-rtsp_transport");
ffmpeg.addArgument("tcp");
ffmpeg.addArgument("-y");
ffmpeg.addArgument("-i");
ffmpeg.addArgument(url);
ffmpeg.addArgument("-c");
ffmpeg.addArgument("copy");
ffmpeg.addArgument("-f");
ffmpeg.addArgument("mp4");
ffmpeg.addArgument(out);
String uuid = UUID.randomUUID().toString();
new Thread(() -> {
try {
ffmpeg.execute();
log.info("任务:{},启动成功!", uuid);
try (BufferedReader br = new BufferedReader(new InputStreamReader(ffmpeg.getErrorStream()))) {
blockFfmpeg(br);
}
} catch (IOException ignored) {
} finally {
ffmpeg.close();
}
}).start();
map.put(uuid, ffmpeg);
return uuid;
}
/**
* @param id uuid
* @return
*/
@GetMapping(value = "/stop")
public synchronized String stop(@RequestParam String id) {
ProcessWrapper ffmpeg = map.get(id);
if (ffmpeg != null) {
OutputStream outputStream = ffmpeg.getOutputStream();
if (outputStream != null) {
try {
outputStream.write("q".getBytes());
// 一定要刷新
outputStream.flush();
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
ffmpeg.close();
map.remove(id);
log.info("任务:{},结束!", id);
}
return "ok";
}
@GetMapping(value = "/map")
public Object map() {
return map.keySet();
}
/**
* 等待命令执行成功,退出
*
* @param br
* @throws IOException
*/
private static void blockFfmpeg(BufferedReader br) throws IOException {
String line;
// 该方法阻塞线程,直至合成成功
while ((line = br.readLine()) != null) {
doNothing(line);
}
}
/**
* 打印日志,调试阶段可解开注释,观察执行情况
*
* @param line
*/
private static void doNothing(String line) {
log.info(line);
}
}