增加文件抽象和文件流过滤

This commit is contained in:
banshan
2021-06-27 14:42:39 +08:00
parent ead7f0ac39
commit 5ee1de64a3
2 changed files with 27 additions and 8 deletions

20
flv.go
View File

@@ -10,7 +10,7 @@ import (
"github.com/Monibuca/utils/v3/codec" "github.com/Monibuca/utils/v3/codec"
) )
func getDuration(file *os.File) uint32 { func getDuration(file FileWr) uint32 {
_, err := file.Seek(-4, io.SeekEnd) _, err := file.Seek(-4, io.SeekEnd)
if err == nil { if err == nil {
var tagSize uint32 var tagSize uint32
@@ -34,12 +34,18 @@ func SaveFlv(streamPath string, append bool) error {
flag = flag | os.O_TRUNC | os.O_WRONLY flag = flag | os.O_TRUNC | os.O_WRONLY
} }
filePath := filepath.Join(config.Path, streamPath+".flv") filePath := filepath.Join(config.Path, streamPath+".flv")
if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil { var file FileWr
return err var err error
} if ExtraConfig.CreateFileFn == nil {
file, err := os.OpenFile(filePath, flag, 0755) if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil {
if err != nil { return err
return err }
file, err = os.OpenFile(filePath, flag, 0755)
if err != nil {
return err
}
}else {
file,err = ExtraConfig.CreateFileFn(filePath)
} }
// return avformat.WriteFLVTag(file, packet) // return avformat.WriteFLVTag(file, packet)
p := Subscriber{ p := Subscriber{

15
main.go
View File

@@ -2,6 +2,7 @@ package record
import ( import (
"encoding/json" "encoding/json"
"io"
"net/http" "net/http"
"os" "os"
"path" "path"
@@ -25,6 +26,18 @@ type FlvFileInfo struct {
Duration uint32 Duration uint32
} }
type FileWr interface {
io.Reader
io.Writer
io.Seeker
io.Closer
}
var ExtraConfig struct {
CreateFileFn func(filename string) (FileWr,error)
AutoRecordFilter func(stream string) bool
}
func init() { func init() {
InstallPlugin(&PluginConfig{ InstallPlugin(&PluginConfig{
Name: "Record", Name: "Record",
@@ -115,7 +128,7 @@ func run() {
func onPublish(v interface{}) { func onPublish(v interface{}) {
p := v.(*Stream) p := v.(*Stream)
if config.AutoRecord { if config.AutoRecord || (ExtraConfig.AutoRecordFilter != nil && ExtraConfig.AutoRecordFilter(p.StreamPath)) {
SaveFlv(p.StreamPath, false) SaveFlv(p.StreamPath, false)
} }
} }