SpawnDev.WebMParser 1.0.1

dotnet add package SpawnDev.WebMParser --version 1.0.1                
NuGet\Install-Package SpawnDev.WebMParser -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SpawnDev.WebMParser" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SpawnDev.WebMParser --version 1.0.1                
#r "nuget: SpawnDev.WebMParser, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SpawnDev.WebMParser as a Cake Addin
#addin nuget:?package=SpawnDev.WebMParser&version=1.0.1

// Install SpawnDev.WebMParser as a Cake Tool
#tool nuget:?package=SpawnDev.WebMParser&version=1.0.1                

WebMParser

WebMParser is a .Net WebM parser written in C#.

The initial goal of this library is to allow adding a duration to WebM video files recorded using MediaRecorder in a web browser. I am using this library in a browser based Blazor WebAssembly video messaging application.

The demo project included with the library is a .Net core 8 console app that currently just allows testing the library.

To fix the duration in a WebM file, WebM parser reads the Timecode information from Clusters and SimpleBlocks and adds a Segment > Info > Duration element with the new duration.

Example of how to add Duration info if not found in a webm stream.


using var inputStream = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read);

var webm = new WebMStreamParser(inputStream);

// FixDuration returns true if the WebM was modified
var modified = webm.FixDuration();
// webm.Modified will also be true if the WebM is modified
if (modified)
{
    var outFile = Path.Combine(Path.GetDirectoryName(inputFile)!, Path.GetFileNameWithoutExtension(inputFile) + ".fixed" + Path.GetExtension(inputFile));
    using var outputStream = new FileStream(outFile, FileMode.Create, FileAccess.Write, FileShare.None);
    webm.CopyTo(outputStream);
}

Example that prints out basic info for every element found in the WebM stream

var elements = webm.Descendants;
foreach (var element in elements)
{
    var indent = new string('-', element.IdChain.Length - 1);
    Console.WriteLine($"{indent}{element}");
}

Example of how to get an element

var durationElement = webm.GetElement<FloatElement>(ElementId.Segment, ElementId.Info, ElementId.Duration);
var duration = durationElement?.Data ?? 0;

Example of how to get all elements of a type

var segments = webm.GetElements<ContainerElement>(ElementId.Segment);

Example of how to use ElementIds to walk the data tree and access information

var segments = webm.GetContainers(ElementId.Segment);
foreach (var segment in segments)
{
    var clusters = segment.GetContainers(ElementId.Cluster);
    foreach (var cluster in clusters)
    {
        var timecode = cluster.GetElement<UintElement>(ElementId.Timecode);
        if (timecode != null)
        {
            duration = timecode.Data;
        };
        var simpleBlocks = cluster.GetElements<SimpleBlockElement>(ElementId.SimpleBlock);
        var simpleBlockLast = simpleBlocks.LastOrDefault();
        if (simpleBlockLast != null)
        {
            duration += simpleBlockLast.Timecode;
        }
    }
}

Example of how to add an element
All parent containers are automatically marked Modified if any children are added, removed, or changed.

var info = GetContainer(ElementId.Segment, ElementId.Info);
info!.Add(ElementId.Duration, 100000);
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 192 1/7/2024
1.0.0 137 1/6/2024