2017-07-29 59 views
2

我正在構建使用AVFoundation的視頻編輯應用程序。當用戶導入視頻時(通過NSOpenPanel文件瀏覽器),我只想讓他們選擇AVFoundation支持的可接受的視頻格式。從UTI獲取視頻文件類型格式

您可以通過在AVURLAsset上調用audioVisualTypes()來檢索這些接受的格式。然而,這些格式包括了很多的亂碼/東西我不需要,例如:

"public.mpeg", 


    "dyn.ah62d46dzqm0gw23sqf40k4pts3y1g7pbru00g55ssvw067b4gq81q4peqz1w88brrz2de7a", 
     "public.dv-movie", 
     "public.pls-playlist", 

"dyn.ah62d46dzqm0gw23sqf40k4pts3y1g7pbru00g55ssvw067b4gq80c6durvy0g2pyrf106p50r3wc62pusb0gnpxrsbw0s7pwru", 
    "public.aac-audio", 

任何人都不會有如何分析到這些文件擴展名的簡單陣列的想法?謝謝

回答

0

你想使用函數UTTypeCopyPreferredTagWithClass。在操場試試這個

import MobileCoreServices 
import AVFoundation 

let avTypes = AVURLAsset.audiovisualTypes() 

let anAVType = avTypes[0] 

if let ext = UTTypeCopyPreferredTagWithClass(anAVType as CFString,kUTTagClassFilenameExtension)?.takeRetainedValue() { 
    print("The AVType \(anAVType) has extension '\(ext)'") 
} 
else { 
    print("Can't find the extension for the AVType '\(anAVType)") 
} 

print("\n") 

let avExtensions = avTypes.map({UTTypeCopyPreferredTagWithClass($0 as CFString,kUTTagClassFilenameExtension)?.takeRetainedValue() as String? ?? ""}) 

print("All extensions = \(avExtensions)\n") 

avExtensions = avExtensions.filter { $0.isEmpty == false } 

print("All extensions filtered = \(avExtensions)\n") 

print("First extension = '\(avExtensions[0])'") 
print("Second extension = '\(avExtensions[1])'") 
print("Third extension = '\(avExtensions[2])'") 

結果:

The AVType AVFileType(_rawValue: public.pls-playlist) has extension 'pls' 


All extensions = ["pls", "", "aifc", "m4r", "", "", "wav", "", "", "3gp", "3g2", "", "", "", "flac", "avi", "m2a", "", "aa", "", "aac", "mpa", "", "", "", "", "", "m3u", "mov", "aiff", "ttml", "", "m4v", "", "", "amr", "caf", "m4a", "mp4", "mp1", "", "m1a", "mp4", "mp2", "mp3", "itt", "au", "eac3", "", "", "webvtt", "", "", "vtt", "ac3", "m4p", "", "", "mqv"] 

All extensions filtered = ["pls", "aifc", "m4r", "wav", "3gp", "3g2", "flac", "avi", "m2a", "aa", "aac", "mpa", "m3u", "mov", "aiff", "ttml", "m4v", "amr", "caf", "m4a", "mp4", "mp1", "m1a", "mp4", "mp2", "mp3", "itt", "au", "eac3", "webvtt", "vtt", "ac3", "m4p", "mqv"] 

First extension = 'pls' 
Second extension = 'aifc' 
Third extension = 'm4r'