av/flv/FLV.go

115 lines
2.4 KiB
Go
Raw Normal View History

2018-02-10 16:25:55 +03:00
package flv
import (
"../tools"
"fmt"
2018-02-10 16:25:55 +03:00
)
const (
headerLength = 72
version = 0x01
maxVideoTagSize = 10000
maxAudioTagSize = 10000
)
const (
VideoTagType = 9
AudioTagType = 8
KeyFrameType = 1
2018-02-15 11:13:45 +03:00
InterFrameType = 2
H264 = 7
AVCNALU = 1
DataHeaderLength = 5
NoTimestampExtension = 0
AACAudioFormat = 10
2018-02-10 16:25:55 +03:00
)
type Header struct {
AudioFlag bool
VideoFlag bool
2018-02-10 16:25:55 +03:00
}
func (h *Header) ToByteSlice() (output []byte) {
output = make([]byte, 0, headerLength)
output = append(output, []byte{0x46, 0x4C, 0x56,
version,
0x00 | tools.BoolToByte(h.AudioFlag)<<2 | tools.BoolToByte(h.VideoFlag),
0x00, 0x00, 0x00, byte(9),
}...)
fmt.Println(output)
return
2018-02-10 16:25:55 +03:00
}
type VideoTag struct {
PrevTagSize uint32
TagType uint8
DataSize uint32
Timestamp uint32
TimestampExtended uint32
FrameType byte
Codec byte
PacketType byte
CompositionTime uint32
Data []byte
2018-02-10 16:25:55 +03:00
}
func (t *VideoTag) ToByteSlice() (output []byte) {
output = make([]byte, 0, maxVideoTagSize)
output = append(output, []byte{
byte(t.PrevTagSize >> 24),
byte(t.PrevTagSize >> 16),
byte(t.PrevTagSize >> 8),
byte(t.PrevTagSize),
byte(t.TagType),
byte(t.DataSize >> 16),
byte(t.DataSize >> 8),
byte(t.DataSize),
byte(t.Timestamp >> 16),
byte(t.Timestamp >> 8),
byte(t.Timestamp),
byte(t.TimestampExtended),
0x00, 0x00, 0x00,
2018-02-15 11:13:45 +03:00
0x00 | byte(t.FrameType << 4) | byte(t.Codec),
t.PacketType,
byte(t.CompositionTime >> 16),byte(t.CompositionTime >> 8),byte(t.CompositionTime),
}...)
output = append(output, t.Data...)
return
2018-02-10 16:25:55 +03:00
}
type AudioTag struct {
PrevTagSize uint32
TagType uint8
DataSize uint32
Timestamp uint32
TimestampExtended uint32
SoundFormat uint8
SoundRate uint8
SoundSize bool
SoundType bool
Data []byte
2018-02-10 16:25:55 +03:00
}
func (t *AudioTag) ToByteSlice() (output []byte) {
output = make([]byte, 0, maxVideoTagSize)
output = append(output, []byte{
byte(t.PrevTagSize >> 24),
byte(t.PrevTagSize >> 16),
byte(t.PrevTagSize >> 8),
byte(t.PrevTagSize),
byte(t.TagType),
byte(t.DataSize >> 16),
byte(t.DataSize >> 8),
byte(t.DataSize),
byte(t.Timestamp >> 16),
byte(t.Timestamp >> 8),
byte(t.Timestamp),
byte(t.TimestampExtended),
0x00, 0x00, 0x00,
byte(t.SoundFormat << 4) | byte(t.SoundRate<<2) | byte(tools.BoolToByte(t.SoundSize)<<1) | byte(tools.BoolToByte(t.SoundType)),
}...)
output = append(output, t.Data...)
return
2018-02-10 16:25:55 +03:00
}