Files
Reisaye/src/main/java/org/reisa/reisaeye/FullVideoPush.java
2025-08-07 02:21:30 +08:00

98 lines
3.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package org.reisa.reisaeye;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class FullVideoPush {
// 服务地址
private static final String SERVER_URL = "http://localhost:8080";
// 本地视频文件
private static final String VIDEO_FILE = "./a.mp4";
public static void main(String[] args) {
try {
// 1. 调用Controller创建流
String streamId = "full-video-" + System.currentTimeMillis();
System.out.println("创建流: " + streamId);
String pushUrl = createStream(streamId);
System.out.println("推流地址: " + pushUrl);
// 2. 推送完整视频(直到视频结束)
System.out.println("开始推送完整视频...");
pushEntireVideo(pushUrl);
System.out.println("视频推送完毕");
// 3. 调用Controller停止流
stopStream(streamId);
System.out.println("流程结束");
} catch (Exception e) {
System.err.println("推送失败: " + e.getMessage());
e.printStackTrace();
}
}
// 调用Controller创建流
private static String createStream(String streamId) throws IOException {
URL url = new URL(SERVER_URL + "/stream/start?streamId=" + streamId);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
// 解析响应获取推流地址
try (BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
String json = br.lines().reduce("", String::concat);
return json.split("\"pushUrl\":\"")[1].split("\"")[0];
} finally {
conn.disconnect();
}
}
// 推送完整视频(直到文件结束)
private static void pushEntireVideo(String rtmpUrl) throws IOException {
// FFmpeg命令推送完整文件不限制时长推完自动结束
String[] cmd = {
"ffmpeg",
"-re", // 按实际帧率推送
"-i", VIDEO_FILE, // 输入视频文件
"-c:v", "libx264", // 视频编码兼容RTMP
"-c:a", "aac", // 音频编码
"-f", "flv", // 输出格式
rtmpUrl // 推流地址
};
Process process = new ProcessBuilder(cmd).redirectErrorStream(true).start();
// 打印FFmpeg日志包含进度
try (BufferedReader br = new BufferedReader(
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println("[FFmpeg] " + line);
}
}
// 等待FFmpeg处理完成视频推完后进程会自动退出
try {
int exitCode = process.waitFor();
if (exitCode != 0) {
throw new RuntimeException("FFmpeg推送异常退出码: " + exitCode);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("推送被中断");
}
}
// 调用Controller停止流
private static void stopStream(String streamId) throws IOException {
URL url = new URL(SERVER_URL + "/stream/stop?streamId=" + streamId);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.getResponseCode(); // 执行请求
conn.disconnect();
}
}