From 0f780c541cc248eea222d2161c79c5bdbad476c8 Mon Sep 17 00:00:00 2001 From: qydysky <32743305+qydysky@users.noreply.github.com> Date: Tue, 14 Feb 2023 23:46:49 +0800 Subject: [PATCH] =?utf8?q?Add=20=E8=87=AA=E5=8A=A8=E7=A7=BB=E9=99=A4?= =?utf8?q?=E5=8E=86=E5=8F=B2=E5=BD=95=E6=92=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Reply/stream.go | 53 +++++++++++++++++++++++++++++++++++++ demo/config/config_K_v.json | 4 +-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/Reply/stream.go b/Reply/stream.go index 8fa3e1b..4f71260 100644 --- a/Reply/stream.go +++ b/Reply/stream.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io" + "io/fs" "net/http" "net/url" "os" @@ -508,6 +509,53 @@ func (t *M4SStream) fetchParseM3U8() (m4s_links []*m4s_link_item, m3u8_addon []b return } +// 移除历史流 +func (t *M4SStream) removeStream() (e error) { + if d, ok := t.common.K_v.LoadV("直播流保存天数").(float64); ok && d >= 1 { + if v, ok := t.common.K_v.LoadV(`直播流保存位置`).(string); ok && v != "" { + type dirEntryDirs []fs.DirEntry + var list dirEntryDirs + f, err := http.Dir(v).Open("/") + if err != nil { + return err + } + defer f.Close() + + if _, err = f.Stat(); err != nil { + return err + } + if d, ok := f.(fs.ReadDirFile); ok { + list, err = d.ReadDir(-1) + } + + if err != nil { + return err + } + + var ( + oldest float64 + oldIndex int = -1 + ) + for i, n := 0, len(list); i < n; i++ { + if list[i].IsDir() && len(list[i].Name()) > 20 { + if tt, err := time.Parse("2006_01_02-15_04_05", list[i].Name()[:19]); err == nil { + if ts := time.Since(tt).Seconds(); ts > d*24*60*60 && ts > oldest { + oldest = ts + oldIndex = i + } + } + } + } + + if oldIndex != -1 { + t.log.L(`I: `, "移除历史流", v+"/"+list[oldIndex].Name()) + return os.RemoveAll(v + "/" + list[oldIndex].Name()) + } + } + } + return nil +} + func (t *M4SStream) saveStream() (e error) { // 设置保存路径 t.Current_save_path = t.config.save_path + "/" + @@ -542,6 +590,11 @@ func (t *M4SStream) saveStream() (e error) { defer t.Callback_stopRec(t) } + // 移除历史流 + if err := t.removeStream(); err != nil { + t.log.L(`W: `, err) + } + // 获取流 switch t.stream_type { case `m3u8`: diff --git a/demo/config/config_K_v.json b/demo/config/config_K_v.json index d131c69..3025900 100644 --- a/demo/config/config_K_v.json +++ b/demo/config/config_K_v.json @@ -54,8 +54,8 @@ "直播流清晰度-help": "清晰度可选-1:不保存 0:默认 20000:4K 10000:原画 400:蓝光 250:超清 150:高清 80:流畅,无提供所选清晰度时,使用低一档清晰度", "直播流清晰度": 10000, "直播流类型-help": "flv,fmp4,flvH,fmp4H,带H后缀的为Hevc格式编码", - "直播流类型": "flv", - "直播流保存位置": "./live", + "直播流保存天数-help": "当t日有1录播时,会尝试删除t-n日的1个最早的录播", + "直播流保存天数": 4, "直播hls流故障转移-help":"true:hls服务器故障时,使用其他", "直播hls流故障转移": true, "直播hls流保存为MP4": true, -- 2.39.2