Reputation: 404
AVAssetExportSession
takes a preset as one of its initialization parameters:
AVAssetExportSession(asset: AVAsset, presetName: String)
where the presets are settings like AVAssetExportPreset640x480
or AVAssetExportPreset1920x1080
. If however I want to encode using a custom resolution (say 250x400), is there a way to do that and if so how?
Upvotes: 3
Views: 4983
Reputation: 2505
You can use a custom resolution, investigate using AVAssetWriter instead of AVAssetExportSession. This question here has some relevant sample code Record square video using AVFoundation and add watermark
Another alternative which is reasonably straight forward is to look at using SDAVAssetExportSession https://github.com/rs/SDAVAssetExportSession which is a "drop in" replacement for AVAssetExportSession that takes some extra settings (internally it's just an implementation of AVAssetReader and AVssetWriter wrapped to look like AVAssetExportSession but additionally exposing the videoSettings and audioSettings options).
Upvotes: 0
Reputation: 1224
These export options are defined and could not able to allow you to encode using a custom resolution. Alternatively, you can try this approach
func exportVideo(asset:AVAsset, renderedWidth: CGFloat, renderedHeight: CGFloat, exportCompletionHandler: (() -> Void)?) {
let videoTrack: AVAssetTrack = asset.tracksWithMediaType(AVMediaTypeVideo)[0]
let videoComposition = AVMutableVideoComposition()
videoComposition.frameDuration = CMTimeMake(1, 30)
videoComposition.renderSize = CGSizeMake(renderedWidth, renderedHeight)
let instruction: AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction.init()
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30))
let transformer: AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack);
//Apply any transformer if needed
//
instruction.layerInstructions = [transformer]
videoComposition.instructions = [instruction]
//Create export path
let exportPath: NSURL = NSURL(fileURLWithPath: "export_path_here")
//
let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)
exporter?.videoComposition = videoComposition
exporter?.outputURL = exportPath
exporter?.outputFileType = AVFileTypeQuickTimeMovie
exporter?.exportAsynchronouslyWithCompletionHandler({ () -> Void in
//Do sth when finished
if let handler = exportCompletionHandler {
handler()
}
})
}
I hope this would be helpful.
Reference: https://www.one-dreamer.com/cropping-video-square-like-vine-instagram-xcode/
Upvotes: 2