Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Shared/Services/AudioMetadataService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ public class AudioMetadataService: BPLogger, AudioMetadataServiceProtocol {
public init() {}

public func extractMetadata(from fileURL: URL) async -> AudioMetadata? {
let asset = AVURLAsset(url: fileURL)
// MP3 and Ogg lack global duration headers. We request precise timing to prevent
// inaccurate duration estimates for VBR files by forcing a full stream scan.
let requiresPreciseDuration = ["opus", "ogg", "mp3"].contains(fileURL.pathExtension.lowercased())
let options: [String: Any]? = requiresPreciseDuration ? [AVURLAssetPreferPreciseDurationAndTimingKey: true] : nil
let asset = AVURLAsset(url: fileURL, options: options)
return await extractMetadata(from: asset)
}

Expand Down Expand Up @@ -232,7 +236,11 @@ public class AudioMetadataService: BPLogger, AudioMetadataServiceProtocol {
}

public func extractManualChapters(from fileURL: URL) async -> [ChapterMetadata]? {
let asset = AVURLAsset(url: fileURL)
// MP3 and Ogg lack global duration headers. We request precise timing to prevent
// inaccurate duration estimates for VBR files by forcing a full stream scan.
let requiresPreciseDuration = ["opus", "ogg", "mp3"].contains(fileURL.pathExtension.lowercased())
let options: [String: Any]? = requiresPreciseDuration ? [AVURLAssetPreferPreciseDurationAndTimingKey: true] : nil
let asset = AVURLAsset(url: fileURL, options: options)
do {
let metadata = try await asset.load(.metadata)
let duration = CMTimeGetSeconds(try await asset.load(.duration))
Expand Down
8 changes: 7 additions & 1 deletion Shared/Services/LibraryService+Sync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,12 @@ extension LibraryService: LibrarySyncProtocol {
public func loadChaptersIfNeeded(relativePath: String) async {
let fileURL = DataManager.getProcessedFolderURL().appendingPathComponent(relativePath)

await loadChaptersIfNeeded(relativePath: relativePath, asset: AVAsset(url: fileURL))
// MP3 and Ogg lack global duration headers. We request precise timing to prevent
// inaccurate duration estimates for VBR files by forcing a full stream scan.
let requiresPreciseDuration = ["opus", "ogg", "mp3"].contains(fileURL.pathExtension.lowercased())
let options: [String: Any]? = requiresPreciseDuration ? [AVURLAssetPreferPreciseDurationAndTimingKey: true] : nil
let asset = AVURLAsset(url: fileURL, options: options)

await loadChaptersIfNeeded(relativePath: relativePath, asset: asset)
}
}
6 changes: 5 additions & 1 deletion Shared/Services/Sync/SyncService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,11 @@ extension SyncService {

let actualDuration: Double
do {
let asset = AVURLAsset(url: fileURL)
// MP3 and Ogg lack global duration headers. We request precise timing to prevent
// inaccurate duration estimates for VBR files by forcing a full stream scan.
let requiresPreciseDuration = ["opus", "ogg", "mp3"].contains(fileURL.pathExtension.lowercased())
let options: [String: Any]? = requiresPreciseDuration ? [AVURLAssetPreferPreciseDurationAndTimingKey: true] : nil
let asset = AVURLAsset(url: fileURL, options: options)
actualDuration = CMTimeGetSeconds(try await asset.load(.duration))
} catch {
/// We have a trustworthy synced duration, so this is a format we can play —
Expand Down