使用AVFoundation裁剪AVAsset视频无法在iOS 8上运行 [英] Cropping AVAsset video with AVFoundation not working iOS 8

查看:95
本文介绍了使用AVFoundation裁剪AVAsset视频无法在iOS 8上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是困扰我的最后一天,我曾经在ObjC中使用此方法将视频裁剪为正方形,这似乎是我几年来发现的唯一可行的方法,但最近尝试进行裁剪在Swift&中使用它iOS 8似乎根本没有裁剪视频,希望有人能帮忙吗?

This has been bugging me for the last day, I used to use this method in ObjC to crop videos into square, it seems to be the only method i've found in a few years that worked but after recently trying to crop using it in Swift & iOS 8 it doesn't seem to crop the video at all, Hopefully somebody can help?

func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
    if error != nil {
        println("Error Outputting recording")
    } else {
        self.writeVideoToAssetsLibrary(self.outputUrl!.copy() as NSURL)
    }
}

func writeVideoToAssetsLibrary(videoUrl: NSURL) {
    var videoAsset: AVAsset = AVAsset.assetWithURL(videoUrl) as AVAsset

    var clipVideoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo).first as AVAssetTrack

    var composition = AVMutableComposition()
    composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())

    var videoComposition = AVMutableVideoComposition()

    videoComposition.renderSize = CGSizeMake(clipVideoTrack.naturalSize.height, clipVideoTrack.naturalSize.height)
    videoComposition.frameDuration = CMTimeMake(1, 30)


    var transformer = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)

    var instruction = AVMutableVideoCompositionInstruction()
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30))

    var transform1: CGAffineTransform = CGAffineTransformMakeTranslation(clipVideoTrack.naturalSize.height, (clipVideoTrack.naturalSize.width - clipVideoTrack.naturalSize.height) / 2)
    var transform2 = CGAffineTransformRotate(transform1, CGFloat(M_PI_2))
    var finalTransform = transform2


    transformer.setTransform(finalTransform, atTime: kCMTimeZero)

    instruction.layerInstructions = [transformer]
    videoComposition.instructions = [instruction]

    var exporter = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPresetHighestQuality)
    exporter.videoComposition = videoComposition
    exporter.outputURL = videoUrl
    exporter.outputFileType = AVFileTypeQuickTimeMovie

    exporter.exportAsynchronouslyWithCompletionHandler({ () -> Void in
        dispatch_async(dispatch_get_main_queue(), {
            self.handleExportCompletion(exporter)
        })
    })
}

func handleExportCompletion(session: AVAssetExportSession) {
    var library = ALAssetsLibrary()

    if library.videoAtPathIsCompatibleWithSavedPhotosAlbum(session.outputURL) {
        var completionBlock: ALAssetsLibraryWriteVideoCompletionBlock

        completionBlock = { assetUrl, error in
            if error != nil {
                println("error writing to disk")
            } else {

            }
        }

        library.writeVideoAtPathToSavedPhotosAlbum(outputUrl, completionBlock: completionBlock)
    }
}

推荐答案

因此,事实证明,如果您尝试将导出器的outputUrl设置为与资产相同,那么就像我所做的那样,它不会对其进行编辑,太愚蠢了!因此,为了将来引用,应将outputUrl设置为一个新的唯一

So it turns out that if you try setting the exporter's outputUrl to the same as the asset your editing like I did it doesn't edit it, so stupid! so for future referencing, outputUrl should be set to a new unique one

这篇关于使用AVFoundation裁剪AVAsset视频无法在iOS 8上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆