Skip to content

Instantly share code, notes, and snippets.

@tuchangwei
Last active October 28, 2015 03:59
Show Gist options
  • Save tuchangwei/5fa9872b395db1346e26 to your computer and use it in GitHub Desktop.
Save tuchangwei/5fa9872b395db1346e26 to your computer and use it in GitHub Desktop.
视频读取
====
NSFileManager.defaultManager().createFileAtPath(videoPath, contents: nil, attributes: nil)
if let fileHander = NSFileHandle(forWritingAtPath: videoPath) {
let BufferSize = 1024*1024
let buffer = calloc(BufferSize, sizeof(UInt8))
let bufferPtr8: UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>(buffer)
var offset = Int64(0)
var bytesRead = 0
repeat {
bytesRead = representation.getBytes(bufferPtr8, fromOffset: offset, length: BufferSize, error: nil)
let data = NSData(bytesNoCopy: bufferPtr8, length: bytesRead, freeWhenDone: false)
fileHander.writeData(data)
offset += bytesRead
} while(bytesRead > 0)
free(bufferPtr8)
=====
视频压缩
====
let urlAsset = AVURLAsset(URL: representation.url())
let exportSession = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetMediumQuality)
exportSession?.outputURL = NSURL(fileURLWithPath: videoPath)
exportSession?.outputFileType = AVFileTypeQuickTimeMovie
exportSession?.exportAsynchronouslyWithCompletionHandler({ () -> Void in
if exportSession?.status == .Completed {
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment