增加metadata的tag写入

This commit is contained in:
langhuihui
2020-08-13 20:16:24 +08:00
parent c7d84fdd3d
commit 1fa44e1287
4 changed files with 60 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.idea

1
go.mod
View File

@@ -5,4 +5,5 @@ go 1.13
require (
github.com/Monibuca/engine/v2 v2.1.1
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381
github.com/zhangpeihao/goamf v0.0.0-20140409082417-3ff2c19514a8
)

2
go.sum
View File

@@ -29,6 +29,8 @@ github.com/shirou/gopsutil v2.20.1+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/zhangpeihao/goamf v0.0.0-20140409082417-3ff2c19514a8 h1:r1JUI0wuHlgRb8jNd3zPBBkjUdrjpVKr8SdJWc8ntg8=
github.com/zhangpeihao/goamf v0.0.0-20140409082417-3ff2c19514a8/go.mod h1:RZd/IqzNpFANwOB9rVmsnAYpo/6KesK4PqrN1a5cRgg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

56
main.go
View File

@@ -1,6 +1,8 @@
package hdl
import (
"bytes"
"encoding/binary"
"log"
"net/http"
"strings"
@@ -8,6 +10,7 @@ import (
. "github.com/Monibuca/engine/v2"
"github.com/Monibuca/engine/v2/avformat"
. "github.com/logrusorgru/aurora"
"github.com/zhangpeihao/goamf"
)
var config = new(ListenerConfig)
@@ -42,6 +45,32 @@ func HDLHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Transfer-Encoding", "chunked")
w.Header().Set("Content-Type", "video/x-flv")
w.Write(avformat.FLVHeader)
var metadata avformat.SendPacket
metadata.AVPacket = new(avformat.AVPacket)
metadata.Type = avformat.FLV_TAG_TYPE_SCRIPT
var buffer bytes.Buffer
amf.WriteString(&buffer, "onMetaData")
WriteEcmaArray(&buffer, amf.Object{
"MetaDataCreator": "monibuca",
"hasVideo": true,
"hasAudio": true,
"hasMatadata": true,
"canSeekToEnd": false,
"duration": 0,
"hasKeyFrames": 0,
"videocodecid": int(s.VideoInfo.CodecID),
"framerate": 0,
"videodatarate": 0,
"audiocodecid": int(s.AudioInfo.SoundFormat),
"filesize": 0,
"width": s.VideoInfo.SPSInfo.Width,
"height": s.VideoInfo.SPSInfo.Height,
"audiosamplerate": s.AudioInfo.SoundRate,
"audiosamplesize": int(s.AudioInfo.SoundSize),
"stereo": s.AudioInfo.SoundType == 1,
})
metadata.Payload = buffer.Bytes()
avformat.WriteFLVTag(w, &metadata)
p := Subscriber{
Sign: sign,
OnData: func(packet *avformat.SendPacket) error {
@@ -56,3 +85,30 @@ func HDLHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
}
}
func WriteEcmaArray(w amf.Writer, o amf.Object) (n int, err error) {
n, err = amf.WriteMarker(w, amf.AMF0_ECMA_ARRAY_MARKER)
if err != nil {
return
}
length := int32(len(o))
err = binary.Write(w, binary.BigEndian, &length)
if err != nil {
return
}
n += 4
m := 0
for name, value := range o {
m, err = amf.WriteObjectName(w, name)
if err != nil {
return
}
n += m
m, err = amf.WriteValue(w, value)
if err != nil {
return
}
n += m
}
m, err = amf.WriteObjectEndMarker(w)
return n + m, err
}