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

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"
)
func getDuration(file *os.File) uint32 {
func getDuration(file FileWr) uint32 {
_, err := file.Seek(-4, io.SeekEnd)
if err == nil {
var tagSize uint32
@@ -34,12 +34,18 @@ func SaveFlv(streamPath string, append bool) error {
flag = flag | os.O_TRUNC | os.O_WRONLY
}
filePath := filepath.Join(config.Path, streamPath+".flv")
if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil {
return err
}
file, err := os.OpenFile(filePath, flag, 0755)
if err != nil {
return err
var file FileWr
var err error
if ExtraConfig.CreateFileFn == nil {
if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil {
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)
p := Subscriber{

15
main.go
View File

@@ -2,6 +2,7 @@ package record
import (
"encoding/json"
"io"
"net/http"
"os"
"path"
@@ -25,6 +26,18 @@ type FlvFileInfo struct {
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() {
InstallPlugin(&PluginConfig{
Name: "Record",
@@ -115,7 +128,7 @@ func run() {
func onPublish(v interface{}) {
p := v.(*Stream)
if config.AutoRecord {
if config.AutoRecord || (ExtraConfig.AutoRecordFilter != nil && ExtraConfig.AutoRecordFilter(p.StreamPath)) {
SaveFlv(p.StreamPath, false)
}
}