Skip to content

Commit

Permalink
更新 AVURLAssetExtension.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcSteven authored Nov 8, 2023
1 parent 431f2f2 commit adc37fa
Showing 1 changed file with 32 additions and 0 deletions.
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
}
}
}

0 comments on commit adc37fa

Please sign in to comment.