# Comprehensive Guide to Reading Media File Information in C#

Video Capture SDK .Net Video Edit SDK .Net Media Player SDK .Net

Accessing detailed information embedded within media files is a fundamental requirement for a wide range of applications, from sophisticated media players and video editors to content management systems and file analysis tools. Understanding properties like codecs, resolution, frame rate, bitrate, duration, and embedded tags allows developers to build more intelligent and user-friendly applications. For instance, a media player might use this information to display file details to the user, while a video editor could use it to configure project settings automatically.

This guide provides a step-by-step walkthrough on how to read comprehensive information from video and audio files using C# and the powerful VisioForge SDKs. Specifically, we will leverage the MediaInfoReader class, available within the VisioForge.Core assembly, which is a common component across several VisioForge .Net products:

Whether you are developing a custom video player, an automated transcoding service, or simply need to catalog a media library, the MediaInfoReader provides a convenient and efficient way to extract the necessary metadata.

# Step-by-Step Implementation

Let's break down the process of reading media file information into manageable steps. The following examples assume a WinForms application context where mmInfo is a TextBox control used for displaying the extracted information, and filename is a string variable holding the path to the media file you want to analyze.

# 1. Instantiate the MediaInfoReader

The first step is to create an instance of the MediaInfoReader class. This object will be our primary tool for interacting with the media file and retrieving its properties.

// Import the necessary namespace
using VisioForge.Core.MediaInfo; // Namespace for MediaInfoReader
using VisioForge.Core.Helpers;  // Namespace for TagLibHelper (optional)

// ... inside your method or class ...

// Create an instance of MediaInfoReader
var infoReader = new MediaInfoReader();

This simple line initializes the reader, making it ready to process a file.

# 2. Verify File Playability (Optional but Recommended)

Before attempting to read detailed information, it's often useful to perform a quick check to see if the file is likely playable or supported by the underlying media frameworks. The MediaInfoReader provides a static method IsFilePlayable for this purpose. While not strictly necessary for reading metadata (as metadata might be readable even if playback fails), it can help pre-emptively identify corrupted or unsupported files.

// Define variables to hold potential error information
FilePlaybackError errorCode;
string errorText;

// Specify the path to the media file
string filename = @"C:\path\to\your\mediafile.mp4"; // Replace with your actual file path

// Check if the file is playable
if (MediaInfoReader.IsFilePlayable(filename, out errorCode, out errorText))
{
    // Display success message (e.g., in a TextBox)
    mmInfo.Text += "Status: This file appears to be playable." + Environment.NewLine;
}
else
{
    // Display error message including the error code and description
    mmInfo.Text += $"Status: This file might not be playable. Error: {errorCode} - {errorText}" + Environment.NewLine;
}

mmInfo.Text += "------------------------------------" + Environment.NewLine;

This step provides early feedback on the file's integrity and compatibility. The errorCode (an enum of type FilePlaybackError) and errorText provide more details if the check fails.

# 3. Read Detailed File Information

This is the core step where we extract the rich metadata. You need to assign the file path to the Filename property of the infoReader object and then call the ReadFileInfo method. The boolean parameter in ReadFileInfo(true) typically indicates whether to perform a full analysis, which is recommended for getting the most complete information.

try
{
    // Assign the filename to the reader
    infoReader.Filename = filename;

    // Read the file information (true for full analysis)
    infoReader.ReadFileInfo(true);

    // --- Process Video Streams ---
    mmInfo.Text += $"Found {infoReader.VideoStreams.Count} video stream(s)." + Environment.NewLine;
    for (int i = 0; i < infoReader.VideoStreams.Count; i++)
    {
        var stream = infoReader.VideoStreams[i];

        mmInfo.Text += Environment.NewLine; // Add spacing
        mmInfo.Text += $"--- Video Stream #{i + 1} ---" + Environment.NewLine;
        mmInfo.Text += $"  Codec: {stream.Codec}" + Environment.NewLine;
        mmInfo.Text += $"  Duration: {stream.Duration}" + Environment.NewLine; // Typically TimeSpan
        mmInfo.Text += $"  Dimensions: {stream.Width}x{stream.Height}" + Environment.NewLine;
        mmInfo.Text += $"  FOURCC: {stream.FourCC}" + Environment.NewLine; // Four-character code identifying the codec
        if (stream.AspectRatio != null && stream.AspectRatio.Item1 > 0 && stream.AspectRatio.Item2 > 0)
        {
             mmInfo.Text += $"  Aspect Ratio: {stream.AspectRatio.Item1}:{stream.AspectRatio.Item2}" + Environment.NewLine;
        }
        mmInfo.Text += $"  Frame Rate: {stream.FrameRate:F2} fps" + Environment.NewLine; // Format for readability
        mmInfo.Text += $"  Bitrate: {stream.Bitrate / 1000.0:F0} kbps" + Environment.NewLine; // Convert to kbps
        mmInfo.Text += $"  Frames Count: {stream.FramesCount}" + Environment.NewLine;
    }

    // --- Process Audio Streams ---
    mmInfo.Text += Environment.NewLine;
    mmInfo.Text += $"Found {infoReader.AudioStreams.Count} audio stream(s)." + Environment.NewLine;
    for (int i = 0; i < infoReader.AudioStreams.Count; i++)
    {
        var stream = infoReader.AudioStreams[i];

        mmInfo.Text += Environment.NewLine; // Add spacing
        mmInfo.Text += $"--- Audio Stream #{i + 1} ---" + Environment.NewLine;
        mmInfo.Text += $"  Codec: {stream.Codec}" + Environment.NewLine;
        mmInfo.Text += $"  Codec Info: {stream.CodecInfo}" + Environment.NewLine;
        mmInfo.Text += $"  Duration: {stream.Duration}" + Environment.NewLine;
        mmInfo.Text += $"  Bitrate: {stream.Bitrate / 1000.0:F0} kbps" + Environment.NewLine; // Convert to kbps
        mmInfo.Text += $"  Channels: {stream.Channels}" + Environment.NewLine; // e.g., 2 for stereo
        mmInfo.Text += $"  Sample Rate: {stream.SampleRate} Hz" + Environment.NewLine;
        mmInfo.Text += $"  Bits Per Sample (BPS): {stream.BPS}" + Environment.NewLine;
        mmInfo.Text += $"  Language: {stream.Language}" + Environment.NewLine; // If available
    }

    // --- Process Subtitle Streams ---
    mmInfo.Text += Environment.NewLine;
    mmInfo.Text += $"Found {infoReader.Subtitles.Count} subtitle stream(s)." + Environment.NewLine;
    for (int i = 0; i < infoReader.Subtitles.Count; i++)
    {
        var stream = infoReader.Subtitles[i];

        mmInfo.Text += Environment.NewLine; // Add spacing
        mmInfo.Text += $"--- Subtitle Stream #{i + 1} ---" + Environment.NewLine;
        mmInfo.Text += $"  Codec/Format: {stream.Codec}" + Environment.NewLine; // e.g., SRT, ASS
        mmInfo.Text += $"  Name: {stream.Name}" + Environment.NewLine; // Optional name/title
        mmInfo.Text += $"  Language: {stream.Language}" + Environment.NewLine; // e.g., "eng", "fre"
    }
}
catch (Exception ex)
{
    // Handle potential errors during file reading
    mmInfo.Text += $"{Environment.NewLine}Error reading file info: {ex.Message}{Environment.NewLine}";
}
finally
{
    // Important: Dispose the reader to release file handles and resources
    infoReader.Dispose();
}

The infoReader object exposes collections like VideoStreams, AudioStreams, and Subtitles. Each element in these collections represents a distinct stream within the media file and contains numerous properties detailing its characteristics. The code iterates through each collection, extracting and displaying relevant information for every stream found.

Error Handling and Disposal: It's crucial to wrap the ReadFileInfo call and subsequent processing in a try...catch block to handle exceptions that might occur if the file is corrupted, inaccessible, or in an unexpected format. Equally important is calling the Dispose() method on the infoReader object (preferably in a finally block) to ensure that any file handles or system resources used by the reader are properly released. Failure to dispose might lead to file locking issues.

# 4. Read Metadata Tags (Optional)

Beyond technical stream information, media files often contain metadata tags storing information like title, artist, album, genre, comments, etc. VisioForge SDKs provide a helper class, TagLibHelper, often utilizing the popular TagLib# library internally, to read (and potentially write) these tags.

// --- Read Metadata Tags ---
mmInfo.Text += Environment.NewLine + "--- Metadata Tags ---" + Environment.NewLine;
try
{
    // Use TagLibHelper to read tags from the file
    var tags = TagLibHelper.ReadTags(filename);

    // Check if tags were successfully read
    if (tags != null)
    {
        // The 'tags' object usually has properties like Title, Performers, Album, Year, etc.
        // The specific properties depend on the tag format (ID3, VorbisComment, etc.)
        // You can access individual properties or use ToString() for a summary.
        mmInfo.Text += $"Title: {tags.Title}" + Environment.NewLine;
        mmInfo.Text += $"Artist(s): {string.Join(", ", tags.Performers ?? new string[0])}" + Environment.NewLine;
        mmInfo.Text += $"Album: {tags.Album}" + Environment.NewLine;
        mmInfo.Text += $"Year: {tags.Year}" + Environment.NewLine;
        mmInfo.Text += $"Genre: {string.Join(", ", tags.Genres ?? new string[0])}" + Environment.NewLine;
        mmInfo.Text += $"Comment: {tags.Comment}" + Environment.NewLine;

        // For a more comprehensive dump (might be verbose):
        // mmInfo.Text += tags.ToString();
    }
    else
    {
        mmInfo.Text += "No standard metadata tags found or readable." + Environment.NewLine;
    }
}
catch (Exception ex)
{
    // Handle errors during tag reading (e.g., file not found, corrupted tags)
    mmInfo.Text += $"Error reading tags: {ex.Message}" + Environment.NewLine;
}

# Required redists

Refer to the specific VisioForge SDK's deployment guide for detailed information on required redistributables.


This guide provides a robust foundation for reading media file information in your C# applications using VisioForge SDKs. By leveraging the MediaInfoReader and TagLibHelper, you can extract a wealth of data to enhance your application's features and user experience.

For more advanced scenarios, explore the full capabilities of the VisioForge SDKs and consult their detailed documentation. Visit our GitHub page to discover more code samples and examples.