Skip to content

Commit

Permalink
Merge branch 'master' into feature/dng
Browse files Browse the repository at this point in the history
  • Loading branch information
drewnoakes authored Jan 30, 2018
2 parents 4b12960 + 0cef5a2 commit 99e61f5
Show file tree
Hide file tree
Showing 16 changed files with 698 additions and 57 deletions.
4 changes: 2 additions & 2 deletions MetadataExtractor.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.0
VisualStudioVersion = 15.0.27130.2010
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8CF154EA-6A2C-4BF4-B263-78758F834192}"
ProjectSection(SolutionItems) = preProject
Expand Down Expand Up @@ -157,6 +157,6 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B3C98CDA-0400-456E-887B-70F751903DC3}
SolutionGuid = {15F8CDD5-5EFF-4277-8559-5AA83D1353C8}
EndGlobalSection
EndGlobal
50 changes: 50 additions & 0 deletions MetadataExtractor/Formats/Avi/AviDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#region License
//
// Copyright 2002-2017 Drew Noakes
// Ported from Java to C# by Yakov Danilov for Imazen LLC in 2014
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// More information about this project is available at:
//
// https://github.com/drewnoakes/metadata-extractor-dotnet
// https://drewnoakes.com/code/exif/
//
#endregion

using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;

namespace MetadataExtractor.Formats.Avi
{
/// <author>Payton Garland</author>
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public class AviDescriptor : TagDescriptor<AviDirectory>
{
public AviDescriptor([NotNull] AviDirectory directory)
: base(directory)
{
}

public override string GetDescription(int tagType)
{
switch (tagType)
{
case AviDirectory.TAG_WIDTH:
case AviDirectory.TAG_HEIGHT:
return Directory.GetString(tagType) + " pixels";
}
return base.GetDescription(tagType);
}
}
}
67 changes: 67 additions & 0 deletions MetadataExtractor/Formats/Avi/AviDirectory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#region License
//
// Copyright 2002-2017 Drew Noakes
// Ported from Java to C# by Yakov Danilov for Imazen LLC in 2014
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// More information about this project is available at:
//
// https://github.com/drewnoakes/metadata-extractor-dotnet
// https://drewnoakes.com/code/exif/
//
#endregion

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace MetadataExtractor.Formats.Avi
{
/// <author>Payton Garland</author>
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public class AviDirectory : Directory
{
public const int TAG_FRAMES_PER_SECOND = 1;
public const int TAG_SAMPLES_PER_SECOND = 2;
public const int TAG_DURATION = 3;
public const int TAG_VIDEO_CODEC = 4;
public const int TAG_AUDIO_CODEC = 5;
public const int TAG_WIDTH = 6;
public const int TAG_HEIGHT = 7;
public const int TAG_STREAMS = 8;

private static readonly Dictionary<int, string> _tagNameMap = new Dictionary<int, string>
{
{TAG_FRAMES_PER_SECOND, "Frames Per Second"},
{TAG_SAMPLES_PER_SECOND, "Samples Per Second"},
{TAG_DURATION, "Duration"},
{TAG_VIDEO_CODEC, "Video Codec"},
{TAG_AUDIO_CODEC, "Audio Codec"},
{TAG_WIDTH, "Width"},
{TAG_HEIGHT, "Height"},
{TAG_STREAMS, "Stream Count"}
};

public AviDirectory()
{
SetDescriptor(new AviDescriptor(this));
}

public override string Name => "Avi";

protected override bool TryGetTagName(int tagType, out string tagName)
{
return _tagNameMap.TryGetValue(tagType, out tagName);
}
}
}
69 changes: 69 additions & 0 deletions MetadataExtractor/Formats/Avi/AviMetadataReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#region License
//
// Copyright 2002-2017 Drew Noakes
// Ported from Java to C# by Yakov Danilov for Imazen LLC in 2014
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// More information about this project is available at:
//
// https://github.com/drewnoakes/metadata-extractor-dotnet
// https://drewnoakes.com/code/exif/
//
#endregion

using System.Collections.Generic;
using System.IO;
using JetBrains.Annotations;
using MetadataExtractor.Formats.FileSystem;
using MetadataExtractor.Formats.Riff;
using MetadataExtractor.IO;

#if NET35
using DirectoryList = System.Collections.Generic.IList<MetadataExtractor.Directory>;
#else
using DirectoryList = System.Collections.Generic.IReadOnlyList<MetadataExtractor.Directory>;
#endif

namespace MetadataExtractor.Formats.Avi
{
/// <summary>Obtains metadata from Avi files.</summary>
/// <author>Drew Noakes https://drewnoakes.com</author>
public static class AviMetadataReader
{
/// <exception cref="System.IO.IOException"/>
/// <exception cref="RiffProcessingException"/>
[NotNull]
public static DirectoryList ReadMetadata([NotNull] string filePath)
{
var directories = new List<Directory>();

using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
directories.AddRange(ReadMetadata(stream));

directories.Add(new FileMetadataReader().Read(filePath));

return directories;
}

/// <exception cref="System.IO.IOException"/>
/// <exception cref="RiffProcessingException"/>
[NotNull]
public static DirectoryList ReadMetadata([NotNull] Stream stream)
{
var directories = new List<Directory>();
new RiffReader().ProcessRiff(new SequentialStreamReader(stream), new AviRiffHandler(directories));
return directories;
}
}
}
172 changes: 172 additions & 0 deletions MetadataExtractor/Formats/Avi/AviRiffHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#region License
//
// Copyright 2002-2017 Drew Noakes
// Ported from Java to C# by Yakov Danilov for Imazen LLC in 2014
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// More information about this project is available at:
//
// https://github.com/drewnoakes/metadata-extractor-dotnet
// https://drewnoakes.com/code/exif/
//
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using JetBrains.Annotations;
using MetadataExtractor.Formats.Riff;
using MetadataExtractor.IO;

namespace MetadataExtractor.Formats.Avi
{
/// <summary>
/// Implementation of <see cref="IRiffHandler"/> specialising in AVI support.
/// </summary>
/// <remarks>
/// Extracts data from chunk/list types:
/// <list type="bullet">
/// <item><c>"avih"</c>: width, height, streams</item>
/// <item><c>"strh"</c>: frames/second, samples/second, duration, video codec</item>
/// </list>
/// Sources:
/// http://www.alexander-noe.com/video/documentation/avi.pdf
/// https://msdn.microsoft.com/en-us/library/ms899422.aspx
/// https://www.loc.gov/preservation/digital/formats/fdd/fdd000025.shtml
/// </remarks>
/// <author>Payton Garland</author>
public sealed class AviRiffHandler : IRiffHandler
{
[NotNull]
private readonly List<Directory> _directories;

public AviRiffHandler([NotNull] List<Directory> directories)
{
_directories = directories;
}

public bool ShouldAcceptRiffIdentifier(string identifier) => identifier == "AVI ";

public bool ShouldAcceptChunk(string fourCc) => fourCc == "strh" ||
fourCc == "avih";

public bool ShouldAcceptList(string fourCc) => fourCc == "hdrl" ||
fourCc == "strl" ||
fourCc == "AVI ";

public void ProcessChunk(string fourCc, byte[] payload)
{
switch (fourCc)
{
case "strh":
{
string error = null;
var reader = new ByteArrayReader(payload, isMotorolaByteOrder: false);
string fccType = null;
string fccHandler = null;
float dwScale = 0;
float dwRate = 0;
int dwLength = 0;
try
{
fccType = reader.GetString(0, 4, Encoding.ASCII);
fccHandler = reader.GetString(4, 4, Encoding.ASCII);
//int dwFlags = reader.GetInt32(8);
//int wPriority = reader.GetInt16(12);
//int wLanguage = reader.GetInt16(14);
//int dwInitialFrames = reader.GetInt32(16);
dwScale = reader.GetFloat32(20);
dwRate = reader.GetFloat32(24);
//int dwStart = reader.GetInt32(28);
dwLength = reader.GetInt32(32);
//int dwSuggestedBufferSize = reader.GetInt32(36);
//int dwQuality = reader.GetInt32(40);
//int dwSampleSize = reader.GetInt32(44);
//byte[] rcFrame = reader.GetBytes(48, 2);
}
catch (IOException e)
{
error = "Exception reading AviRiff chunk 'strh' : " + e.Message;
}

var directory = new AviDirectory();
if (error == null)
{
if (fccType == "vids")
{
directory.Set(AviDirectory.TAG_FRAMES_PER_SECOND, (dwRate / dwScale));

double duration = dwLength / (dwRate / dwScale);
int hours = (int)duration / (int)(Math.Pow(60, 2));
int minutes = ((int)duration / (int)(Math.Pow(60, 1))) - (hours * 60);
int seconds = (int)Math.Round((duration / (Math.Pow(60, 0))) - (minutes * 60));
string time = new DateTime(2000, 1, 1, hours, minutes, seconds).ToString("hh:mm:ss");

directory.Set(AviDirectory.TAG_DURATION, time);
directory.Set(AviDirectory.TAG_VIDEO_CODEC, fccHandler);
}
else
if (fccType == "auds")
{
directory.Set(AviDirectory.TAG_SAMPLES_PER_SECOND, (dwRate / dwScale));
}
}
else
directory.AddError(error);
_directories.Add(directory);
break;
}
case "avih":
{
string error = null;
var reader = new ByteArrayReader(payload, isMotorolaByteOrder: false);
int dwStreams = 0;
int dwWidth = 0;
int dwHeight = 0;
try
{
//int dwMicroSecPerFrame = reader.GetInt32(0);
//int dwMaxBytesPerSec = reader.GetInt32(4);
//int dwPaddingGranularity = reader.GetInt32(8);
//int dwFlags = reader.GetInt32(12);
//int dwTotalFrames = reader.GetInt32(16);
//int dwInitialFrames = reader.GetInt32(20);
dwStreams = reader.GetInt32(24);
//int dwSuggestedBufferSize = reader.GetInt32(28);
dwWidth = reader.GetInt32(32);
dwHeight = reader.GetInt32(36);
//byte[] dwReserved = reader.GetBytes(40, 4);
}
catch (IOException e)
{
error = "Exception reading AviRiff chunk 'avih' : " + e.Message;
}

var directory = new AviDirectory();
if (error == null)
{
directory.Set(AviDirectory.TAG_WIDTH, dwWidth);
directory.Set(AviDirectory.TAG_HEIGHT, dwHeight);
directory.Set(AviDirectory.TAG_STREAMS, dwStreams);
}
else
directory.AddError(error);
_directories.Add(directory);
break;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;

namespace MetadataExtractor.Formats.Exif.Makernotes
{
/// <summary>
/// Provides human-readable string representations of tag values stored in a <see cref="DJIMakernoteDirectory"/>.
/// </summary>
/// <remarks>Using information from https://metacpan.org/pod/distribution/Image-ExifTool/lib/Image/ExifTool/TagNames.pod#DJI-Tags</remarks>
/// <author>Charlie Matherne, adapted from Drew Noakes https://drewnoakes.com</author>
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public class DJIMakernoteDescriptor : TagDescriptor<DJIMakernoteDirectory>
{
public DJIMakernoteDescriptor([NotNull] DJIMakernoteDirectory directory)
: base(directory)
{
}
}
}
Loading

0 comments on commit 99e61f5

Please sign in to comment.