mirror of
https://github.com/Monibuca/plugin-record.git
synced 2025-10-16 05:40:51 +08:00
feat: add raw_audio recoder
This commit is contained in:
64
raw.go
64
raw.go
@@ -12,11 +12,19 @@ import (
|
||||
|
||||
type RawRecorder struct {
|
||||
Recorder
|
||||
IsAudio bool
|
||||
}
|
||||
|
||||
func (r *RawRecorder) Start(streamPath string) error {
|
||||
r.Record = &RecordPluginConfig.Raw
|
||||
if r.IsAudio {
|
||||
r.Record = &RecordPluginConfig.RawAudio
|
||||
} else {
|
||||
r.Record = &RecordPluginConfig.Raw
|
||||
}
|
||||
r.ID = streamPath + "/raw"
|
||||
if r.IsAudio {
|
||||
r.ID += "_audio"
|
||||
}
|
||||
if _, ok := RecordPluginConfig.recordings.Load(r.ID); ok {
|
||||
return ErrRecordExist
|
||||
}
|
||||
@@ -24,11 +32,22 @@ func (r *RawRecorder) Start(streamPath string) error {
|
||||
}
|
||||
|
||||
func (r *RawRecorder) OnEvent(event any) {
|
||||
r.Recorder.OnEvent(event)
|
||||
switch v := event.(type) {
|
||||
case *RawRecorder:
|
||||
filename := strconv.FormatInt(time.Now().Unix(), 10) + r.Ext
|
||||
if r.Fragment == 0 {
|
||||
filename = r.Stream.Path + r.Ext
|
||||
} else {
|
||||
filename = filepath.Join(r.Stream.Path, filename)
|
||||
}
|
||||
if file, err := r.CreateFileFn(filename, r.append); err == nil {
|
||||
r.SetIO(file)
|
||||
}
|
||||
go r.start()
|
||||
case *track.Video:
|
||||
if r.IsAudio {
|
||||
break
|
||||
}
|
||||
if r.Ext == "." {
|
||||
if v.CodecID == codec.CodecID_H264 {
|
||||
r.Ext = ".h264"
|
||||
@@ -36,14 +55,45 @@ func (r *RawRecorder) OnEvent(event any) {
|
||||
r.Ext = ".h265"
|
||||
}
|
||||
}
|
||||
r.AddTrack(v)
|
||||
case *track.Audio:
|
||||
if !r.IsAudio {
|
||||
break
|
||||
}
|
||||
if r.Ext == "." {
|
||||
switch v.CodecID {
|
||||
case codec.CodecID_AAC:
|
||||
r.Ext = ".aac"
|
||||
case codec.CodecID_PCMA:
|
||||
r.Ext = ".pcma"
|
||||
case codec.CodecID_PCMU:
|
||||
r.Ext = ".pcmu"
|
||||
}
|
||||
}
|
||||
r.AddTrack(v)
|
||||
case AudioFrame:
|
||||
if r.Fragment > 0 {
|
||||
if r.cut(v.AbsTime); r.newFile {
|
||||
r.newFile = false
|
||||
r.Close()
|
||||
if file, err := r.CreateFileFn(filepath.Join(r.Stream.Path, strconv.FormatInt(time.Now().Unix(), 10)+r.Ext), false); err == nil {
|
||||
r.SetIO(file)
|
||||
}
|
||||
}
|
||||
}
|
||||
v.WriteRawTo(r)
|
||||
case VideoFrame:
|
||||
if r.Fragment != 0 && r.newFile {
|
||||
r.newFile = false
|
||||
r.Close()
|
||||
if file, err := r.CreateFileFn(filepath.Join(r.Stream.Path, strconv.FormatInt(time.Now().Unix(), 10)+r.Ext), false); err == nil {
|
||||
r.SetIO(file)
|
||||
if r.Fragment > 0 && v.IFrame {
|
||||
if r.cut(v.AbsTime); r.newFile {
|
||||
r.newFile = false
|
||||
r.Close()
|
||||
if file, err := r.CreateFileFn(filepath.Join(r.Stream.Path, strconv.FormatInt(time.Now().Unix(), 10)+r.Ext), false); err == nil {
|
||||
r.SetIO(file)
|
||||
}
|
||||
}
|
||||
}
|
||||
v.WriteAnnexBTo(r)
|
||||
default:
|
||||
r.IO.OnEvent(v)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user