MediaFile Class
Provides static methods for reading and writing media file tags.
public static class MediaFileRemarks
Writing replaces the tags of a file: the written MediaTagInfo becomes the whole tag, and any field left null is removed from the file rather than left at its current value. To change one field, read the tags first, modify the returned object and write it back.
These methods report problems with the file through the returned MediaTagResult instead of throwing. They still throw for invalid arguments.
Methods
| Name | Description |
|---|---|
| DetectFormat(Stream) | Detects the media format from stream content (magic bytes). The stream position is restored after detection. |
| DetectFormat(string) | Detects the media format from a file path using both magic bytes and file extension. |
| ReadTags(Stream, MediaFormat?) | Reads tags from the specified stream, starting at its current position. |
| ReadTags(string) | Reads tags from the specified file. |
| RemoveTags(Stream, Stream, MediaFormat) | Removes every tag this library can write, writing the result to the output stream. |
| RemoveTags(string) | Removes every tag this library can write from the specified file. |
| WriteTags(Stream, Stream, MediaTagInfo, MediaFormat) | Writes the input stream to the output stream, replacing its tags. |
| WriteTags(Stream, Stream, MediaTagInfo, MediaFormat, MediaTagWriteOptions) | Writes the input stream to the output stream, replacing its tags, using the specified options. |
| WriteTags(string, MediaTagInfo) | Replaces the tags of the specified file. |
| WriteTags(string, MediaTagInfo, MediaTagWriteOptions) | Replaces the tags of the specified file, using the specified options. |
DetectFormat overloads
DetectFormat(Stream)
MediaFile.csDetects the media format from stream content (magic bytes). The stream position is restored after detection.
public static MediaFormat? DetectFormat(Stream stream)Parameters
streamStream- The stream to detect the format from.
Returns
MediaFormat? The detected format, or null if not recognized.
Exceptions
ArgumentNullExceptionstreamisnull.
DetectFormat(string)
MediaFile.csDetects the media format from a file path using both magic bytes and file extension.
public static MediaFormat? DetectFormat(string filePath)Parameters
filePathstring- The path to the media file.
Returns
MediaFormat? The detected format, or null if not recognized.
Exceptions
ArgumentExceptionfilePathisnullor empty.
ReadTags overloads
ReadTags(Stream, MediaFormat?)
MediaFile.csReads tags from the specified stream, starting at its current position.
public static MediaTagResult<MediaTagInfo> ReadTags(Stream stream, MediaFormat? format = null)Parameters
streamStream- The seekable stream containing the media file.
formatMediaFormat?- The format of the media file. If
null, the format is auto-detected.
Returns
MediaTagResult<MediaTagInfo> A result containing the parsed tags, or an error.
Exceptions
ArgumentNullExceptionstreamisnull.
ReadTags(string)
MediaFile.csReads tags from the specified file.
public static MediaTagResult<MediaTagInfo> ReadTags(string filePath)Parameters
filePathstring- The path to the media file.
Returns
MediaTagResult<MediaTagInfo> A result containing the parsed tags, or an error.
Exceptions
ArgumentExceptionfilePathisnullor empty.
RemoveTags overloads
RemoveTags(Stream, Stream, MediaFormat)
MediaFile.csRemoves every tag this library can write, writing the result to the output stream.
public static MediaTagResult RemoveTags(Stream inputStream, Stream outputStream, MediaFormat format)Parameters
inputStreamStream- The seekable stream containing the original media file, read from its current position.
outputStreamStream- The stream to write the modified media file to.
formatMediaFormat- The format of the media file.
Returns
MediaTagResult A result indicating success or failure.
Exceptions
ArgumentNullExceptioninputStreamoroutputStreamisnull.
RemoveTags(string)
MediaFile.csRemoves every tag this library can write from the specified file.
public static MediaTagResult RemoveTags(string filePath)Parameters
filePathstring- The path to the media file.
Returns
MediaTagResult A result indicating success or failure. The file is left untouched when the operation fails.
Exceptions
ArgumentExceptionfilePathisnullor empty.
WriteTags overloads
WriteTags(Stream, Stream, MediaTagInfo, MediaFormat)
MediaFile.csWrites the input stream to the output stream, replacing its tags.
public static MediaTagResult WriteTags(Stream inputStream, Stream outputStream, MediaTagInfo tags, MediaFormat format)Parameters
inputStreamStream- The seekable stream containing the original media file, read from its current position.
outputStreamStream- The stream to write the modified media file to.
tagsMediaTagInfo- The tags to write. They replace the existing tags: fields left
nullare removed. formatMediaFormat- The format of the media file.
Returns
MediaTagResult A result indicating success or failure. On failure the content already written to outputStream must be discarded: it is not a valid media file.
Exceptions
ArgumentNullExceptioninputStream,outputStreamortagsisnull.
WriteTags(Stream, Stream, MediaTagInfo, MediaFormat, MediaTagWriteOptions)
MediaFile.csWrites the input stream to the output stream, replacing its tags, using the specified options.
public static MediaTagResult WriteTags(Stream inputStream, Stream outputStream, MediaTagInfo tags, MediaFormat format, MediaTagWriteOptions options)Parameters
inputStreamStream- The seekable stream containing the original media file, read from its current position.
outputStreamStream- The stream to write the modified media file to.
tagsMediaTagInfo- The tags to write. They replace the existing tags: fields left
nullare removed. formatMediaFormat- The format of the media file.
optionsMediaTagWriteOptions- Controls how the tags are written.
Returns
MediaTagResult A result indicating success or failure. On failure the content already written to outputStream must be discarded: it is not a valid media file.
Exceptions
ArgumentNullException- One of the arguments is
null.
WriteTags(string, MediaTagInfo)
MediaFile.csReplaces the tags of the specified file.
public static MediaTagResult WriteTags(string filePath, MediaTagInfo tags)Parameters
filePathstring- The path to the media file.
tagsMediaTagInfo- The tags to write. They replace the existing tags: fields left
nullare removed from the file.
Returns
MediaTagResult A result indicating success or failure. The file is left untouched when the operation fails.
Exceptions
ArgumentExceptionfilePathisnullor empty.ArgumentNullExceptiontagsisnull.
WriteTags(string, MediaTagInfo, MediaTagWriteOptions)
MediaFile.csReplaces the tags of the specified file, using the specified options.
public static MediaTagResult WriteTags(string filePath, MediaTagInfo tags, MediaTagWriteOptions options)Parameters
filePathstring- The path to the media file.
tagsMediaTagInfo- The tags to write. They replace the existing tags: fields left
nullare removed from the file. optionsMediaTagWriteOptions- Controls how the tags are written.
Returns
MediaTagResult A result indicating success or failure. The file is left untouched when the operation fails.
Exceptions
ArgumentExceptionfilePathisnullor empty.ArgumentNullExceptiontagsoroptionsisnull.