Skip to content

Commit 7b42132

Browse files
author
Meyn
committed
Implement EAC3 codec
1 parent d0d71cf commit 7b42132

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

Tubifarry/Core/Model/AudioMetadataHandler.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,16 @@ public bool TryEmbedMetadata(Album albumInfo, Track trackInfo)
392392
file.Save();
393393
return true;
394394
}
395+
catch (TagLib.CorruptFileException ex)
396+
{
397+
_logger?.Error(ex, $"File is corrupted or has incorrect extension: {TrackPath}");
398+
return false;
399+
}
400+
catch (TagLib.UnsupportedFormatException ex)
401+
{
402+
_logger?.Error(ex, $"File format does not support metadata embedding: {TrackPath}");
403+
return false;
404+
}
395405
catch (Exception ex)
396406
{
397407
_logger?.Error(ex, $"Failed to embed metadata in track: {TrackPath}");
@@ -407,6 +417,25 @@ public bool TryEmbedMetadata(Album albumInfo, Track trackInfo)
407417
public static bool IsTargetFormatSupportedForEncoding(AudioFormat format) => BaseConversionParameters.ContainsKey(format);
408418

409419

420+
///// <summary>
421+
///// Checks if a given audio format supports embedded metadata tags.
422+
///// </summary>
423+
///// <param name="format">The audio format to check</param>
424+
///// <returns>True if the format supports metadata tagging, false otherwise</returns>
425+
public static bool SupportsMetadataEmbedding(AudioFormat format) => format switch
426+
{
427+
// Formats that DO NOT support metadata embedding
428+
AudioFormat.AC3 or AudioFormat.EAC3 or AudioFormat.MIDI => false,
429+
430+
// Formats that DO support metadata embedding
431+
AudioFormat.AAC or AudioFormat.MP3 or AudioFormat.Opus or AudioFormat.Vorbis or
432+
AudioFormat.FLAC or AudioFormat.WAV or AudioFormat.MP4 or AudioFormat.AIFF or
433+
AudioFormat.OGG or AudioFormat.WMA or AudioFormat.ALAC or AudioFormat.APE => true,
434+
435+
// Unknown formats - assume they might support it
436+
_ => true
437+
};
438+
410439
/// <summary>
411440
/// Gets the actual audio codec from a file using FFmpeg and returns the corresponding AudioFormat.
412441
/// </summary>

Tubifarry/Core/Utilities/AudioFormat.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public enum AudioFormat
1818
AMR,
1919
WMA,
2020
ALAC,
21-
APE
21+
APE,
22+
AC3,
23+
EAC3
2224
}
2325

2426
internal static class AudioFormatHelper
@@ -30,7 +32,9 @@ internal static class AudioFormatHelper
3032
AudioFormat.Vorbis,
3133
AudioFormat.MP4,
3234
AudioFormat.AMR,
33-
AudioFormat.WMA
35+
AudioFormat.WMA,
36+
AudioFormat.AC3,
37+
AudioFormat.EAC3
3438
];
3539

3640
private static readonly int[] _standardBitrates = [
@@ -55,12 +59,14 @@ internal static class AudioFormatHelper
5559
{
5660
{ AudioFormat.AAC, (256, 64, 320) },
5761
{ AudioFormat.MP3, (320, 64, 320) },
58-
{ AudioFormat.Opus, (96, 32, 510) },
62+
{ AudioFormat.Opus, (256, 32, 510) },
5963
{ AudioFormat.Vorbis, (224, 64, 500) },
6064
{ AudioFormat.MP4, (256, 64, 320) },
6165
{ AudioFormat.AMR, (12, 5, 12) },
6266
{ AudioFormat.WMA, (192, 48, 320) },
63-
{ AudioFormat.OGG, (224, 64, 500) }
67+
{ AudioFormat.OGG, (224, 64, 500) },
68+
{ AudioFormat.AC3, (448, 192, 640) },
69+
{ AudioFormat.EAC3, (768, 192, 6144) }
6470
};
6571

6672
/// <summary>
@@ -91,6 +97,7 @@ internal static class AudioFormatHelper
9197
"opus" => ".opus",
9298
"flac" => ".flac",
9399
"ac3" => ".ac3",
100+
"eac3" or "ec3" => ".ec3",
94101
"alac" => ".m4a",
95102
"vorbis" => ".ogg",
96103
"ape" => ".ape",
@@ -116,6 +123,8 @@ internal static class AudioFormatHelper
116123
"wma" => AudioFormat.WMA,
117124
"alac" => AudioFormat.ALAC,
118125
"ape" => AudioFormat.APE,
126+
"ac3" or "ac-3" => AudioFormat.AC3,
127+
"eac3" or "eac-3" or "e-ac-3" or "ec-3" or "ec3" => AudioFormat.EAC3,
119128
_ => AudioFormat.Unknown // Default for unknown formats
120129
};
121130

@@ -138,6 +147,8 @@ internal static class AudioFormatHelper
138147
AudioFormat.OGG => ".ogg",
139148
AudioFormat.ALAC => ".m4a",
140149
AudioFormat.APE => ".ape",
150+
AudioFormat.AC3 => ".ac3",
151+
AudioFormat.EAC3 => ".ec3",
141152
_ => ".aac" // Default to AAC if the format is unknown
142153
};
143154

@@ -176,6 +187,8 @@ internal static class AudioFormatHelper
176187
"wma" => AudioFormat.WMA,
177188
"alac" => AudioFormat.ALAC,
178189
"ape" => AudioFormat.APE,
190+
"ac3" => AudioFormat.AC3,
191+
"ec3" or "eac3" => AudioFormat.EAC3,
179192
_ => AudioFormat.Unknown
180193
};
181194

Tubifarry/Download/Clients/SubSonic/SubSonicDownloadRequest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,14 @@ private async Task<bool> PostProcessTrackAsync(SubSonicSearchSong trackInfo, Loa
306306
try
307307
{
308308
AudioMetadataHandler audioData = new(trackPath) { AlbumCover = _albumCover };
309+
310+
AudioFormat detectedFormat = AudioFormatHelper.GetAudioCodecFromExtension(trackPath);
311+
if (!AudioMetadataHandler.SupportsMetadataEmbedding(detectedFormat))
312+
{
313+
_logger.Warn($"Skipping metadata embedding for {detectedFormat} format. Not supported: {Path.GetFileName(trackPath)}");
314+
return true;
315+
}
316+
309317
Album album = CreateAlbumFromSubSonicData(trackInfo, _currentAlbum);
310318
Track track = CreateTrackFromSubSonicData(trackInfo);
311319

0 commit comments

Comments
 (0)