java调用ffmpeg拉流录制转存

不需要在本地或容器内安装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);
    }


}

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

啥也不会lll

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

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

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

打赏作者

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

抵扣说明:

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

余额充值