Skip to content

yartat/MP-MediaInfo

Repository files navigation

MP-MediaInfo

MP-MediaInfo is .NET wrapper for MediaArea MediaInfo and use native packages NuGet Badge and NuGet Badge.

License Build Core Build

Features

  • Comprehensive Media Analysis: Wraps the MediaInfo library to provide detailed information about video, audio, subtitle, and metadata streams
  • Rich Property Access: Exposes properties for video codecs, bitrates, resolution, frame rates, audio properties, subtitles, chapters, and extensive tag information
  • Multi-Format Support: Supports analysis of virtually all video and audio formats supported by MediaInfo (see Supported Formats)
  • Stream Information: Provides detailed access to individual video streams, audio streams, subtitle streams, chapters, and menu information
  • Metadata Extraction: Extract technical tags and general metadata from media files
  • Cross-Platform: Targets .NET Framework 4.0+, .NET Standard 2.1, .NET 6.0, .NET 8.0, and .NET 10.0
  • Optional Logging: Built-in support for custom logging to track analysis operations

Available packages

Framework Package
.NET Framework 4.0 NuGet Badge
.NET Framework 4.5 NuGet Badge
.NET Standard 2.1 NuGet Badge
.NET 6.0 NuGet Badge
.NET 8.0 NuGet Badge
.NET 10.0 NuGet Badge

Installation

Two packages are available:

  • MediaInfo.Wrapper.Core - For .NET Standard 2.1, .NET 6.0+. Recommended for modern applications, cross-platform projects, and ASP.NET Core services
  • MediaInfo.Wrapper - For .NET Framework 4.0+. Use this if you're on Windows only with .NET Framework

Choose based on your target framework:

.NET Core

dotnet add package MediaInfo.Wrapper.Core --version 26.1.0

.NET Framework

Install-Package MediaInfo.Wrapper -Version 26.1.0

Usage

Basic Setup

Add to usings:

using MediaInfo;

Instantiate a MediaInfoWrapper with the path to your media file:

var media = new MediaInfoWrapper("path/to/media/file.mp4");

Checking Analysis Success

Always verify the file was successfully analyzed:

if (media.Success)
{
    // File analyzed successfully
}
else
{
    // Handle analysis failure
    Console.WriteLine("Failed to analyze media file");
}

General Information

Access basic media information:

var containerFormat = media.Format;              // e.g., "MPEG-4"
var duration = media.Duration;                  // Duration in milliseconds
var overallBitRate = media.OverallBitRate;     // Combined bitrate of all streams
var isScanningNeeded = media.ScanningNeeded;   // Whether file needs deeper analysis

Video Stream Analysis

Extract detailed video information:

if (media.HasVideo)
{
    var videoStream = media.VideoStreams.FirstOrDefault();
    if (videoStream != null)
    {
        var width = videoStream.Width;                          // Resolution width
        var height = videoStream.Height;                        // Resolution height
        var codec = videoStream.Codec;                          // e.g., "AVC"
        var frameRate = videoStream.FrameRate;                  // Frames per second
        var frameRateMode = videoStream.FrameRateMode;         // e.g., CFR, VFR
        var bitRate = videoStream.BitRate;                     // Video stream bitrate
        var standard = videoStream.Standard;                   // e.g., "NTSC", "PAL"
        var aspectRatio = videoStream.AspectRatio;             // e.g., "16:9"
        var chromaSubSampling = videoStream.ChromaSubSampling; // e.g., "4:2:0"
        var colorSpace = videoStream.ColorSpace;               // e.g., "YUV"
        var hdrFormat = videoStream.Hdr;                       // e.g., "HDR10", "Dolby Vision"
        var profile = videoStream.Profile;                     // Codec profile
        var level = videoStream.Level;                         // Profile level
    }
}

Audio Stream Analysis

Access audio track information:

foreach (var audioStream in media.AudioStreams)
{
    var language = audioStream.Language;                    // ISO 639-2 language code
    var codec = audioStream.Codec;                          // e.g., "AAC", "AC-3"
    var bitRate = audioStream.BitRate;                     // Audio bitrate
    var channels = audioStream.Channels;                   // Number of audio channels
    var channelLayout = audioStream.ChannelLayout;         // e.g., "L R C LFE Ls Rs"
    var samplingRate = audioStream.SamplingRate;           // Sample rate in Hz
    var bitDepth = audioStream.BitDepth;                   // Bits per sample
    var bitrateMode = audioStream.BitrateMode;             // CBR, VBR, etc.
    var title = audioStream.Title;                         // Track title if available
}

Subtitle Stream Analysis

Retrieve subtitle information:

foreach (var subtitleStream in media.SubtitleStreams)
{
    var codec = subtitleStream.Codec;              // e.g., "PGS", "ASS"
    var language = subtitleStream.Language;        // ISO 639-2 language code
    var title = subtitleStream.Title;              // Subtitle track name
}

Audio and Video Tags

Extract metadata:

var audioTags = media.AudioTags;       // Audio metadata (album, artist, etc.)
var videoTags = media.VideoTags;       // Video metadata (title, description, etc.)
var generalInfo = media.GeneralTags;   // File-level metadata

if (audioTags != null)
{
    var artist = audioTags.Performer;
    var album = audioTags.Album;
    var title = audioTags.Title;
}

Chapter Information

Access chapter/menu information:

foreach (var chapter in media.ChapterStreams)
{
    var startTime = chapter.StartTime;  // Chapter start timestamp
    // Chapter stream information available
}

Using a Logger

Optionally provide a logger to track analysis:

public class ConsoleLogger : ILogger
{
    public void Log(string message) => Console.WriteLine(message);
}

var logger = new ConsoleLogger();
var media = new MediaInfoWrapper("path/to/file.mp4", logger);

Supported Formats

MP-MediaInfo supports analysis of virtually all media formats supported by the underlying MediaInfo library, including:

Video Formats: MP4, MKV, AVI, MOV, FLV, WebM, WMV, 3GP, and many more

Audio Codecs: H.264/AVC, H.265/HEVC, MPEG-4, VP8, VP9, AV1, Theora, and others

Audio Formats: MP3, AAC, FLAC, Opus, Vorbis, AC-3, DTS, TrueHD, Atmos, and more

Subtitle Formats: PGS, ASS/SSA, SRT, SUBRIP, DVB, and other subtitle types

For a complete and detailed list, refer to the MediaInfo documentation.

Advanced Usage

Error Handling

Handle potential errors gracefully:

try
{
    var media = new MediaInfoWrapper(filePath);
    
    if (!media.Success)
    {
        Console.WriteLine("Analysis was not successful");
        return;
    }
    
    // Process media information
}
catch (FileNotFoundException)
{
    Console.WriteLine("Media file not found");
}
catch (Exception ex)
{
    Console.WriteLine($"Error analyzing media: {ex.Message}");
}

Batch Processing Multiple Files

Analyze multiple files efficiently:

var mediaFiles = Directory.GetFiles("mediaFolder", "*.mp4");

foreach (var file in mediaFiles)
{
    try
    {
        var media = new MediaInfoWrapper(file);
        if (media.Success)
        {
            Console.WriteLine($"{Path.GetFileName(file)}: {media.Format}");
            // Process each file
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error processing {file}: {ex.Message}");
    }
}

Checking Specific Media Characteristics

var media = new MediaInfoWrapper(filePath);

// Check if file has video
if (media.HasVideo)
{
    var videoStream = media.VideoStreams.FirstOrDefault();
    if (videoStream?.Hdr != null)
    {
        Console.WriteLine($"HDR Format: {videoStream.Hdr}");
    }
}

// Check number of audio tracks
Console.WriteLine($"Audio tracks: {media.AudioStreams.Count}");

// Find specific audio codec
var ac3Tracks = media.AudioStreams
    .Where(s => s.Codec?.Contains("AC-3") ?? false)
    .ToList();

Troubleshooting

Analysis Returns False (media.Success == false)

  • Verify the file path is correct and the file exists
  • Ensure the file is not corrupted
  • Check that the media format is supported
  • On Linux/macOS, verify all dependencies (libzen, zlib) are installed
  • Try enabling logging to see detailed error messages

Missing Native Libraries

Windows: The native MediaInfo libraries are included in the NuGet package. No additional installation needed.

Linux/macOS: Install system dependencies as described in the Dependencies section.

Incomplete Information

Some media files may require deeper scanning. Check media.ScanningNeeded:

if (media.ScanningNeeded)
{
    Console.WriteLine("File may benefit from deeper analysis");
    // Files with streaming headers might have incomplete information
}

Out of Memory Issues

When processing very large files or many files in parallel, be mindful of memory usage. Consider disposing of MediaInfoWrapper objects when done:

using (var media = new MediaInfoWrapper(filePath))
{
    // Use media
}
// Disposed automatically

Demo application

ASP.NET Core demo application is available which shows the usage of the package, serialization and running from the docker container. Code from this demo should not be used in production code, the code is merely to demonstrate the usage of this package.

Dependencies

Make sure that the following dependencies are installed in the operating system before starting the project

.NET Core package supports next operating systems

Operation system Version
MacOS 10.15 (Catalina), 11 (Big Sur)
Ubuntu 16.04, 18.04, 20.04 and 21.04
CenOS 7 and above
Fedora 32 and above
OpenSUSE 15.2 and Tumbleweed
RedHat 7 and above
Debian 9 and above
Arch Linux
Windows 7 and above
Docker buster

MacOS

Some dependencies are available with MacPorts. To install MacPorts: https://guide.macports.org/#installing

port install zlib curl zenlib

Ubuntu

sudo apt-get update
sudo apt-get install libzen0v5 libmms0 zlib1g zlibc libnghttp2-14 librtmp1 curl libcurl4-gnutls-dev libglib2.0-dev

CentOS

CentOS 7

sudo rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum -y update
sudo yum -y install zlib curl libzen bzip2 libcurl
sudo rpm -ivh https://download1.rpmfusion.org/free/el/updates/7/x86_64/l/libmms-0.6.4-2.el7.x86_64.rpm

CentOS 8

sudo rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
sudo yum -y update
sudo yum -y install zlib curl libzen bzip2 libcurl
sudo rpm -ivh https://download1.rpmfusion.org/free/el/updates/8/x86_64/l/libmms-0.6.4-8.el8.x86_64.rpm

Fedora

sudo dnf update
sudo dnf -y install zlib curl libzen openssl libmms

OpenSUSE

sudo zypper refresh
sudo zypper update -y
sudo zypper install -y zlib curl libmms0 openssl libnghttp2-14

RedHat

RedHat 7

sudo rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum -y update
sudo yum -y install zlib curl libzen bzip2 libcurl
sudo rpm -ivh https://download1.rpmfusion.org/free/el/updates/7/x86_64/l/libmms-0.6.4-2.el7.x86_64.rpm

RedHat 8

sudo rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
sudo yum -y update
sudo yum -y install zlib curl libzen bzip2 libcurl
sudo rpm -ivh https://download1.rpmfusion.org/free/el/updates/8/x86_64/l/libmms-0.6.4-8.el8.x86_64.rpm

Debian

sudo apt-get update
sudo apt-get install libzen0v5 libmms0 openssl zlib1g zlibc libnghttp2-14 librtmp1 curl libcurl4-gnutls-dev libglib2.0

Windows

Windows package contains all dependencies and does not required any actions.

ArchLinux

sudo pacman -Syu
sudo pacman -S libcurl-gnutls libzen libmms libssh librtmp0

Docker

.NET Core 3.1

FROM mcr.microsoft.com/dotnet/aspnet:3.1
RUN apt-get update && apt-get install -y libzen0v5 libmms0 openssl zlib1g zlibc libnghttp2-14 librtmp1 curl libcurl4-gnutls-dev libglib2.0

.NET 6.0

FROM mcr.microsoft.com/dotnet/aspnet:6.0
RUN apt-get update && apt-get install -y libzen0v5 libmms0 openssl zlib1g zlibc libnghttp2-14 librtmp1 curl libcurl4-gnutls-dev libglib2.0

.NET 8.0

FROM mcr.microsoft.com/dotnet/aspnet:8.0
RUN apt-get update && apt-get install -y libzen0v5 libmms0 openssl zlib1g zlibc libnghttp2-14 librtmp1 curl libcurl4-gnutls-dev libglib2.0

.NET 10.0

FROM mcr.microsoft.com/dotnet/aspnet:10.0
RUN apt-get update && apt-get install -y libzen0v5 libmms0 openssl zlib1g zlibc libnghttp2-14 librtmp1 curl libcurl4-gnutls-dev libglib2.0

About

Mediaportal MediaInfoLib wrapper

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages