Extended AVMutableComposition so that it can be exported.
However, even a 1-second video does not progress at all when export is performed.
I tried to print out the progress, and found that it didn't make any progress at 0.0.
What could be the possible cause?
extension AVMutableComposition {
private struct AssociatedKeys {
static var initialTransform: CGAffineTransform!
}
var initialTransform: CGAffineTransform {
get {
objc_getAssociatedObject(self, &AssociatedKeys.initialTransform) as! CGAffineTransform
}
set(newValue) {
objc_setAssociatedObject(self, &AssociatedKeys.initialTransform, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
convenience init(asset: AVAsset) {
self.init()
let videoTrack = asset.tracks(withMediaType: .video).last!
self.initialTransform = videoTrack.preferredTransform
do {
try insertTimeRange(CMTimeRange(start: .zero, duration: asset.duration), of: asset, at: .zero)
} catch let error {
debugPrint(error)
}
}
func export() {
guard let exportSession = AVAssetExportSession(asset: self, presetName: AVAssetExportPresetLowQuality) else {
print("Could not create an export session")
}
let outputURL = URL(fileURLWithPath: NSTemporaryDirectory() + "video.mp4")
exportSession.outputURL = outputURL
exportSession.outputFileType = .mp4
exportSession.shouldOptimizeForNetworkUse = true
let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
print(exportSession.progress, exportSession.status.rawValue) // 0.0 1
}
exportSession.exportAsynchronously {
//
}
}
}