cosgoflv

package module
v0.0.0-...-bed049e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2024 License: MIT Imports: 3 Imported by: 0

README

A go lib for handling FLV files

Warning: This lib only handles flv with the following conditions met:

  • Valid flv file
  • Flv version is 1
  • AAC audio(if parsing SoundData)
  • AVC video(if parsing VideoData)

Warning: The lib does not support parsing data tags. Relevant code is in flv_data_tag.go and flv_scriptdata.go

Quick Start

package main

import (
    "os"
    . "github.com/Coosis/cos-goflv"
)

func main() {
    // Reading an flv
	// Place the sample.flv file in root dir
	f, err := os.Open("sample.flv")
	if err != nil {
		panic(err)
	}
	defer f.Close()
	flv := Flv{}
	if err := flv.Read(f); err != nil {
		panic(err)
	}

	// ...

	// Writing an flv
	// Will create a copy of the sample.flv file in root dir
	f2, err := os.Create("sample_cp.flv")
	if err != nil {
		panic(err)
	}
	defer f2.Close()
	if err := flv.Write(f2); err != nil {
		panic(err)
	}

}

For better integration with golang, the following types(right) are used for types(left) in the flv spec:

UB[2] -> uint32 UB[4] -> uint8 UI24 -> uint32 SI24 -> int32

If not parsed, data will be stored in the Data field of the Tag struct

To have more control over the data, parse them into audio/video tag using TryIntoAudio and TryIntoVideo Example:

package main

import (
    "os"
    . "github.com/Coosis/cos-goflv"
)

func main() {
    // Reading an flv
	// Place the sample.flv file in root dir
	f, err := os.Open("sample.flv")
	if err != nil {
		panic(err)
	}
	defer f.Close()
	flv := Flv{}
	if err := flv.Read(f); err != nil {
		panic(err)
	}

    audio := &FlvAudioTag{}
    if err := flv.Body.Tags[0].Tag.TryIntoAudio(audio); err != nil {
        panic(err)
    }

    video := &FlvVideoTag{}
    if err := flv.Body.Tags[1].Tag.TryIntoVideo(video); err != nil {
        panic(err)
    }
}

Documentation

Index

Constants

View Source
const (
	ASCII_F = 0x46
	ASCII_L = 0x4C
	ASCII_V = 0x56
	V1      = 0x01
)
View Source
const (
	OBJECTENDMET = "SCRIPTDATAOBJECTEND"
)
View Source
const (
	SCRIPTDATAOBJECTEND uint8 = 0x09
)

Variables

This section is empty.

Functions

func IsValidAACPacketType

func IsValidAACPacketType(aacPacketType AACPacketType) error

func IsValidCodecID

func IsValidCodecID(codecID uint8) error

func IsValidFlvTagType

func IsValidFlvTagType(tagType FlvTagType) error

func IsValidFrameType

func IsValidFrameType(frameType uint8) error

func IsValidSamplingRate

func IsValidSamplingRate(rate uint8) error

func IsValidSoundFormat

func IsValidSoundFormat(format uint8) error

func IsValidSoundSize

func IsValidSoundSize(size uint8) error

func IsValidSoundType

func IsValidSoundType(soundType uint8) error

Types

type AACPacketType

type AACPacketType uint8
const (
	AAC_SequenceHeader AACPacketType = iota
	AAC_Raw
)

type AVCPacketType

type AVCPacketType uint8
const (
	AVC_SequenceHeader AVCPacketType = iota
	AVC_NALU
	AVC_EndOfSequence
)

type CodecID

type CodecID uint8
const (
	JPEG CodecID = iota + 1
	SorensonH263
	ScreenVideo
	On2VP6
	On2VP6Alpha
	ScreenVideov2
	AVC
)

type Flv

type Flv struct {
	Header FlvHeader
	Body   FlvBody
}

func EmptyFlv

func EmptyFlv() *Flv

func (*Flv) Read

func (f *Flv) Read(r io.Reader) error

func (*Flv) Write

func (f *Flv) Write(w io.Writer) error

type FlvAACAudioData

type FlvAACAudioData struct {
	AACPacketType AACPacketType
	Data          []byte
}

func (*FlvAACAudioData) Parse

func (aacdata *FlvAACAudioData) Parse(data []byte) error

type FlvAudioTag

type FlvAudioTag struct {
	Format SoundFormat
	Rate   SamplingRate
	Size   SoundSize
	Type   SoundType
	Data   []byte
}

func (*FlvAudioTag) Parse

func (audioTag *FlvAudioTag) Parse(data []byte) error

type FlvAvcVideoPacket

type FlvAvcVideoPacket struct {
	AVCPacketType   AVCPacketType
	CompositionTime int32
	Data            []byte
}

func (*FlvAvcVideoPacket) Parse

func (avp *FlvAvcVideoPacket) Parse(data []byte) error

type FlvBody

type FlvBody struct {
	Tag []TagnSize
}

func EmptyFlvBody

func EmptyFlvBody() *FlvBody

func (*FlvBody) AddTag

func (b *FlvBody) AddTag(tags ...FlvTag)

func (*FlvBody) Read

func (b *FlvBody) Read(r io.Reader) error

func (*FlvBody) Write

func (b *FlvBody) Write(w io.Writer) error

type FlvDataTag

type FlvDataTag struct {
	Objects []FlvScriptDataObject
	End     uint32
}

type FlvHeader

type FlvHeader struct {
	Version uint8

	TypeFlags uint8

	DataOffset uint32
}

func EmptyFlvHeader

func EmptyFlvHeader() *FlvHeader

func (*FlvHeader) Parse

func (header *FlvHeader) Parse(data []byte) error

func (*FlvHeader) Read

func (header *FlvHeader) Read(r io.Reader) error

func (*FlvHeader) Write

func (header *FlvHeader) Write(w io.Writer) error

type FlvScriptDataDate

type FlvScriptDataDate struct {
	// Number of milliseconds since midnight, January 1, 1970 UTC
	DateTime float64
	// Local time offset in minutes from UTC
	LocalDateTimeOffset int16
}

type FlvScriptDataLongString

type FlvScriptDataLongString struct {
	StringLength uint32
	StringData   string
}

func (*FlvScriptDataLongString) Parse

func (dataString *FlvScriptDataLongString) Parse(data []byte) (int, error)

type FlvScriptDataObject

type FlvScriptDataObject struct {
	ObjectName FlvScriptDataString
	ObjectData FlvScriptDataValue
}

func (*FlvScriptDataObject) Parse

func (obj *FlvScriptDataObject) Parse(data []byte) (int, error)

type FlvScriptDataString

type FlvScriptDataString struct {
	StringLength uint16
	StringData   string
}

func (*FlvScriptDataString) Parse

func (dataString *FlvScriptDataString) Parse(data []byte) (int, error)

type FlvScriptDataValue

type FlvScriptDataValue struct {
	DataType        uint8
	ECMAArrayLength uint32
	ScriptDataValue []byte
}

func (*FlvScriptDataValue) Parse

func (val *FlvScriptDataValue) Parse(data []byte) (int, error)

type FlvScriptDataVariable

type FlvScriptDataVariable struct {
	VariableName FlvScriptDataString
	VariableData FlvScriptDataValue
}

type FlvScriptDataVariableEnd

type FlvScriptDataVariableEnd struct {
	// Always 9
	VariableEndMarker1 uint32
}

type FlvTag

type FlvTag struct {
	TagType   FlvTagType
	DataSize  uint32
	TimeStamp uint32
	StreamID  uint32
	// Keep as-is, as it's not consumed by the parser
	Data []byte
}

func (*FlvTag) Read

func (tag *FlvTag) Read(r io.Reader) error

func (*FlvTag) TryIntoAudio

func (tag *FlvTag) TryIntoAudio(audioTag *FlvAudioTag) error

func (*FlvTag) TryIntoVideo

func (tag *FlvTag) TryIntoVideo(videoTag *FlvVideoTag) error

func (*FlvTag) Write

func (tag *FlvTag) Write(w io.Writer) error

type FlvTagType

type FlvTagType uint8
const (
	AUDIO      FlvTagType = 8
	VIDEO      FlvTagType = 9
	SCRIPTDATA FlvTagType = 18
	OTHER      FlvTagType = 0
)

type FlvVideoTag

type FlvVideoTag struct {
	FrameType FrameType
	CodecID   CodecID
	VideoData []byte
}

func (*FlvVideoTag) Parse

func (videoTag *FlvVideoTag) Parse(data []byte) error

type FrameType

type FrameType uint8
const (
	KeyFrame FrameType = iota + 1
	InterFrame
	DisposableInterFrame
	GeneratedKeyFrame
	// Equivalent to video info/command frame
	VideoFrame
)

type SamplingRate

type SamplingRate uint8
const (
	// Sampling rate. For AAC, it's always 3
	KHZ5P5 SamplingRate = iota
	KHZ11
	KHZ22
	KHZ44
)

type SoundFormat

type SoundFormat uint8
const (
	// 7, 8, 14, 15 are reserved for internal use
	LinearPCMPlatformEndian SoundFormat = iota
	ADPCM
	MP3
	LinearPCMLittleEndian
	Nellymoser16KHzMono
	Nellymoser8KHzMono
	Nellymoser
	G711ALawLogarithmicPCM
	G711MuLawLogarithmicPCM
	Reserved
	AAC
	Speex
	MP38KHz
	DeviceSpecificSound
)

type SoundSize

type SoundSize uint8
const (
	Bit8 SoundSize = iota
	Bit16
)

type SoundType

type SoundType uint8
const (
	Mono SoundType = iota
	Stereo
)

type TagnSize

type TagnSize struct {
	Tag      FlvTag
	PrevSize uint32
}

func (*TagnSize) Read

func (ts *TagnSize) Read(r io.Reader) error

func (*TagnSize) Write

func (ts *TagnSize) Write(w io.Writer) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL