record

package module
v4.9.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2025 License: MIT Imports: 40 Imported by: 0

README

RECORD插件

对流进行录制的功能插件,提供Flv、fmp4、hls、裸流格式的录制功能。

插件地址

https://github.com/Monibuca/plugin-record

插件引入

import (
    _ "m7s.live/plugin/record/v4"
)

配置

  • 配置中的path 表示要保存的文件的根路径,可以使用相对路径或者绝对路径
  • filter 代表要过滤的StreamPath正则表达式,如果不匹配,则表示不录制。为空代表不进行过滤
  • fragment表示分片大小(20s代表20秒,1m代表1分钟,可以组合),0代表不分片
record:
  subscribe: # 参考全局配置格式
  flv:
      ext: .flv
      path: record/flv
      autorecord: false
      filter: ""
      fragment: 0
  mp4:
      ext: .mp4
      path: record/mp4
      autorecord: false
      filter: ""
      fragment: 0
  hls:
      ext: .m3u8
      path: record/hls
      autorecord: false
      filter: ""
      fragment: 0
  raw:
      ext: .
      path: record/raw
      autorecord: false
      filter: ""
      fragment: 0

API

  • /record/api/list/recording 罗列所有正在录制中的流的信息
  • /record/api/list?type=[flv|mp4|hls|raw] 罗列所有录制的flv|mp4|m3u8|raw文件
  • /record/api/start?type=flv&streamPath=live/rtc&fileName=xxx&fragment=10s 开始录制某个流,返回一个字符串用于停止录制用的id(fileName是可选的,且只用于非切片情况,fragment用于覆盖配置中的切片时间,是可选的,如果fileName和fragment都存在,则忽略fileName)
  • /record/api/stop?id=xxx 停止录制某个流

点播功能

访问格式: [http/https]://[host]:[port]/record/[streamPath].[flv|mp4|m3u8|h264|h265]

例如:

  • http://localhost:8080/record/live/test.flv 将会读取对应的flv文件
  • http://localhost:8080/record/live/test.mp4 将会读取对应的fmp4文件

// GO仓库刷新INDEX

GOPROXY=proxy.golang.org go list -m github.com/eanfs/plugin-record/v4@v4.9.2

GOPROXY=proxy.golang.org go list -m github.com/eanfs/plugin-transform/v1@v1.0.0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRecordExist = errors.New("recorder exist")
View Source
var RecordPluginConfig = &RecordConfig{
	Flv: Record{
		Path:          "record/flv",
		Ext:           ".flv",
		GetDurationFn: getFLVDuration,
	},
	Fmp4: Record{
		Path: "record/fmp4",
		Ext:  ".mp4",
	},
	Mp4: Record{
		Path: "record/mp4",
		Ext:  ".mp4",
	},
	Hls: Record{
		Path: "record/hls",
		Ext:  ".m3u8",
	},
	Raw: Record{
		Path: "record/raw",
		Ext:  ".",
	},
	RawAudio: Record{
		Path: "record/raw",
		Ext:  ".",
	},
	beforeDuration:              30,
	afterDuration:               30,
	MysqlDSN:                    "",
	ExceptionPostUrl:            "http://www.163.com",
	SqliteDbPath:                "./m7sv4.db",
	DiskMaxPercent:              80.00,
	LocalIp:                     getLocalIP(),
	RecordFileExpireDays:        0,
	RecordPathNotShowStreamPath: true,
}
View Source
var WritingFiles sync.Map

Functions

func SendToThirdPartyAPI added in v4.8.0

func SendToThirdPartyAPI(exception *Exception)

向第三方发送异常报警

Types

type EventRecord added in v4.8.0

type EventRecord struct {
	Id             uint   `json:"id" desc:"自增长id" gorm:"primaryKey;autoIncrement"`
	StreamPath     string `json:"streamPath" desc:"流路径" gorm:"type:varchar(255);comment:流路径"`
	RecId          string `json:"RecId" desc:"录制编号" gorm:"type:varchar(255);comment:录制编号"`
	RecordMode     string `` /* 171-byte string literal not displayed */
	EventName      string `json:"eventName" desc:"事件名称" gorm:"type:varchar(255);comment:事件名称"`
	BeforeDuration string `json:"beforeDuration" desc:"事件前缓存时长" gorm:"type:varchar(255);comment:事件前缓存时长"`
	AfterDuration  string `json:"afterDuration" desc:"事件后缓存时长" gorm:"type:varchar(255);comment:事件后缓存时长"`
	CreateTime     string `json:"createTime" desc:"录像时间" gorm:"type:varchar(255);comment:录像时间"`
	StartTime      string `json:"startTime" desc:"录像开始时间" gorm:"type:varchar(255);comment:录像开始时间"`
	EndTime        string `json:"endTime" desc:"录像结束时间" gorm:"type:varchar(255);comment:录像结束时间"`
	Filepath       string `json:"filePath" desc:"录像文件物理路径" gorm:"type:varchar(255);comment:录像文件物理路径"`
	Urlpath        string `json:"urlPath" desc:"录像文件下载URL路径" gorm:"type:varchar(255);comment:录像文件下载URL路径"`
	IsDelete       string `` /* 177-byte string literal not displayed */
	UserId         string `json:"useId" desc:"用户id" gorm:"type:varchar(255);comment:用户id"`
	Filename       string `json:"fileName" desc:"文件名" gorm:"type:varchar(255);comment:文件名"`
	Fragment       string `json:"fragment" desc:"切片大小" gorm:"type:varchar(255);comment:切片大小;default:'0'"`
	EventDesc      string `json:"eventDesc" desc:"事件描述" gorm:"type:varchar(255);comment:事件描述"`
	Type           string `json:"type" desc:"录像文件类型" gorm:"type:varchar(255);comment:录像文件类型,flv,mp4,raw,fmp4,hls"`
	EventLevel     string `` /* 223-byte string literal not displayed */
}

mysql数据库eventrecord表

type Exception added in v4.8.0

type Exception struct {
	CreateTime string `json:"createTime" gorm:"type:varchar(50)"`
	AlarmType  string `json:"alarmType" gorm:"type:varchar(50)"`
	AlarmDesc  string `json:"alarmDesc" gorm:"type:varchar(50)"`
	ServerIP   string `json:"serverIP" gorm:"type:varchar(50)"`
	StreamPath string `json:"streamPath" gorm:"type:varchar(50)"`
	FileName   string `json:"fileName" gorm:"type:varchar(100)"`
}

mysql数据库里Exception 定义异常结构体

type FLVKeyframe added in v4.8.0

type FLVKeyframe struct {
	FLVFileName  string    `gorm:"not null"`
	FrameOffset  int64     `gorm:"not null"`
	FrameAbstime uint32    `gorm:"not null"`
	CreatedAt    time.Time `gorm:"autoCreateTime"`
}

sqlite数据库用来存放每个flv文件的关键帧对应的offset及abstime数据

type FLVRecorder

type FLVRecorder struct {
	Recorder

	Offset int64

	RecordMode
	// contains filtered or unexported fields
}

func NewFLVRecorder

func NewFLVRecorder(mode RecordMode) (r *FLVRecorder)

func (*FLVRecorder) Close

func (r *FLVRecorder) Close() (err error)

func (*FLVRecorder) GetRecordModeString added in v4.8.0

func (r *FLVRecorder) GetRecordModeString(mode RecordMode) string

func (*FLVRecorder) OnEvent

func (r *FLVRecorder) OnEvent(event any)

func (*FLVRecorder) SetId added in v4.8.0

func (r *FLVRecorder) SetId(streamPath string)

func (*FLVRecorder) Start

func (r *FLVRecorder) Start(streamPath string) (err error)

func (*FLVRecorder) StartWithDynamicTimeout added in v4.8.0

func (r *FLVRecorder) StartWithDynamicTimeout(streamPath, fileName string, timeout time.Duration) error

func (*FLVRecorder) StartWithFileName added in v4.6.0

func (r *FLVRecorder) StartWithFileName(streamPath string, fileName string) error

func (*FLVRecorder) StopTimerRecord added in v4.8.0

func (r *FLVRecorder) StopTimerRecord(reason ...zapcore.Field)

停止定时录像

func (*FLVRecorder) UpdateTimeout added in v4.8.0

func (r *FLVRecorder) UpdateTimeout(timeout time.Duration)

type FMP4Recorder

type FMP4Recorder struct {
	Recorder
	// contains filtered or unexported fields
}

func NewFMP4Recorder

func NewFMP4Recorder() *FMP4Recorder

func (*FMP4Recorder) Close

func (r *FMP4Recorder) Close() (err error)

func (*FMP4Recorder) GetRecordModeString added in v4.8.0

func (r *FMP4Recorder) GetRecordModeString(mode RecordMode) string

func (*FMP4Recorder) OnEvent

func (r *FMP4Recorder) OnEvent(event any)

func (*FMP4Recorder) SetId added in v4.8.0

func (r *FMP4Recorder) SetId(string)

func (*FMP4Recorder) Start

func (r *FMP4Recorder) Start(streamPath string) (err error)

func (*FMP4Recorder) StartWithDynamicTimeout added in v4.8.0

func (r *FMP4Recorder) StartWithDynamicTimeout(streamPath, fileName string, timeout time.Duration) error

func (*FMP4Recorder) StartWithFileName added in v4.6.0

func (r *FMP4Recorder) StartWithFileName(streamPath string, fileName string) error

func (*FMP4Recorder) UpdateTimeout added in v4.8.0

func (r *FMP4Recorder) UpdateTimeout(timeout time.Duration)

type FileWr

type FileWr interface {
	io.Reader
	io.Writer
	io.Seeker
	io.Closer
}

type FileWriter

type FileWriter struct {
	io.Reader
	io.Writer
	io.Seeker
	io.Closer
	// contains filtered or unexported fields
}

func (*FileWriter) Close

func (f *FileWriter) Close() error

func (*FileWriter) Seek

func (f *FileWriter) Seek(offset int64, whence int) (int64, error)

type HLSRecorder

type HLSRecorder struct {
	Recorder
	MemoryTs
	// contains filtered or unexported fields
}

func NewHLSRecorder

func NewHLSRecorder() (r *HLSRecorder)

func (*HLSRecorder) Close

func (r *HLSRecorder) Close() (err error)

func (*HLSRecorder) CreateFile

func (h *HLSRecorder) CreateFile() (fw FileWr, err error)

创建一个新的ts文件

func (*HLSRecorder) GetRecordModeString added in v4.8.0

func (h *HLSRecorder) GetRecordModeString(mode RecordMode) string

func (*HLSRecorder) OnEvent

func (h *HLSRecorder) OnEvent(event any)

func (*HLSRecorder) SetId added in v4.8.0

func (h *HLSRecorder) SetId(string)

func (*HLSRecorder) Start

func (h *HLSRecorder) Start(streamPath string) error

func (*HLSRecorder) StartWithDynamicTimeout added in v4.8.0

func (h *HLSRecorder) StartWithDynamicTimeout(streamPath, fileName string, timeout time.Duration) error

func (*HLSRecorder) StartWithFileName added in v4.6.0

func (h *HLSRecorder) StartWithFileName(streamPath string, fileName string) error

func (*HLSRecorder) UpdateTimeout added in v4.8.0

func (h *HLSRecorder) UpdateTimeout(timeout time.Duration)

type IRecorder

type IRecorder interface {
	ISubscriber
	GetRecorder() *Recorder
	Start(streamPath string) error
	StartWithFileName(streamPath string, fileName string) error
	io.Closer
	CreateFile() (FileWr, error)
	StartWithDynamicTimeout(streamPath, fileName string, timeout time.Duration) error
	UpdateTimeout(timeout time.Duration)
	GetRecordModeString(mode RecordMode) string
	SetId(streamPath string)
}

type MP4Recorder

type MP4Recorder struct {
	Recorder
	*mp4.Movmuxer `json:"-" yaml:"-"`
	// contains filtered or unexported fields
}

func NewMP4Recorder

func NewMP4Recorder() *MP4Recorder

func (*MP4Recorder) Close

func (r *MP4Recorder) Close() (err error)

func (*MP4Recorder) GetRecordModeString added in v4.8.0

func (r *MP4Recorder) GetRecordModeString(mode RecordMode) string

func (*MP4Recorder) OnEvent

func (r *MP4Recorder) OnEvent(event any)

func (*MP4Recorder) SetId added in v4.8.0

func (r *MP4Recorder) SetId(string)

func (*MP4Recorder) Start

func (r *MP4Recorder) Start(streamPath string) (err error)

func (*MP4Recorder) StartWithDynamicTimeout added in v4.8.0

func (r *MP4Recorder) StartWithDynamicTimeout(streamPath, fileName string, timeout time.Duration) error

func (*MP4Recorder) StartWithFileName added in v4.6.0

func (r *MP4Recorder) StartWithFileName(streamPath string, fileName string) error

func (*MP4Recorder) UpdateTimeout added in v4.8.0

func (r *MP4Recorder) UpdateTimeout(timeout time.Duration)

type RawRecorder

type RawRecorder struct {
	Recorder
	IsAudio bool
}

func NewRawAudioRecorder

func NewRawAudioRecorder() (r *RawRecorder)

func NewRawRecorder

func NewRawRecorder() (r *RawRecorder)

func (*RawRecorder) Close

func (r *RawRecorder) Close() (err error)

func (*RawRecorder) GetRecordModeString added in v4.8.0

func (r *RawRecorder) GetRecordModeString(mode RecordMode) string

func (*RawRecorder) OnEvent

func (r *RawRecorder) OnEvent(event any)

func (*RawRecorder) SetId added in v4.8.0

func (r *RawRecorder) SetId(string)

func (*RawRecorder) Start

func (r *RawRecorder) Start(streamPath string) error

func (*RawRecorder) StartWithDynamicTimeout added in v4.8.0

func (r *RawRecorder) StartWithDynamicTimeout(streamPath, fileName string, timeout time.Duration) error

func (*RawRecorder) StartWithFileName added in v4.6.0

func (r *RawRecorder) StartWithFileName(streamPath string, fileName string) error

func (*RawRecorder) UpdateTimeout added in v4.8.0

func (r *RawRecorder) UpdateTimeout(timeout time.Duration)

type Record

type Record struct {
	Ext           string        `desc:"文件扩展名"`           //文件扩展名
	Path          string        `desc:"存储文件的目录"`         //存储文件的目录
	AutoRecord    bool          `desc:"是否自动录制"`          //是否自动录制
	Filter        config.Regexp `desc:"录制过滤器"`           //录制过滤器
	Fragment      time.Duration `desc:"分片大小,0表示不分片"`     //分片大小,0表示不分片
	Duration      time.Duration `desc:"视频最大录制时长,0表示不限制"` //分片大小,0表示不分片
	http.Handler  `json:"-" yaml:"-"`
	CreateFileFn  func(filename string, append bool) (FileWr, error) `json:"-" yaml:"-"`
	GetDurationFn func(file io.ReadSeeker) uint32                    `json:"-" yaml:"-"`
}

func (*Record) Init

func (r *Record) Init()

func (*Record) NeedRecord

func (r *Record) NeedRecord(streamPath string) bool

func (*Record) Tree

func (r *Record) Tree(dstPath string, level int) (files []*VideoFileInfo, err error)

type RecordConfig

type RecordConfig struct {
	config.Subscribe
	config.HTTP
	Flv      Record `desc:"flv录制配置"`
	Mp4      Record `desc:"mp4录制配置"`
	Fmp4     Record `desc:"fmp4录制配置"`
	Hls      Record `desc:"hls录制配置"`
	Raw      Record `desc:"视频裸流录制配置"`
	RawAudio Record `desc:"音频裸流录制配置"`

	MysqlDSN                    string        `desc:"mysql数据库连接字符串"`
	ExceptionPostUrl            string        `desc:"第三方异常上报地址"`
	SqliteDbPath                string        `desc:"sqlite数据库路径"`
	DiskMaxPercent              float64       `desc:"硬盘使用百分之上限值,超过后报警"`
	LocalIp                     string        `desc:"本机IP"`
	RecordFileExpireDays        int           `desc:"录像自动删除的天数,0或未设置表示不自动删除"`
	RecordPathNotShowStreamPath bool          `desc:"录像路径中是否包含streamPath,默认true"`
	Storage                     StorageConfig `desc:"MINIO 配置"`
	// contains filtered or unexported fields
}

func (*RecordConfig) API_alarm_list added in v4.8.0

func (conf *RecordConfig) API_alarm_list(w http.ResponseWriter, r *http.Request)

录像报警列表

func (*RecordConfig) API_event_list added in v4.8.0

func (conf *RecordConfig) API_event_list(w http.ResponseWriter, r *http.Request)

事件录像列表

func (*RecordConfig) API_event_pull added in v4.8.0

func (conf *RecordConfig) API_event_pull(w http.ResponseWriter, r *http.Request)

根据事件id拉取录像流

func (*RecordConfig) API_event_start added in v4.8.0

func (conf *RecordConfig) API_event_start(w http.ResponseWriter, r *http.Request)

事件录像

func (*RecordConfig) API_list

func (conf *RecordConfig) API_list(w http.ResponseWriter, r *http.Request)

func (*RecordConfig) API_list_page added in v4.6.0

func (conf *RecordConfig) API_list_page(w http.ResponseWriter, r *http.Request)

func (*RecordConfig) API_list_recording

func (conf *RecordConfig) API_list_recording(w http.ResponseWriter, r *http.Request)

func (*RecordConfig) API_list_recording_page added in v4.6.0

func (conf *RecordConfig) API_list_recording_page(w http.ResponseWriter, r *http.Request)

func (*RecordConfig) API_recordfile_delete added in v4.6.0

func (conf *RecordConfig) API_recordfile_delete(w http.ResponseWriter, r *http.Request)

func (*RecordConfig) API_recordfile_modify added in v4.6.0

func (conf *RecordConfig) API_recordfile_modify(w http.ResponseWriter, r *http.Request)

func (*RecordConfig) API_start

func (conf *RecordConfig) API_start(w http.ResponseWriter, r *http.Request)

func (*RecordConfig) API_stop

func (conf *RecordConfig) API_stop(w http.ResponseWriter, r *http.Request)

func (*RecordConfig) CheckRecordDB added in v4.9.0

func (conf *RecordConfig) CheckRecordDB()

查询所有正在录制中的记录

func (*RecordConfig) Download_flv_ added in v4.8.0

func (conf *RecordConfig) Download_flv_(w http.ResponseWriter, r *http.Request)

func (*RecordConfig) OnEvent

func (conf *RecordConfig) OnEvent(event any)

func (*RecordConfig) Play_flv_ added in v4.8.0

func (conf *RecordConfig) Play_flv_(w http.ResponseWriter, r *http.Request)

func (*RecordConfig) ServeHTTP

func (conf *RecordConfig) ServeHTTP(w http.ResponseWriter, r *http.Request)

type RecordMode added in v4.8.0

type RecordMode int

录像类型

const (
	OrdinaryMode RecordMode = iota // iota 初始值为 0,表示普通录像(连续录像),包括自动录像和手动录像
	EventMode                      // 1,表示事件录像
)

使用常量块和 iota 来定义枚举值

type Recorder

type Recorder struct {
	Subscriber
	Storage  StorageConfig
	SkipTS   uint32
	Record   `json:"-" yaml:"-"`
	File     FileWr `json:"-" yaml:"-"`
	FileName string // 自定义文件名,分段录像无效
	// contains filtered or unexported fields
}

func (*Recorder) CreateFile

func (r *Recorder) CreateFile() (f FileWr, err error)

func (*Recorder) GetRecorder

func (r *Recorder) GetRecorder() *Recorder

func (*Recorder) OnEvent

func (r *Recorder) OnEvent(event any)

func (*Recorder) RemoveRecordById added in v4.9.0

func (r *Recorder) RemoveRecordById()

更新录像文件表中记录,包括录像文件的大小、结束时间以及录像状态

func (*Recorder) SaveToDB added in v4.9.0

func (r *Recorder) SaveToDB()

保存Recorder到数据库中

func (*Recorder) UploadFile added in v4.4.8

func (r *Recorder) UploadFile(filePath string, fileName string)

type StorageConfig added in v4.4.8

type StorageConfig struct {
	Endpoint  string
	AccessKey string
	SecretKey string
	Bucket    string
	UseSSL    bool
}

type VideoFileInfo

type VideoFileInfo struct {
	Path     string
	Size     int64
	Duration uint32
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL