mirror of
https://github.com/langhuihui/monibuca.git
synced 2026-04-23 01:07:03 +08:00
feat: use storage from storage config
This commit is contained in:
@@ -202,6 +202,18 @@ func (s *LocalStorage) CreateFile(ctx context.Context, path string) (File, error
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (s *LocalStorage) OpenFileFromStorageLevel(ctx context.Context, path string, storageLevel int) (File, error) {
|
||||
if storageLevel == 0 {
|
||||
storageLevel = 1
|
||||
}
|
||||
fullPath := filepath.Join(s.GetStoragePath(storageLevel), path)
|
||||
file, err := os.Open(fullPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open file: %w", err)
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (s *LocalStorage) OpenFile(ctx context.Context, path string) (File, error) {
|
||||
// 选择存储路径
|
||||
basePath, err := s.selectStoragePath()
|
||||
|
||||
+3
-4
@@ -125,15 +125,14 @@ func (p *MP4Plugin) downloadSingleFile(stream *m7s.RecordStream, flag mp4.Flag,
|
||||
if isLocalStorage {
|
||||
// 本地存储:根据存储级别获取完整路径后打开文件
|
||||
if localStorage, ok := st.(*storage.LocalStorage); ok {
|
||||
fullPath := localStorage.GetFullPath(stream.FilePath, stream.StorageLevel)
|
||||
file, err = os.Open(fullPath)
|
||||
file, err = localStorage.OpenFileFromStorageLevel(p, stream.FilePath, stream.StorageLevel)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to open local file: %v", err), http.StatusInternalServerError)
|
||||
p.Error("failed to open local file", "err", err, "path", fullPath)
|
||||
p.Error("failed to open local file", "err", err, "path", stream.FilePath, "storageLevel", stream.StorageLevel)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
p.Info("reading file for fmp4 conversion from local storage", "storageLevel", stream.StorageLevel, "path", fullPath)
|
||||
p.Info("reading file for fmp4 conversion from local storage", "storageLevel", stream.StorageLevel, "path", stream.FilePath)
|
||||
} else {
|
||||
// 类型不匹配,使用 OpenFile 作为兜底
|
||||
file, err = st.OpenFile(context.Background(), stream.FilePath)
|
||||
|
||||
@@ -3,8 +3,10 @@ package mp4
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"m7s.live/v5/pkg/storage"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
@@ -21,23 +23,71 @@ type DemuxerRange struct {
|
||||
AudioCodec, VideoCodec codec.ICodecCtx
|
||||
OnAudio, OnVideo func(box.Sample) error
|
||||
OnCodec func(codec.ICodecCtx, codec.ICodecCtx)
|
||||
storage storage.Storage
|
||||
}
|
||||
|
||||
func (d *DemuxerRange) Demux(ctx context.Context) error {
|
||||
var ts, tsOffset int64
|
||||
var audioInitialized, videoInitialized bool
|
||||
st := d.storage
|
||||
var globalStorageType string
|
||||
var file storage.File
|
||||
var err error
|
||||
if st != nil {
|
||||
globalStorageType = st.GetKey()
|
||||
}
|
||||
for _, stream := range d.Streams {
|
||||
// 检查流的时间范围是否在指定范围内
|
||||
if stream.EndTime.Before(d.StartTime) || stream.StartTime.After(d.EndTime) {
|
||||
continue
|
||||
}
|
||||
if filepath.IsAbs(stream.FilePath) {
|
||||
file, err = os.Open(stream.FilePath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
useGlobalStorage := st != nil && globalStorageType == stream.StorageType
|
||||
isLocalStorage := stream.StorageType == string(storage.StorageTypeLocal) || stream.StorageType == ""
|
||||
if useGlobalStorage {
|
||||
if isLocalStorage {
|
||||
if localStorage, ok := st.(*storage.LocalStorage); ok {
|
||||
fullPath := localStorage.GetFullPath(stream.FilePath, stream.StorageLevel)
|
||||
file, err = os.Open(fullPath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
// 类型不匹配,使用 OpenFile 作为兜底
|
||||
file, err = st.OpenFile(ctx, stream.FilePath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
} else {
|
||||
filePath, err := st.GetURL(ctx, stream.FilePath)
|
||||
if err != nil || filePath == "" {
|
||||
continue
|
||||
}
|
||||
file, err = st.OpenFile(ctx, filePath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
} else {
|
||||
file, err = os.Open(stream.FilePath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 保存上一个文件的最后时间戳,用于跨文件连续
|
||||
baseOffset := ts
|
||||
file, err := os.Open(stream.FilePath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
//file, err := os.Open(stream.FilePath)
|
||||
//if err != nil {
|
||||
// continue
|
||||
//}
|
||||
defer file.Close()
|
||||
|
||||
demuxer := NewDemuxer(file)
|
||||
|
||||
@@ -64,6 +64,7 @@ func (p *RecordReader) Run() (err error) {
|
||||
writer.PublishVideoWriter = m7s.NewPublishVideoWriter[*VideoFrame](publisher, allocator)
|
||||
}
|
||||
},
|
||||
storage: pullJob.Plugin.Server.Storage,
|
||||
}
|
||||
demuxerRange.OnAudio = func(a box.Sample) error {
|
||||
if publisher.Paused != nil {
|
||||
|
||||
Reference in New Issue
Block a user