-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
431f2f2
commit adc37fa
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
MemoryChainKit/Sources/Extensions/AVFoundation/AVURLAssetExtension.swift
This file contains 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,32 @@ | ||
|
||
|
||
//Copyright belongs to Marc Steven in 8th Nov, 2023 | ||
|
||
|
||
import AVFoundation | ||
import Foundation | ||
|
||
public extension AVURLAsset { | ||
/// Audio format for the file in the URL asset | ||
var audioFormat: AVAudioFormat? { | ||
// pull the input format out of the audio file... | ||
if let source = try? AVAudioFile(forReading: url) { | ||
return source.fileFormat | ||
|
||
// if that fails it might be a video, so check the tracks for audio | ||
} else { | ||
let audioTracks = tracks.filter { $0.mediaType == .audio } | ||
|
||
guard !audioTracks.isEmpty else { return nil } | ||
|
||
let formatDescriptions = audioTracks.compactMap { | ||
$0.formatDescriptions as? [CMFormatDescription] | ||
}.reduce([], +) | ||
|
||
let audioFormats: [AVAudioFormat] = formatDescriptions.compactMap { | ||
AVAudioFormat(cmAudioFormatDescription: $0) | ||
} | ||
return audioFormats.first | ||
} | ||
} | ||
} |