Skip to content

Commit 4f4bff9

Browse files
committed
feat: per-stream resolution config with global fallback
1 parent 25af002 commit 4f4bff9

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

config.example.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ rtsp:
88
streams:
99
- name: "cam1"
1010
url: "rtsp://admin:password@192.168.1.100:554/ch1"
11+
# resolution: "1920x1080" # 可選:此串流的輸出解析度(留空用全域設定)
1112
- name: "cam2"
1213
url: "rtsp://admin:password@192.168.1.100:554/ch2"
14+
resolution: "1280x720" # 此串流強制縮放至 720p
1315
# 可自由新增更多串流:
1416
# - name: "cam3"
1517
# url: "rtsp://10.8.60.55:8553/cam3"
18+
# resolution: "original" # 保持原始解析度
1619
segment_duration: 600 # 每段錄製時長(秒)
1720

1821
# ============================================================================
@@ -27,6 +30,7 @@ schedule:
2730
# ============================================================================
2831
output:
2932
dir: "~/rtsp-recorder/videos" # 輸出目錄
33+
resolution: "1920x1080" # 全域預設解析度(個別串流未設定時使用)
3034

3135
# ============================================================================
3236
# GCS 上傳設定

rust/crates/recorder/src/recorder.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ async fn background_converter(
194194
stats: Arc<Mutex<Stats>>,
195195
mut shutdown: broadcast::Receiver<()>,
196196
ff: FfmpegPaths,
197-
resolution: String,
197+
streams: Vec<StreamConfig>,
198+
default_resolution: String,
198199
) {
199200
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(30));
200201

@@ -212,6 +213,16 @@ async fn background_converter(
212213
use std::os::unix::fs::PermissionsExt;
213214
let _ = std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644));
214215
}
216+
// 從檔名取得 stream name,查找對應的解析度設定
217+
let resolution = path.file_stem()
218+
.and_then(|s| s.to_str())
219+
.and_then(|name| name.split('_').next())
220+
.and_then(|stream_name| {
221+
streams.iter()
222+
.find(|s| s.name == stream_name)
223+
.and_then(|s| s.resolution.clone())
224+
})
225+
.unwrap_or_else(|| default_resolution.clone());
215226
// 檢查檔案是否正在寫入(修改時間 > 10 秒前)
216227
if let Ok(metadata) = path.metadata() {
217228
if let Ok(modified) = metadata.modified() {
@@ -258,9 +269,10 @@ pub async fn run_daemon(config: &Config, stats: Arc<Mutex<Stats>>, ff: &FfmpegPa
258269
let converter_shutdown = shutdown_tx.subscribe();
259270
let output_dir = config.output.dir.clone();
260271
let converter_ff = ff.clone();
261-
let converter_resolution = config.output.resolution.clone();
272+
let converter_streams = config.rtsp.streams.clone();
273+
let converter_default_resolution = config.output.resolution.clone();
262274
tokio::spawn(async move {
263-
background_converter(output_dir, converter_stats, converter_shutdown, converter_ff, converter_resolution).await;
275+
background_converter(output_dir, converter_stats, converter_shutdown, converter_ff, converter_streams, converter_default_resolution).await;
264276
tracing::error!("[轉檔] 背景任務意外結束");
265277
});
266278

rust/crates/shared/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ pub struct RtspConfig {
2626
pub struct StreamConfig {
2727
pub name: String,
2828
pub url: String,
29+
/// 此串流的輸出解析度(可選,留空使用全域設定)
30+
#[serde(default)]
31+
pub resolution: Option<String>,
2932
}
3033

3134
#[derive(Debug, Clone, Deserialize)]

0 commit comments

Comments
 (0)