I've been trying to find out how to use UTIs (Unified Type Identifier) on macOS in swift, but have not been able to find anything. Does anyone know how I could use UTIs to get a list of video files in a directory that are playable by AVPlayer?
Using Hiroki Kato's UTIKit (https://github.com/cockscomb/UTIKit):
let dir = URL(fileURLWithPath: "./your_dir_here")
let mimeTypes = AVURLAsset.audiovisualMIMETypes()
let playableUtis = mimeTypes.map { UTI(mimeType: $0)! }
let dirContents = try! FileManager.default.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil, options: [.skipsHiddenFiles])
let movies = dirContents.filter { UTI("public.movie") ~= UTI(filenameExtension: $0.pathExtension) }
let playableMovies = movies.filter {
guard let movieUti = UTI(filenameExtension: $0.pathExtension) else {
return false
}
return playableUtis.contains(movieUti)
}
Now playableMovies
contains only movie-files playable by AVPlayer
.