Skip to content

Commit 85c66cf

Browse files
committed
Add the ability to customize audio parser specifications and ignored file extensions
1 parent 014b6eb commit 85c66cf

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

Sources/Shared/Toolkit/Format/Sniffers/AudiobookFormatSniffer.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ public struct ZABFormatSniffer: FormatSniffer {
7474
let entries = container.entries
7575
.filter {
7676
$0.lastPathSegment?.hasPrefix(".") == false &&
77-
$0.lastPathSegment != "Thumbs.db" &&
78-
$0.pathExtension != "jpg" &&
79-
$0.pathExtension != .pdf
77+
$0.lastPathSegment != "Thumbs.db"
8078
}
8179
let containerExtensions = Set(entries.compactMap(\.pathExtension))
8280
guard

Sources/Streamer/Parser/Audio/AudioParser.swift

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@ import ReadiumShared
1212
///
1313
/// It can also work for a standalone audio file.
1414
public final class AudioParser: PublicationParser {
15-
private let assetRetriever: AssetRetriever
16-
private let manifestAugmentor: AudioPublicationManifestAugmentor
15+
/// The default set of audio format specifications that determine whether an archive qualifies as an audiobook.
16+
/// These formats include common audio file types such as MP3, AAC, FLAC, etc.
17+
public static let defaultAudioSpecifications: Set<FormatSpecification> = audioSpecifications
1718

18-
public init(
19-
assetRetriever: AssetRetriever,
20-
manifestAugmentor: AudioPublicationManifestAugmentor = AVAudioPublicationManifestAugmentor()
21-
) {
22-
self.assetRetriever = assetRetriever
23-
self.manifestAugmentor = manifestAugmentor
24-
}
19+
/// A set of file extensions that are ignored during audiobook processing.
20+
/// These typically include playlist files, metadata, or auxiliary files that should not be treated as audio content.
21+
public static let defaultIgnoredExtensions: Set<FileExtension> = ignoredExtensions
2522

26-
private let audioSpecifications: Set<FormatSpecification> = [
23+
private static let audioSpecifications: Set<FormatSpecification> = [
2724
.aac,
2825
.aiff,
2926
.flac,
@@ -35,6 +32,38 @@ public final class AudioParser: PublicationParser {
3532
.webm,
3633
]
3734

35+
private static let ignoredExtensions: Set<FileExtension> = [
36+
"asx",
37+
"bio",
38+
"m3u",
39+
"m3u8",
40+
"pla",
41+
"pls",
42+
"smil",
43+
"txt",
44+
"vlc",
45+
"wpl",
46+
"xspf",
47+
"zpl",
48+
]
49+
50+
private let assetRetriever: AssetRetriever
51+
private let manifestAugmentor: AudioPublicationManifestAugmentor
52+
private let ignoredExtensions: Set<FileExtension>
53+
private let audioSpecifications: Set<FormatSpecification>
54+
55+
public init(
56+
assetRetriever: AssetRetriever,
57+
manifestAugmentor: AudioPublicationManifestAugmentor = AVAudioPublicationManifestAugmentor(),
58+
audioSpecifications: Set<FormatSpecification> = AudioParser.defaultAudioSpecifications,
59+
ignoredExtensions: Set<FileExtension> = AudioParser.defaultIgnoredExtensions
60+
) {
61+
self.assetRetriever = assetRetriever
62+
self.manifestAugmentor = manifestAugmentor
63+
self.ignoredExtensions = ignoredExtensions
64+
self.audioSpecifications = audioSpecifications
65+
}
66+
3867
public func parse(
3968
asset: Asset,
4069
warnings: WarningLogger?
@@ -107,23 +136,6 @@ public final class AudioParser: PublicationParser {
107136
guard let filename = url.lastPathSegment else {
108137
return true
109138
}
110-
let ignoredExtensions: [FileExtension] = [
111-
"asx",
112-
"bio",
113-
"m3u",
114-
"m3u8",
115-
"pla",
116-
"pls",
117-
"smil",
118-
"txt",
119-
"vlc",
120-
"wpl",
121-
"xspf",
122-
"zpl",
123-
"jpg",
124-
"pdf",
125-
]
126-
127139
return url.pathExtension == nil
128140
|| ignoredExtensions.contains(url.pathExtension!)
129141
|| filename.hasPrefix(".")

0 commit comments

Comments
 (0)