This commit is contained in:
deepch 2024-01-10 18:02:17 +03:00
parent 385d9cc93c
commit 4df258b899
2 changed files with 44 additions and 19 deletions

View File

@ -170,6 +170,20 @@ func (self *AVSync) check(i int) (start time.Duration, end time.Duration, correc
return return
} }
type CalcDuration struct {
LastTime map[int8]time.Duration
}
func (self *CalcDuration) ModifyPacket(pkt *av.Packet, streams []av.CodecData, videoidx int, audioidx int) (drop bool, err error) {
if tmp, ok := self.LastTime[pkt.Idx]; ok && tmp != 0 {
pkt.Duration = pkt.Time - self.LastTime[pkt.Idx]
} else if pkt.Time < 100*time.Millisecond {
pkt.Duration = pkt.Time
}
self.LastTime[pkt.Idx] = pkt.Time
return
}
// Make packets reading speed as same as walltime, effect like ffmpeg -re option. // Make packets reading speed as same as walltime, effect like ffmpeg -re option.
type Walltime struct { type Walltime struct {
firsttime time.Time firsttime time.Time

View File

@ -16,7 +16,7 @@ import (
) )
var MIME = []byte{11, 22, 111, 222, 11, 22, 111, 222} var MIME = []byte{11, 22, 111, 222, 11, 22, 111, 222}
var listTag = []string{"{host_name}", "{stream_name}", "{channel_name}", "{stream_id}", "{channel_id}", "{start_year}", "{start_month}", "{start_day}", "{start_minute}", "{start_second}", "{start_millisecond}", "{start_unix_second}", "{start_unix_millisecond}", "{start_time}", "{start_pts}", "{end_year}", "{end_month}", "{end_day}", "{end_minute}", "{end_second}", "{end_millisecond}", "{start_unix_second}", "{start_unix_millisecond}", "{end_time}", "{end_pts}", "{duration_second}", "{duration_millisecond}"} var listTag = []string{"{server_id}", "{hostname_short}", "{hostname_long}", "{stream_name}", "{channel_name}", "{stream_id}", "{channel_id}", "{start_year}", "{start_month}", "{start_day}", "{start_minute}", "{start_second}", "{start_millisecond}", "{start_unix_second}", "{start_unix_millisecond}", "{start_time}", "{start_pts}", "{end_year}", "{end_month}", "{end_day}", "{end_minute}", "{end_second}", "{end_millisecond}", "{start_unix_second}", "{start_unix_millisecond}", "{end_time}", "{end_pts}", "{duration_second}", "{duration_millisecond}"}
const ( const (
MP4 = "mp4" MP4 = "mp4"
@ -24,19 +24,19 @@ const (
) )
type Muxer struct { type Muxer struct {
muxer *mp4.Muxer muxer *mp4.Muxer
format string format string
limit int limit int
d *os.File d *os.File
m *os.File m *os.File
dur time.Duration dur time.Duration
h int h int
gof *Gof gof *Gof
patch string patch string
start, end time.Time start, end time.Time
pstart, pend time.Duration pstart, pend time.Duration
started bool started bool
hostName, streamName, channelName, streamID, channelID string serverID, streamName, channelName, streamID, channelID, hostname string
} }
type Gof struct { type Gof struct {
@ -64,18 +64,20 @@ func init() {
} }
func NewMuxer(hostName, streamName, channelName, streamID, channelID, patch string, format string, limit int) (m *Muxer, err error) { func NewMuxer(serverID, streamName, channelName, streamID, channelID, patch string, format string, limit int) (m *Muxer, err error) {
hostname, _ := os.Hostname()
m = &Muxer{ m = &Muxer{
patch: patch, patch: patch,
h: -1, h: -1,
gof: &Gof{}, gof: &Gof{},
format: format, format: format,
limit: limit, limit: limit,
hostName: hostName, serverID: serverID,
streamName: streamName, streamName: streamName,
channelName: channelName, channelName: channelName,
streamID: streamID, streamID: streamID,
channelID: channelID, channelID: channelID,
hostname: hostname,
} }
return return
} }
@ -186,7 +188,12 @@ func (m *Muxer) OpenNVR() (err error) {
func (m *Muxer) OpenMP4() (err error) { func (m *Muxer) OpenMP4() (err error) {
m.WriteTrailer() m.WriteTrailer()
m.start = time.Now().UTC() m.start = time.Now().UTC()
if m.d, err = os.CreateTemp("", "rtspvideo.*.mp4"); err != nil {
p := m.filePatch()
if err = os.MkdirAll(filepath.Dir(p), 0755); err != nil {
return
}
if m.d, err = os.Create(filepath.Dir(p) + "/tmp.mp4"); err != nil {
return return
} }
m.muxer = mp4.NewMuxer(m.d) m.muxer = mp4.NewMuxer(m.d)
@ -202,8 +209,12 @@ func (m *Muxer) filePatch() string {
m.end = time.Now().UTC() m.end = time.Now().UTC()
for _, s := range listTag { for _, s := range listTag {
switch s { switch s {
case "{host_name}": case "{server_id}":
ts = strings.Replace(ts, "{host_name}", m.hostName, -1) ts = strings.Replace(ts, "{host_name}", m.serverID, -1)
case "{hostname_short}":
ts = strings.Replace(ts, "{host_name}", m.hostname, -1)
case "{hostname_long}":
ts = strings.Replace(ts, "{host_name}", m.hostname, -1)
case "{stream_name}": case "{stream_name}":
ts = strings.Replace(ts, "{stream_name}", m.streamName, -1) ts = strings.Replace(ts, "{stream_name}", m.streamName, -1)
case "{channel_name}": case "{channel_name}":