-
Notifications
You must be signed in to change notification settings - Fork 180
Enhance AudioParser
#414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Enhance AudioParser
#414
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
364d2cf
Add resource metadata extraction to AudioParser
domkm 21e93ad
Attempt to fix CI failure
domkm 370dc81
Make AudioParser metadata logic configurable
domkm 4140284
Merge branch 'develop' of github.com:readium/swift-toolkit into audio…
domkm 4f39967
Update AudioParser for 3.0.0
domkm 3a78647
Remove malfunctioning bitrate extraction
domkm 39096c4
Minor refactoring and update changelog
mickael-menu 0cc6665
Update Carthage project
mickael-menu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
Sources/Streamer/Parser/Audio/AudioPublicationManifestAugmentor.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // | ||
| // Copyright 2024 Readium Foundation. All rights reserved. | ||
| // Use of this source code is governed by the BSD-style license | ||
| // available in the top-level LICENSE file of the project. | ||
| // | ||
|
|
||
| import AVFoundation | ||
| import Foundation | ||
| import ReadiumShared | ||
| import UIKit | ||
|
|
||
| /// Implements a strategy to augment a `Manifest` of an audio publication with additional metadata and | ||
| /// cover, for example by looking into the audio files metadata. | ||
| public protocol AudioPublicationManifestAugmentor { | ||
| func augment(_ baseManifest: Manifest, using fetcher: Fetcher) -> AudioPublicationAugmentedManifest | ||
| } | ||
|
|
||
| public struct AudioPublicationAugmentedManifest { | ||
| var manifest: Manifest | ||
| var cover: UIImage? | ||
| } | ||
|
|
||
| /// An `AudioPublicationManifestAugmentor` using AVFoundation to retrieve the audio metadata. | ||
| /// | ||
| /// It will only work for local publications (file://). | ||
| public final class AVAudioPublicationManifestAugmentor: AudioPublicationManifestAugmentor { | ||
| public init() {} | ||
|
|
||
| public func augment(_ manifest: Manifest, using fetcher: Fetcher) -> AudioPublicationAugmentedManifest { | ||
| let avAssets = manifest.readingOrder.map { link in | ||
| fetcher.get(link).file | ||
| .map { AVURLAsset(url: $0.url, options: [AVURLAssetPreferPreciseDurationAndTimingKey: true]) } | ||
| } | ||
| var manifest = manifest | ||
| manifest.readingOrder = zip(manifest.readingOrder, avAssets).map { link, avAsset in | ||
| guard let avAsset = avAsset else { return link } | ||
| var link = link | ||
| link.title = avAsset.metadata.filter([.commonIdentifierTitle]).first(where: { $0.stringValue }) | ||
| link.duration = avAsset.duration.seconds | ||
| return link | ||
| } | ||
| let avMetadata = avAssets.compactMap { $0?.metadata }.reduce([], +) | ||
| var metadata = manifest.metadata | ||
| metadata.localizedTitle = avMetadata.filter([.commonIdentifierTitle, .id3MetadataAlbumTitle]).first(where: { $0.stringValue })?.localizedString ?? manifest.metadata.localizedTitle | ||
| metadata.localizedSubtitle = avMetadata.filter([.id3MetadataSubTitle, .iTunesMetadataTrackSubTitle]).first(where: { $0.stringValue })?.localizedString | ||
| metadata.modified = avMetadata.filter([.commonIdentifierLastModifiedDate]).first(where: { $0.dateValue }) | ||
| metadata.published = avMetadata.filter([.commonIdentifierCreationDate, .id3MetadataDate]).first(where: { $0.dateValue }) | ||
| metadata.languages = avMetadata.filter([.commonIdentifierLanguage, .id3MetadataLanguage]).compactMap(\.stringValue).removingDuplicates() | ||
| metadata.subjects = avMetadata.filter([.commonIdentifierSubject]).compactMap(\.stringValue).removingDuplicates().map { Subject(name: $0) } | ||
| metadata.authors = avMetadata.filter([.commonIdentifierAuthor, .iTunesMetadataAuthor]).compactMap(\.stringValue).removingDuplicates().map { Contributor(name: $0) } | ||
| metadata.artists = avMetadata.filter([.commonIdentifierArtist, .id3MetadataOriginalArtist, .iTunesMetadataArtist, .iTunesMetadataOriginalArtist]).compactMap(\.stringValue).removingDuplicates().map { Contributor(name: $0) } | ||
| metadata.illustrators = avMetadata.filter([.iTunesMetadataAlbumArtist]).compactMap(\.stringValue).removingDuplicates().map { Contributor(name: $0) } | ||
| metadata.contributors = avMetadata.filter([.commonIdentifierContributor]).compactMap(\.stringValue).removingDuplicates().map { Contributor(name: $0) } | ||
| metadata.publishers = avMetadata.filter([.commonIdentifierPublisher, .id3MetadataPublisher, .iTunesMetadataPublisher]).compactMap(\.stringValue).removingDuplicates().map { Contributor(name: $0) } | ||
| metadata.description = avMetadata.filter([.commonIdentifierDescription, .iTunesMetadataDescription]).first?.stringValue | ||
| metadata.duration = avAssets.reduce(0) { duration, avAsset in | ||
| guard let duration = duration, let avAsset = avAsset else { return nil } | ||
| return duration + avAsset.duration.seconds | ||
| } | ||
| manifest.metadata = metadata | ||
| let cover = avMetadata.filter([.commonIdentifierArtwork, .id3MetadataAttachedPicture, .iTunesMetadataCoverArt]).first(where: { $0.dataValue.flatMap(UIImage.init(data:)) }) | ||
| return .init(manifest: manifest, cover: cover) | ||
| } | ||
| } | ||
|
|
||
| private extension [AVMetadataItem] { | ||
| func filter(_ identifiers: [AVMetadataIdentifier]) -> [AVMetadataItem] { | ||
| identifiers.flatMap { AVMetadataItem.metadataItems(from: self, filteredByIdentifier: $0) } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.