Class MediaType
Represents media format parameters for audio, video, text, or binary streams.
public class MediaType
- Inheritance
-
MediaType
- Inherited Members
Remarks
This class encapsulates codec information, timing parameters, and content-specific properties such as resolution and framerate for video, or sample rate and channels for audio.
Constructors
MediaType()
Constructs an empty media type with default values. ContentType and CodecId are set to Unknown, Timescale to 100ns units.
public MediaType()
Fields
InternalTimescale
Default internal timescale using 100-nanosecond units (1/10,000,000 seconds). This matches the timescale used by DirectShow and Media Foundation.
public static readonly Rational InternalTimescale
Field Value
KeyframeIntervalAttribute
Metadata key for keyframe interval (GOP size) in video streams.
public static readonly string KeyframeIntervalAttribute
Field Value
LevelAttribute
Metadata key for codec level (e.g., H.264 level 4.1).
public static readonly string LevelAttribute
Field Value
ProfileAttribute
Metadata key for codec profile (e.g., H.264 High Profile).
public static readonly string ProfileAttribute
Field Value
QualityAttribute
Metadata key for encoding quality setting (e.g., MJPEG quality 1-100).
public static readonly string QualityAttribute
Field Value
StrideAttribute
Metadata key for row stride (bytes per row) in uncompressed video.
public static readonly string StrideAttribute
Field Value
WellKnownFramerates
Array of commonly used video framerates for matching approximate values to exact rationals. Includes NTSC (23.976, 29.97, 59.94, 119.88), PAL (25, 50), and integer framerates.
public static readonly Rational[] WellKnownFramerates
Field Value
- Rational[]
Properties
Attachments
Gets or sets the attachments dictionary. For internal use only.
public Dictionary<string, object> Attachments { get; set; }
Property Value
Bitrate
Gets or sets the bitrate in bits per second.
public int Bitrate { get; set; }
Property Value
BitsPerSample
Gets the bit depth for PCM audio. Derived from SampleFormat. Setter exists for compatibility but is ignored.
public int BitsPerSample { get; set; }
Property Value
ChannelLayout
Gets or sets the audio channel layout bitmask.
public ulong ChannelLayout { get; set; }
Property Value
Channels
Gets or sets the number of audio channels.
public int Channels { get; set; }
Property Value
CodecId
Gets or sets the codec identifier.
public Codec CodecId { get; set; }
Property Value
CodecPrivateData
Gets or sets the codec-specific configuration data (e.g., SPS/PPS for H.264, AudioSpecificConfig for AAC). Required for proper decoder initialization of most compressed codecs.
public byte[] CodecPrivateData { get; set; }
Property Value
- byte[]
CodecPrivateDataString
Gets the codec private data as a lowercase hexadecimal string. Returns an empty string if CodecPrivateData is null.
public string CodecPrivateDataString { get; }
Property Value
Compressed
Gets a value indicating whether the media data is compressed. Derived from CodecId. Setter exists for compatibility but is ignored.
public bool Compressed { get; set; }
Property Value
ContentType
Gets or sets the content type (Audio, Video, Text, Binary, etc.).
public ContentType ContentType { get; set; }
Property Value
FixedSizeSamples
Gets or sets a value indicating whether media frames have a fixed size. Auto-detected based on codec if not explicitly set. Setter only applies to certain codecs (MP1, MP2, MP3, EAC3, DTS).
public bool FixedSizeSamples { get; set; }
Property Value
FrameDuration
Gets or sets the frame duration in timescale units.
public long FrameDuration { get; set; }
Property Value
FrameSize
Gets or sets the default frame size in bytes. Only meaningful for codecs with fixed frame sizes.
public int FrameSize { get; set; }
Property Value
Framerate
Gets or sets the video framerate as a rational number.
public Rational Framerate { get; set; }
Property Value
Height
Gets or sets the video height in pixels.
public int Height { get; set; }
Property Value
IsComplete
Gets a value indicating whether this media type has all required parameters set. For audio: requires SampleRate, Channels, and Bitrate (if compressed). AAC also requires CodecPrivateData. For video: requires Width, Height, and Bitrate (if compressed). H.264/H.265/MPEG requires CodecPrivateData.
public bool IsComplete { get; }
Property Value
Metadata
Gets or sets the metadata dictionary containing additional stream properties.
public Dictionary<string, string> Metadata { get; set; }
Property Value
PixelAspectRatio
Gets or sets the pixel aspect ratio for anamorphic video.
public Rational PixelAspectRatio { get; set; }
Property Value
PixelFormat
Gets or sets the video pixel format. Only meaningful for uncompressed video.
public PixelFormat PixelFormat { get; set; }
Property Value
ProfileLevel
Gets or sets the codec profile and level. Auto-extracted from CodecPrivateData for MPEG2, MPEG4, H.264, and AAC codecs.
public long ProfileLevel { get; set; }
Property Value
ReorderingQueueDepth
Gets or sets the reordering queue depth. Auto-extracted from CodecPrivateData for H.264/H.265. Used for video with B-frames.
public int ReorderingQueueDepth { get; set; }
Property Value
SampleFormat
Gets or sets the audio sample format. Only meaningful for uncompressed audio (PCM).
public SampleFormat SampleFormat { get; set; }
Property Value
SampleRate
Gets or sets the audio sampling rate in Hz.
public int SampleRate { get; set; }
Property Value
Timescale
Gets or sets the timescale. Default is 100ns units, matching DirectShow and Media Foundation.
public Rational Timescale { get; set; }
Property Value
Width
Gets or sets the video width in pixels.
public int Width { get; set; }
Property Value
Methods
Clone()
Creates a deep copy of this media type instance. All properties are copied, including CodecPrivateData array and Metadata/Attachments dictionaries.
public MediaType Clone()
Returns
- MediaType
A new MediaType instance with identical property values.
Equals(object)
Determines whether this media type is equal to another object. Compares ContentType, CodecId, Bitrate, Timescale, CodecPrivateData, and content-specific properties.
public override bool Equals(object obj)
Parameters
objobjectThe object to compare with this instance.
Returns
- bool
True if the objects are equal; otherwise, false.
Remarks
The following properties are excluded from comparison: FrameDuration (can fluctuate), FrameSize (estimation only), ReorderingQueueDepth (initial value only), Compressed (derived from CodecId), FixedSizeSamples (usually derived), ProfileLevel (contained in CodecPrivateData).
FromParameterString(string, bool)
Deserializes a media type from a URL query string format.
public static MediaType FromParameterString(string pars, bool useStringEnums = true)
Parameters
parsstringThe query string to parse (e.g., "contentType=Video&codecId=H264&width=1920...").
useStringEnumsboolIf true, enum values are parsed as strings; if false, as integers.
Returns
- MediaType
A new MediaType instance populated from the query string parameters.
GetHashCode()
Returns a hash code for this instance.
public override int GetHashCode()
Returns
- int
A hash code for this instance.
GetWellKnownFramerate(double)
Converts an approximate framerate to a well-known exact rational value.
public static Rational GetWellKnownFramerate(double framerateDouble)
Parameters
framerateDoubledoubleThe framerate as a floating-point value.
Returns
- Rational
A matching well-known framerate from WellKnownFramerates array if within 0.01 tolerance, a rounded integer framerate (n/1) if the value is between 1-120 and within 0.1 of a whole number, or an empty Rational if no match is found.
GetWellKnownFramerate(Rational)
Converts an approximate framerate to a well-known exact rational value.
public static Rational GetWellKnownFramerate(Rational framerate)
Parameters
framerateRationalThe framerate to match.
Returns
- Rational
A matching well-known framerate (e.g., 30000/1001 for ~29.97 fps), a rounded integer framerate if close to a whole number, or an empty Rational if no match is found.
ShallowEquals(MediaType)
Performs a shallow equality comparison with another media type. Compares only ContentType, CodecId, Timescale, ProfileLevel, and basic content-specific properties (SampleRate/Channels for audio, Width/Height for video).
public bool ShallowEquals(MediaType right)
Parameters
rightMediaTypeThe media type to compare with this instance.
Returns
- bool
True if the media types are shallowly equal; otherwise, false.
ToBucketMediaType()
Generates a MIME codec string suitable for HLS/DASH manifests and MSE SourceBuffer.
public string ToBucketMediaType()
Returns
- string
A codec string such as "avc1.64001F" for H.264, "hvc1..." for H.265, or "mp4a.40.2" for AAC. Returns an empty string if the codec is not supported or CodecPrivateData is unavailable.
ToParameterString(bool)
Serializes this media type to a URL query string format.
public string ToParameterString(bool useStringEnums = true)
Parameters
useStringEnumsboolIf true, enum values are serialized as strings; if false, as integers.
Returns
- string
A query string containing all media type parameters (e.g., "contentType=Video&codecId=H264&bitrate=5000000..."). Includes content-specific parameters and metadata entries prefixed with "metadata-".
ToString()
Returns a human-readable string representation of this media type.
public override string ToString()
Returns
- string
A string describing the media type with key parameters.
ToString(bool)
Returns a human-readable string representation of this media type.
public string ToString(bool extended)
Parameters
extendedboolIf true, includes additional details like profile/level and codec private data.
Returns
- string
For video: "Video [Codec] [Bitrate] kbps [Width]x[Height] [Framerate] fps". For audio: "Audio [Codec] [Bitrate] kbps [SampleRate] Hz [Channels] ch". For text/binary: content type and codec information with optional bitrate. Appends "(incomplete)" if IsComplete returns false.
Operators
operator ==(MediaType, MediaType)
Determines whether two MediaType instances are equal.
public static bool operator ==(MediaType left, MediaType right)
Parameters
Returns
- bool
True if both are null or if they are equal according to Equals; otherwise, false.
operator !=(MediaType, MediaType)
Determines whether two MediaType instances are not equal.
public static bool operator !=(MediaType left, MediaType right)
Parameters
Returns
- bool
True if one is null and the other is not, or if they are not equal; otherwise, false.