MetadataExtractor .NET

repository·main·Indexed 22 days ago

https://github.com/drewnoakes/metadata-extractor-dotnet

A .NET library for reading metadata from image, movie, and audio files. It supports standards such as Exif, IPTC, and XMP, and a wide range of file types including JPEG, PNG, TIFF, WebP, MP4, and various Camera Raw formats. The library targets .NET 8.0, .NET Standard 2.1, and .NET Standard 2.0.

Tokens
1.3K
Snippets
7
Records
9
Agent score
27%

What's inside MetadataExtractor

  1. Install MetadataExtractor via NuGet

    main

    You can install the MetadataExtractor library using the NuGet Package Manager in Visual Studio, the Package Manager Console, or by manually adding a PackageReference to your project file.

    Using Package Manager Console

    PM> Install-Package MetadataExtractor

    Using Project File (.csproj)

    Add the following to your <ItemGroup>:

    <PackageReference Include="MetadataExtractor" Version="2.9.1" />
    <PackageReference Include="MetadataExtractor" Version="2.9.1" />
  2. Extract Exif data from JPEG files using JpegMetadataReader.ReadMetadata

    main

    If you are certain the input is JPEG data and you only need Exif metadata, you can optimize performance by passing a specific reader to JpegMetadataReader.ReadMetadata. This approach ignores other metadata types in the file. Note that this will throw an exception if the file is not actually a JPEG.

    var directories = JpegMetadataReader.ReadMetadata(_stream, new[] { new ExifReader() });
    var subIfdDirectory = directories.OfType<ExifSubIfdDirectory>().First();
    var dateTime = subIfdDirectory.GetDateTime(ExifDirectoryBase.TagDateTimeOriginal);
  3. Extract metadata from any supported file type using ImageMetadataReader.ReadMetadata

    main

    The most general and recommended solution for most use cases is ImageMetadataReader.ReadMetadata(_stream). This method automatically determines the file type and returns all found metadata. It will throw an exception if the file does not contain a supported file type.

    var directories = ImageMetadataReader.ReadMetadata(_stream);
    var subIfdDirectory = directories.OfType<ExifSubIfdDirectory>().First();
    var dateTime = subIfdDirectory.GetDateTime(ExifDirectoryBase.TagDateTimeOriginal);
  4. Extract all metadata from a JPEG file using JpegMetadataReader.ReadMetadata

    main

    To retrieve all available metadata from a JPEG file, use JpegMetadataReader.ReadMetadata(_stream). This method will throw an exception if the file does not contain JPEG data.

    var directories = JpegMetadataReader.ReadMetadata(_stream);
    var subIfdDirectory = directories.OfType<ExifSubIfdDirectory>().First();
    var dateTime = subIfdDirectory.GetDateTime(ExifDirectoryBase.TagDateTimeOriginal);
  5. Access a specific metadata tag

    main

    To retrieve a specific value, identify the correct directory type (e.g., ExifSubIfdDirectory) and use the GetDescription method with the appropriate tag constant.

    Example for accessing the Exif DateTime tag:

    var subIfdDirectory = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault();
    var dateTime = subIfdDirectory?.GetDescription(ExifDirectoryBase.TagDateTime);
    var subIfdDirectory = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault();
    var dateTime = subIfdDirectory?.GetDescription(ExifDirectoryBase.TagDateTime);
  6. Read and iterate through all metadata directories

    main

    To extract metadata from a file, use ImageMetadataReader.ReadMetadata(imagePath). This returns an IEnumerable<Directory> containing various metadata directories (e.g., Exif, IPTC, XMP) found in the file.

    You can iterate through all directories and all tags within them to print every available piece of metadata:

    IEnumerable<Directory> directories = ImageMetadataReader.ReadMetadata(imagePath);
    
    foreach (var directory in directories)
    foreach (var tag in directory.Tags)
        Console.WriteLine($"{directory.Name} - {tag.Name} = {tag.Description}");
    IEnumerable<Directory> directories = ImageMetadataReader.ReadMetadata(imagePath);
    
    foreach (var directory in directories)
    foreach (var tag in directory.Tags)
        Console.WriteLine($"{directory.Name} - {tag.Name} = {tag.Description}");
  7. Supported .NET Frameworks

    main

    The library is distributed via a single NuGet package that targets the following frameworks:

    • .NET 8.0 (net8.0): Includes support for NativeAOT.
    • .NET Standard 2.1 (netstandard2.1): Uses newer APIs where possible.
    • .NET Standard 2.0 (netstandard2.0): Provides compatibility for .NET Framework and .NET Core.
  8. Supported metadata formats and file types

    main

    MetadataExtractor supports a wide range of metadata formats and file types:

    Metadata Formats

    • Exif
    • IPTC
    • XMP
    • JFIF / JFXX
    • ICC Profiles
    • Photoshop

    Supported File Types

    • Images: AVIF, BMP, DNG, EPS, GIF, HEIF/HEIC, ICO, JPEG/JFIF, Netpbm, PCX, PNG, PSD, TGA, TIFF/BigTIFF, WebP, and various Camera Raw formats (3FR, ARW, CRW/CR2/CR3/CRX, GPR, KDC, NEF, ORF, PEF, RAF, RW2, RWL, SRW).
    • Movies: AVCI, AVI, MOV, MP4.
    • Audio: WAV, MP3.

    Additionally, the library decodes camera-specific "makernote" data for many manufacturers including Apple, Canon, DJI, Nikon, Sony, and others.