This commit is contained in:
deepch 2024-04-30 17:06:05 +03:00
parent f6346a05aa
commit b6b1f4a437

View File

@ -47,7 +47,7 @@ type Muxer struct {
pstart, pend time.Duration pstart, pend time.Duration
started bool started bool
serverID, streamName, channelName, streamID, channelID, hostLong, hostShort string serverID, streamName, channelName, streamID, channelID, hostLong, hostShort string
handleFileChange func(string, string, int64, time.Time, time.Time, time.Duration) handleFileChange func(bool, string, string, int64, time.Time, time.Time, time.Duration)
} }
type Gof struct { type Gof struct {
@ -75,7 +75,7 @@ func init() {
} }
func NewMuxer(serverID, streamName, channelName, streamID, channelID string, mpoint []string, patch, format string, limit int, c func(string, string, int64, time.Time, time.Time, time.Duration)) (m *Muxer, err error) { func NewMuxer(serverID, streamName, channelName, streamID, channelID string, mpoint []string, patch, format string, limit int, c func(bool, string, string, int64, time.Time, time.Time, time.Duration)) (m *Muxer, err error) {
hostLong, _ := os.Hostname() hostLong, _ := os.Hostname()
var hostShort string var hostShort string
if p, _, ok := strings.Cut(hostLong, "."); ok { if p, _, ok := strings.Cut(hostLong, "."); ok {
@ -218,9 +218,20 @@ func (m *Muxer) OpenMP4() (err error) {
if err = os.MkdirAll(filepath.Dir(d), 0755); err != nil { if err = os.MkdirAll(filepath.Dir(d), 0755); err != nil {
return return
} }
if m.d, err = os.Create(filepath.Join(filepath.Dir(d), fmt.Sprintf("tmp_%s_%d.mp4", uuid.New(), time.Now().Unix()))); err != nil { name := filepath.Join(filepath.Dir(d), fmt.Sprintf("tmp_%s_%d.mp4", uuid.New(), time.Now().Unix()))
if m.d, err = os.Create(name); err != nil {
return return
} }
m.handleFileChange(
true,
m.Codecs(),
name,
0,
m.start,
m.end,
m.dur,
)
m.muxer = mp4.NewMuxer(m.d) m.muxer = mp4.NewMuxer(m.d)
m.muxer.NegativeTsMakeZero = true m.muxer.NegativeTsMakeZero = true
if err = m.muxer.WriteHeader(m.gof.Streams); err != nil { if err = m.muxer.WriteHeader(m.gof.Streams); err != nil {
@ -329,7 +340,7 @@ func (m *Muxer) filePatch() (string, error) {
func (m *Muxer) Codecs() string { func (m *Muxer) Codecs() string {
var codecs []string var codecs []string
for _, stream := range m.gof.Streams { for _, stream := range m.gof.Streams {
codecs = append(codecs, stream.Type().String()) codecs = append(codecs, strings.ToLower(stream.Type().String()))
} }
return strings.Join(codecs, ",") return strings.Join(codecs, ",")
@ -356,6 +367,7 @@ func (m *Muxer) WriteTrailer() (err error) {
size = fi.Size() size = fi.Size()
} }
m.handleFileChange( m.handleFileChange(
false,
m.Codecs(), m.Codecs(),
filepath.Join(filepath.Dir(m.d.Name()), filepath.Base(p)), filepath.Join(filepath.Dir(m.d.Name()), filepath.Base(p)),
size, size,