go-mkvparse: github.com/remko/go-mkvparse Index | Examples | Files

package mkvparse

import "github.com/remko/go-mkvparse"

Package mkvparse provides push-style parser functions for parsing Matroska (`.mkv`, `.mka`) files.

Code:

package main

import (
    "fmt"
    "log"
    "time"
)

type MyParser struct {
}

func (p *MyParser) HandleMasterBegin(id ElementID, info ElementInfo) (bool, error) {
    return true, nil
}

func (p *MyParser) HandleMasterEnd(id ElementID, info ElementInfo) error {
    return nil
}

func (p *MyParser) HandleString(id ElementID, value string, info ElementInfo) error {
    if id == TitleElement {
        fmt.Printf("%s: %v\n", NameForElementID(id), value)
    }
    return nil
}

func (p *MyParser) HandleInteger(id ElementID, value int64, info ElementInfo) error {
    return nil
}

func (p *MyParser) HandleFloat(id ElementID, value float64, info ElementInfo) error {
    return nil
}

func (p *MyParser) HandleDate(id ElementID, value time.Time, info ElementInfo) error {
    return nil
}

func (p *MyParser) HandleBinary(id ElementID, value []byte, info ElementInfo) error {
    return nil
}

func main() {
    handler := MyParser{}
    if err := ParsePath("example.mkv", &handler); err != nil {
        log.Fatalf("%v", err)
    }
}

Index

Examples

Package Files

elements.go mkvparse.go sectionparser.go

func NameForElementID Uses

func NameForElementID(id ElementID) string

Gives the human-readable name for the given element ID.

Code:

fmt.Println(NameForElementID(InfoElement))

Output:

Info

func Parse Uses

func Parse(reader io.Reader, handler Handler) error

Parse the contents of `reader`

func ParsePath Uses

func ParsePath(path string, handler Handler) error

Parse the file pointed to by `path`

func ParseSections Uses

func ParseSections(file *os.File, handler Handler, sections ...ElementID) error

Parses only the given sections of `file`.

When present, uses the seek index to avoid having to parse the entire file

Code:

package main

import (
    "fmt"
    "log"
    "os"
    "time"
)

type myTitleParser struct {
}

func (p *myTitleParser) HandleMasterBegin(id ElementID, info ElementInfo) (bool, error) {
    return true, nil
}

func (p *myTitleParser) HandleMasterEnd(id ElementID, info ElementInfo) error {
    return nil
}

func (p *myTitleParser) HandleString(id ElementID, value string, info ElementInfo) error {
    if id == TitleElement {
        fmt.Printf("%s: %v\n", NameForElementID(id), value)
    }
    return nil
}

func (p *myTitleParser) HandleInteger(id ElementID, value int64, info ElementInfo) error {
    return nil
}

func (p *myTitleParser) HandleFloat(id ElementID, value float64, info ElementInfo) error {
    return nil
}

func (p *myTitleParser) HandleDate(id ElementID, value time.Time, info ElementInfo) error {
    return nil
}

func (p *myTitleParser) HandleBinary(id ElementID, value []byte, info ElementInfo) error {
    return nil
}

func main() {
    handler := myTitleParser{}
    file, err := os.Open("example.mkv")
    if err != nil {
        log.Fatalf("%v", err)
    }
    defer file.Close()
    if err = ParseSections(file, &handler, InfoElement); err != nil {
        log.Fatalf("%v", err)
    }
}

type ElementID Uses

type ElementID int64

ElementID represents the EBML ID of an element. The supported EBML IDs are documented in the Matroska specification: https://www.matroska.org/technical/specs/index.html

const (
    EBMLElement                        ElementID = 0x1A45DFA3
    EBMLVersionElement                 ElementID = 0x4286
    EBMLReadVersionElement             ElementID = 0x42F7
    EBMLMaxIDLengthElement             ElementID = 0x42F2
    EBMLMaxSizeLengthElement           ElementID = 0x42F3
    DocTypeElement                     ElementID = 0x4282
    DocTypeVersionElement              ElementID = 0x4287
    DocTypeReadVersionElement          ElementID = 0x4285
    VoidElement                        ElementID = 0xEC
    CRC32Element                       ElementID = 0xBF
    SignatureSlotElement               ElementID = 0x1B538667
    SignatureAlgoElement               ElementID = 0x7E8A
    SignatureHashElement               ElementID = 0x7E9A
    SignaturePublicKeyElement          ElementID = 0x7EA5
    SignatureElement                   ElementID = 0x7EB5
    SignatureElementsElement           ElementID = 0x7E5B
    SignatureElementListElement        ElementID = 0x7E7B
    SignedElementElement               ElementID = 0x6532
    SegmentElement                     ElementID = 0x18538067
    SeekHeadElement                    ElementID = 0x114D9B74
    SeekElement                        ElementID = 0x4DBB
    SeekIDElement                      ElementID = 0x53AB
    SeekPositionElement                ElementID = 0x53AC
    InfoElement                        ElementID = 0x1549A966
    SegmentUIDElement                  ElementID = 0x73A4
    SegmentFilenameElement             ElementID = 0x7384
    PrevUIDElement                     ElementID = 0x3CB923
    PrevFilenameElement                ElementID = 0x3C83AB
    NextUIDElement                     ElementID = 0x3EB923
    NextFilenameElement                ElementID = 0x3E83BB
    SegmentFamilyElement               ElementID = 0x4444
    ChapterTranslateElement            ElementID = 0x6924
    ChapterTranslateEditionUIDElement  ElementID = 0x69FC
    ChapterTranslateCodecElement       ElementID = 0x69BF
    ChapterTranslateIDElement          ElementID = 0x69A5
    TimecodeScaleElement               ElementID = 0x2AD7B1
    DurationElement                    ElementID = 0x4489
    DateUTCElement                     ElementID = 0x4461
    TitleElement                       ElementID = 0x7BA9
    MuxingAppElement                   ElementID = 0x4D80
    WritingAppElement                  ElementID = 0x5741
    ClusterElement                     ElementID = 0x1F43B675
    TimecodeElement                    ElementID = 0xE7
    SilentTracksElement                ElementID = 0x5854
    SilentTrackNumberElement           ElementID = 0x58D7
    PositionElement                    ElementID = 0xA7
    PrevSizeElement                    ElementID = 0xAB
    SimpleBlockElement                 ElementID = 0xA3
    BlockGroupElement                  ElementID = 0xA0
    BlockElement                       ElementID = 0xA1
    BlockVirtualElement                ElementID = 0xA2
    BlockAdditionsElement              ElementID = 0x75A1
    BlockMoreElement                   ElementID = 0xA6
    BlockAddIDElement                  ElementID = 0xEE
    BlockAdditionalElement             ElementID = 0xA5
    BlockDurationElement               ElementID = 0x9B
    ReferencePriorityElement           ElementID = 0xFA
    ReferenceBlockElement              ElementID = 0xFB
    ReferenceVirtualElement            ElementID = 0xFD
    CodecStateElement                  ElementID = 0xA4
    DiscardPaddingElement              ElementID = 0x75A2
    SlicesElement                      ElementID = 0x8E
    TimeSliceElement                   ElementID = 0xE8
    LaceNumberElement                  ElementID = 0xCC
    FrameNumberElement                 ElementID = 0xCD
    BlockAdditionIDElement             ElementID = 0xCB
    DelayElement                       ElementID = 0xCE
    SliceDurationElement               ElementID = 0xCF
    ReferenceFrameElement              ElementID = 0xC8
    ReferenceOffsetElement             ElementID = 0xC9
    ReferenceTimeCodeElement           ElementID = 0xCA
    EncryptedBlockElement              ElementID = 0xAF
    TracksElement                      ElementID = 0x1654AE6B
    TrackEntryElement                  ElementID = 0xAE
    TrackNumberElement                 ElementID = 0xD7
    TrackUIDElement                    ElementID = 0x73C5
    TrackTypeElement                   ElementID = 0x83
    FlagEnabledElement                 ElementID = 0xB9
    FlagDefaultElement                 ElementID = 0x88
    FlagForcedElement                  ElementID = 0x55AA
    FlagLacingElement                  ElementID = 0x9C
    MinCacheElement                    ElementID = 0x6DE7
    MaxCacheElement                    ElementID = 0x6DF8
    DefaultDurationElement             ElementID = 0x23E383
    DefaultDecodedFieldDurationElement ElementID = 0x234E7A
    TrackTimecodeScaleElement          ElementID = 0x23314F
    TrackOffsetElement                 ElementID = 0x537F
    MaxBlockAdditionIDElement          ElementID = 0x55EE
    NameElement                        ElementID = 0x536E
    LanguageElement                    ElementID = 0x22B59C
    LanguageIETFElement                ElementID = 0x22B59D
    CodecIDElement                     ElementID = 0x86
    CodecPrivateElement                ElementID = 0x63A2
    CodecNameElement                   ElementID = 0x258688
    AttachmentLinkElement              ElementID = 0x7446
    CodecSettingsElement               ElementID = 0x3A9697
    CodecInfoURLElement                ElementID = 0x3B4040
    CodecDownloadURLElement            ElementID = 0x26B240
    CodecDecodeAllElement              ElementID = 0xAA
    TrackOverlayElement                ElementID = 0x6FAB
    CodecDelayElement                  ElementID = 0x56AA
    SeekPreRollElement                 ElementID = 0x56BB
    TrackTranslateElement              ElementID = 0x6624
    TrackTranslateEditionUIDElement    ElementID = 0x66FC
    TrackTranslateCodecElement         ElementID = 0x66BF
    TrackTranslateTrackIDElement       ElementID = 0x66A5
    VideoElement                       ElementID = 0xE0
    FlagInterlacedElement              ElementID = 0x9A
    FieldOrderElement                  ElementID = 0x9D
    StereoModeElement                  ElementID = 0x53B8
    AlphaModeElement                   ElementID = 0x53C0
    OldStereoModeElement               ElementID = 0x53B9
    PixelWidthElement                  ElementID = 0xB0
    PixelHeightElement                 ElementID = 0xBA
    PixelCropBottomElement             ElementID = 0x54AA
    PixelCropTopElement                ElementID = 0x54BB
    PixelCropLeftElement               ElementID = 0x54CC
    PixelCropRightElement              ElementID = 0x54DD
    DisplayWidthElement                ElementID = 0x54B0
    DisplayHeightElement               ElementID = 0x54BA
    DisplayUnitElement                 ElementID = 0x54B2
    AspectRatioTypeElement             ElementID = 0x54B3
    ColourSpaceElement                 ElementID = 0x2EB524
    GammaValueElement                  ElementID = 0x2FB523
    FrameRateElement                   ElementID = 0x2383E3
    ColourElement                      ElementID = 0x55B0
    MatrixCoefficientsElement          ElementID = 0x55B1
    BitsPerChannelElement              ElementID = 0x55B2
    ChromaSubsamplingHorzElement       ElementID = 0x55B3
    ChromaSubsamplingVertElement       ElementID = 0x55B4
    CbSubsamplingHorzElement           ElementID = 0x55B5
    CbSubsamplingVertElement           ElementID = 0x55B6
    ChromaSitingHorzElement            ElementID = 0x55B7
    ChromaSitingVertElement            ElementID = 0x55B8
    RangeElement                       ElementID = 0x55B9
    TransferCharacteristicsElement     ElementID = 0x55BA
    PrimariesElement                   ElementID = 0x55BB
    MaxCLLElement                      ElementID = 0x55BC
    MaxFALLElement                     ElementID = 0x55BD
    MasteringMetadataElement           ElementID = 0x55D0
    PrimaryRChromaticityXElement       ElementID = 0x55D1
    PrimaryRChromaticityYElement       ElementID = 0x55D2
    PrimaryGChromaticityXElement       ElementID = 0x55D3
    PrimaryGChromaticityYElement       ElementID = 0x55D4
    PrimaryBChromaticityXElement       ElementID = 0x55D5
    PrimaryBChromaticityYElement       ElementID = 0x55D6
    WhitePointChromaticityXElement     ElementID = 0x55D7
    WhitePointChromaticityYElement     ElementID = 0x55D8
    LuminanceMaxElement                ElementID = 0x55D9
    LuminanceMinElement                ElementID = 0x55DA
    ProjectionElement                  ElementID = 0x7670
    ProjectionTypeElement              ElementID = 0x7671
    ProjectionPrivateElement           ElementID = 0x7672
    ProjectionPoseYawElement           ElementID = 0x7673
    ProjectionPosePitchElement         ElementID = 0x7674
    ProjectionPoseRollElement          ElementID = 0x7675
    AudioElement                       ElementID = 0xE1
    SamplingFrequencyElement           ElementID = 0xB5
    OutputSamplingFrequencyElement     ElementID = 0x78B5
    ChannelsElement                    ElementID = 0x9F
    ChannelPositionsElement            ElementID = 0x7D7B
    BitDepthElement                    ElementID = 0x6264
    TrackOperationElement              ElementID = 0xE2
    TrackCombinePlanesElement          ElementID = 0xE3
    TrackPlaneElement                  ElementID = 0xE4
    TrackPlaneUIDElement               ElementID = 0xE5
    TrackPlaneTypeElement              ElementID = 0xE6
    TrackJoinBlocksElement             ElementID = 0xE9
    TrackJoinUIDElement                ElementID = 0xED
    TrickTrackUIDElement               ElementID = 0xC0
    TrickTrackSegmentUIDElement        ElementID = 0xC1
    TrickTrackFlagElement              ElementID = 0xC6
    TrickMasterTrackUIDElement         ElementID = 0xC7
    TrickMasterTrackSegmentUIDElement  ElementID = 0xC4
    ContentEncodingsElement            ElementID = 0x6D80
    ContentEncodingElement             ElementID = 0x6240
    ContentEncodingOrderElement        ElementID = 0x5031
    ContentEncodingScopeElement        ElementID = 0x5032
    ContentEncodingTypeElement         ElementID = 0x5033
    ContentCompressionElement          ElementID = 0x5034
    ContentCompAlgoElement             ElementID = 0x4254
    ContentCompSettingsElement         ElementID = 0x4255
    ContentEncryptionElement           ElementID = 0x5035
    ContentEncAlgoElement              ElementID = 0x47E1
    ContentEncKeyIDElement             ElementID = 0x47E2
    ContentEncAESSettingsElement       ElementID = 0x47E7
    AESSettingsCipherModeElement       ElementID = 0x47E8
    ContentSignatureElement            ElementID = 0x47E3
    ContentSigKeyIDElement             ElementID = 0x47E4
    ContentSigAlgoElement              ElementID = 0x47E5
    ContentSigHashAlgoElement          ElementID = 0x47E6
    CuesElement                        ElementID = 0x1C53BB6B
    CuePointElement                    ElementID = 0xBB
    CueTimeElement                     ElementID = 0xB3
    CueTrackPositionsElement           ElementID = 0xB7
    CueTrackElement                    ElementID = 0xF7
    CueClusterPositionElement          ElementID = 0xF1
    CueRelativePositionElement         ElementID = 0xF0
    CueDurationElement                 ElementID = 0xB2
    CueBlockNumberElement              ElementID = 0x5378
    CueCodecStateElement               ElementID = 0xEA
    CueReferenceElement                ElementID = 0xDB
    CueRefTimeElement                  ElementID = 0x96
    CueRefClusterElement               ElementID = 0x97
    CueRefNumberElement                ElementID = 0x535F
    CueRefCodecStateElement            ElementID = 0xEB
    AttachmentsElement                 ElementID = 0x1941A469
    AttachedFileElement                ElementID = 0x61A7
    FileDescriptionElement             ElementID = 0x467E
    FileNameElement                    ElementID = 0x466E
    FileMimeTypeElement                ElementID = 0x4660
    FileDataElement                    ElementID = 0x465C
    FileUIDElement                     ElementID = 0x46AE
    FileReferralElement                ElementID = 0x4675
    FileUsedStartTimeElement           ElementID = 0x4661
    FileUsedEndTimeElement             ElementID = 0x4662
    ChaptersElement                    ElementID = 0x1043A770
    EditionEntryElement                ElementID = 0x45B9
    EditionUIDElement                  ElementID = 0x45BC
    EditionFlagHiddenElement           ElementID = 0x45BD
    EditionFlagDefaultElement          ElementID = 0x45DB
    EditionFlagOrderedElement          ElementID = 0x45DD
    ChapterAtomElement                 ElementID = 0xB6
    ChapterUIDElement                  ElementID = 0x73C4
    ChapterStringUIDElement            ElementID = 0x5654
    ChapterTimeStartElement            ElementID = 0x91
    ChapterTimeEndElement              ElementID = 0x92
    ChapterFlagHiddenElement           ElementID = 0x98
    ChapterFlagEnabledElement          ElementID = 0x4598
    ChapterSegmentUIDElement           ElementID = 0x6E67
    ChapterSegmentEditionUIDElement    ElementID = 0x6EBC
    ChapterPhysicalEquivElement        ElementID = 0x63C3
    ChapterTrackElement                ElementID = 0x8F
    ChapterTrackNumberElement          ElementID = 0x89
    ChapterDisplayElement              ElementID = 0x80
    ChapStringElement                  ElementID = 0x85
    ChapLanguageElement                ElementID = 0x437C
    ChapLanguageIETFElement            ElementID = 0x437D
    ChapCountryElement                 ElementID = 0x437E
    ChapProcessElement                 ElementID = 0x6944
    ChapProcessCodecIDElement          ElementID = 0x6955
    ChapProcessPrivateElement          ElementID = 0x450D
    ChapProcessCommandElement          ElementID = 0x6911
    ChapProcessTimeElement             ElementID = 0x6922
    ChapProcessDataElement             ElementID = 0x6933
    TagsElement                        ElementID = 0x1254C367
    TagElement                         ElementID = 0x7373
    TargetsElement                     ElementID = 0x63C0
    TargetTypeValueElement             ElementID = 0x68CA
    TargetTypeElement                  ElementID = 0x63CA
    TagTrackUIDElement                 ElementID = 0x63C5
    TagEditionUIDElement               ElementID = 0x63C9
    TagChapterUIDElement               ElementID = 0x63C4
    TagAttachmentUIDElement            ElementID = 0x63C6
    SimpleTagElement                   ElementID = 0x67C8
    TagNameElement                     ElementID = 0x45A3
    TagLanguageElement                 ElementID = 0x447A
    TagLanguageIETFElement             ElementID = 0x447B
    TagDefaultElement                  ElementID = 0x4484
    TagStringElement                   ElementID = 0x4487
    TagBinaryElement                   ElementID = 0x4485
)

Supported ElementIDs

type ElementInfo Uses

type ElementInfo struct {
    Offset int64
    Size   int64
    Level  int
}

ElementInfo contains information about an element encountered in the stream, and is passed to the handler by the parser on parse events.

type Handler Uses

type Handler interface {
    // Return `true` to descend into the element, `false` to skip this element's children.
    HandleMasterBegin(ElementID, ElementInfo) (bool, error)
    HandleMasterEnd(ElementID, ElementInfo) error
    HandleString(ElementID, string, ElementInfo) error
    HandleInteger(ElementID, int64, ElementInfo) error
    HandleFloat(ElementID, float64, ElementInfo) error
    HandleDate(ElementID, time.Time, ElementInfo) error
    HandleBinary(ElementID, []byte, ElementInfo) error
}

Handler declares an interface for handling parse events

Package mkvparse imports 9 packages (graph). Updated 2020-06-15. Refresh now. Tools for package owners.