Skip to content

Commit

Permalink
Support reading metadata without loading entire moov
Browse files Browse the repository at this point in the history
  • Loading branch information
Mbucari committed Aug 27, 2023
1 parent 3e4c479 commit ac826f9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
10 changes: 8 additions & 2 deletions src/AAXClean/AAXClean.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Version>1.1.0</Version>
<Version>1.1.1</Version>
<Authors>MrGneissGuy</Authors>
<Company>Just Me, Inc.</Company>
<Description>Decrypts Audible aax and aaxc files to mp4.</Description>
Expand All @@ -26,10 +26,16 @@
<PublishReadyToRun>true</PublishReadyToRun>
<DebugType>embedded</DebugType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Mpeg4Lib\Mpeg4Lib.csproj" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>

</Project>
43 changes: 37 additions & 6 deletions src/AAXClean/AppleTags.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Mpeg4Lib.Boxes;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace AAXClean
Expand Down Expand Up @@ -40,12 +42,13 @@ public AppleTags(AppleListBox appleListBox)
public string Version { get => IList.GetTagString("VERS"); set => IList.EditOrAddTag("VERS", value); }
public byte[] Cover { get => IList.GetTagBytes("covr"); set => IList.EditOrAddTag("covr", value, AppleDataType.JPEG); }

public (int trackNum, int trackCount) Tracks
[DisallowNull]
public (int trackNum, int trackCount)? Tracks
{
get
{
var data = IList.GetTagBytes("trkn");
if (data is null) return (1, 1);
if (data is null) return null;

int trackNum = (data[2] << 8) | data[3];
int trackCount = (data[4] << 8) | data[5];
Expand All @@ -54,16 +57,44 @@ public AppleTags(AppleListBox appleListBox)
}
set
{
if (value is not (int trackNum, int trackCount))
throw new ArgumentNullException(nameof(Tracks), "Cannot set to null");

var data = new byte[8];

data[3] = (byte)value.trackNum;
data[2] = (byte)(value.trackNum >> 8);
data[3] = (byte)trackNum;
data[2] = (byte)(trackNum >> 8);

data[5] = (byte)value.trackCount;
data[4] = (byte)(value.trackCount >> 8);
data[5] = (byte)trackCount;
data[4] = (byte)(trackCount >> 8);

IList.EditOrAddTag("trkn", data);
}
}

public static AppleTags FromFile(string mp4File)
{
using var file = System.IO.File.Open(mp4File, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);

BoxHeader header;
do
{
header = new BoxHeader(file);

if (header.Type is "moov")
continue;
else if (header.Type is "udta")
break;
else
file.Position += header.TotalBoxSize - header.HeaderSize;

}while(file.Position < file.Length);

if (header?.Type is not "udta") return null;

var ilst = new UdtaBox(file, header, null)?.GetChild<MetaBox>()?.GetChild<AppleListBox>();

return ilst is null ? null : new AppleTags(ilst);
}
}
}
9 changes: 8 additions & 1 deletion src/Mpeg4Lib/Mpeg4Lib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Version>0.1.2</Version>
<Version>0.1.20</Version>
<Authors>MrGneissGuy</Authors>
<Company>Just Me, Inc.</Company>
<Description>Parsees mpeg-4 files</Description>
Expand All @@ -22,4 +22,11 @@
<DebugType>embedded</DebugType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>

0 comments on commit ac826f9

Please sign in to comment.