feat: 视频

视频初步搭建
This commit is contained in:
ovo 2024-12-08 18:55:52 +08:00
parent 0a79f9cc23
commit 330acd2d79
3 changed files with 120 additions and 4 deletions

View File

@ -0,0 +1,83 @@
package com.guwan.backend.client;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Slf4j
@Component
@RequiredArgsConstructor
public class SrsClient {
private final RestTemplate restTemplate;
@Value("${srs.server.url}")
private String srsServerUrl;
/**
* 开始录制
*/
public void startRecord(String streamKey, RecordConfig config) {
String url = String.format("%s/api/v1/streams/%s/recording/start", srsServerUrl, streamKey);
try {
ResponseEntity<String> response = restTemplate.postForEntity(url, config, String.class);
if (!response.getStatusCode().is2xxSuccessful()) {
throw new RuntimeException("开始录制失败: " + response.getBody());
}
} catch (Exception e) {
log.error("开始录制失败", e);
throw new RuntimeException("开始录制失败", e);
}
}
/**
* 停止录制
*/
public void stopRecord(String streamKey) {
String url = String.format("%s/api/v1/streams/%s/recording/stop", srsServerUrl, streamKey);
try {
ResponseEntity<String> response = restTemplate.postForEntity(url, null, String.class);
if (!response.getStatusCode().is2xxSuccessful()) {
throw new RuntimeException("停止录制失败: " + response.getBody());
}
} catch (Exception e) {
log.error("停止录制失败", e);
throw new RuntimeException("停止录制失败", e);
}
}
/**
* 获取流信息
*/
public StreamInfo getStreamInfo(String streamKey) {
String url = String.format("%s/api/v1/streams/%s", srsServerUrl, streamKey);
try {
ResponseEntity<StreamInfo> response = restTemplate.getForEntity(url, StreamInfo.class);
return response.getBody();
} catch (Exception e) {
log.error("获取流信息失败", e);
throw new RuntimeException("获取流信息失败", e);
}
}
@Data
public static class RecordConfig {
private String format = "flv"; // 录制格式
private String filePath; // 文件路径
}
@Data
public static class StreamInfo {
private String streamId; // 流ID
private String clientId; // 客户端ID
private String ip; // 客户端IP
private Long startTime; // 开始时间
private String status; // 状态
private Long bytesIn; // 输入字节数
private Long bytesOut; // 输出字节数
}
}

View File

@ -66,9 +66,9 @@ public class LiveService {
* 开始录制
*/
private void startRecording(String streamKey) {
srsClient.startRecord(streamKey, new RecordConfig()
.setFormat("flv")
.setFilePath("/recordings/" + streamKey + ".flv"));
SrsClient.RecordConfig config = new SrsClient.RecordConfig();
config.setFilePath("/recordings/" + streamKey + ".flv");
srsClient.startRecord(streamKey, config);
}
/**
@ -77,4 +77,32 @@ public class LiveService {
private void stopRecording(String streamKey) {
srsClient.stopRecord(streamKey);
}
/**
* 生成推流密钥
*/
private String generateStreamKey() {
return UUID.randomUUID().toString();
}
/**
* 生成推流地址
*/
private String generateRtmpUrl(String streamKey) {
return "rtmp://localhost:1935/live/" + streamKey;
}
/**
* 生成播放地址
*/
private String generateHlsUrl(String streamKey) {
return "http://localhost:8088/live/" + streamKey + ".m3u8";
}
/**
* 生成回放地址
*/
private String generateReplayUrl(String streamKey) {
return "http://localhost:8080/recordings/" + streamKey + ".flv";
}
}

View File

@ -124,4 +124,9 @@ config:
detect-pool-size: 16
compare-pool-size: 16
rec-face-thd: 0.8
rec-id-thd: 0.5
rec-id-thd: 0.5
# SRS配置
srs:
server:
url: http://localhost:1985 # SRS HTTP API地址