2
liusuyi
2026-05-12 9b5c9db9189493fbc6db48f55a7b7311b2a3253c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.ard.work.sdk.zlxd.record;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
@Slf4j
@Component
public class RtspToMP4 {
 
    private Process process;
    private Thread logThread;
    private volatile boolean running = false;
 
    /**
     * 开始录制
     *
     * @param streamUrl rtsp地址
     * @param filePath 输出路径
     * @param maxFileSizeBytes 最大文件大小(可传null)
     * @param maxDurationSec 最大录制秒(可传null)
     */
    public synchronized void start(String streamUrl, String filePath, Long maxFileSizeBytes, Integer maxDurationSec) {
 
        if (running) {
            log.warn("录制已在运行中");
            return;
        }
 
        try {
            List<String> command = new ArrayList<>();
 
            command.add("ffmpeg");
            command.add("-rtsp_transport");
            command.add("tcp");
            command.add("-y");
            command.add("-i");
            command.add(streamUrl);
 
            // 视频直接copy(CPU最低)
            command.add("-c:v");
            command.add("copy");
 
            // 禁止音频(避免pcm_mulaw问题)
            command.add("-an");
 
            // 最大文件大小限制
            if (maxFileSizeBytes != null) {
                command.add("-fs");
                command.add(String.valueOf(maxFileSizeBytes));
            }
 
            // 最大时长限制
            if (maxDurationSec != null) {
                command.add("-t");
                command.add(String.valueOf(maxDurationSec));
            }
 
            // MP4优化(避免损坏)
            command.add("-movflags");
            command.add("faststart");
 
            command.add(filePath);
 
            log.info("启动FFmpeg:{}", command);
 
            ProcessBuilder pb = new ProcessBuilder(command);
            pb.redirectErrorStream(true);
 
            process = pb.start();
            running = true;
 
            startLogThread(process.getInputStream());
 
            // 等待进程结束(异步)
            new Thread(() -> {
                try {
                    int exitCode = process.waitFor();
                    log.info("FFmpeg退出,code={}", exitCode);
                } catch (Exception e) {
                    log.error("等待进程异常", e);
                } finally {
                    running = false;
                    process = null;
                }
            }).start();
 
        } catch (Exception e) {
            running = false;
            log.error("启动录制失败", e);
        }
    }
 
    /**
     * 停止录制(优雅停止)
     */
    public synchronized void stop() {
 
        if (!running || process == null) {
            log.warn("录制未运行");
            return;
        }
 
        log.info("开始停止录制...");
 
        try {
            // 发送q优雅退出
            OutputStream os = process.getOutputStream();
            os.write('q');
            os.flush();
            os.close();
 
            // 等待3秒
            if (!process.waitFor(3, TimeUnit.SECONDS)) {
                log.warn("FFmpeg未退出,强制关闭");
                process.destroy();
 
                if (!process.waitFor(1, TimeUnit.SECONDS)) {
                    process.destroyForcibly();
                }
            }
 
        } catch (Exception e) {
            log.error("停止录制异常", e);
            process.destroyForcibly();
        } finally {
            running = false;
            process = null;
        }
 
        // 停止日志线程
        if (logThread != null && logThread.isAlive()) {
            logThread.interrupt();
        }
 
        log.info("录制已停止");
    }
 
    /**
     * 是否运行
     */
    public boolean isRunning() {
        return running;
    }
 
    /**
     * 启动日志线程
     */
    private void startLogThread(InputStream inputStream) {
 
        logThread = new Thread(() -> {
            try (BufferedReader br =
                         new BufferedReader(new InputStreamReader(inputStream))) {
 
                String line;
                while ((line = br.readLine()) != null && running) {
                    log.debug("FFmpeg: {}", line);
                }
 
            } catch (Exception e) {
                if (running) {
                    log.error("日志线程异常", e);
                }
            }
        });
 
        logThread.setDaemon(true);
        logThread.start();
    }
}