-
Notifications
You must be signed in to change notification settings - Fork 273
/
AvController.swift
53 lines (48 loc) · 1.57 KB
/
AvController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import AVFoundation
import MobileCoreServices
class AvController: NSObject {
public func getVideoAsset(_ url:URL)->AVURLAsset {
return AVURLAsset(url: url)
}
public func getTrack(_ asset: AVURLAsset)->AVAssetTrack? {
var track : AVAssetTrack? = nil
let group = DispatchGroup()
group.enter()
asset.loadValuesAsynchronously(forKeys: ["tracks"], completionHandler: {
var error: NSError? = nil;
let status = asset.statusOfValue(forKey: "tracks", error: &error)
if (status == .loaded) {
track = asset.tracks(withMediaType: AVMediaType.video).first
}
group.leave()
})
group.wait()
return track
}
public func getVideoOrientation(_ path:String)-> Int? {
let url = Utility.getPathUrl(path)
let asset = getVideoAsset(url)
guard let track = getTrack(asset) else {
return nil
}
let size = track.naturalSize
let txf = track.preferredTransform
if size.width == txf.tx && size.height == txf.ty {
return 0
} else if txf.tx == 0 && txf.ty == 0 {
return 90
} else if txf.tx == 0 && txf.ty == size.width {
return 180
} else {
return 270
}
}
public func getMetaDataByTag(_ asset:AVAsset,key:String)->String {
for item in asset.commonMetadata {
if item.commonKey?.rawValue == key {
return item.stringValue ?? "";
}
}
return ""
}
}