在iOS中播放视频到广场[Swift 3]

时间:2017-03-24 01:01:11

标签: ios swift video crop

我无法使用Swift 3和iOS 10.x裁剪视频。在我执行裁剪程序后,我将视频保存到我的照片库中,它看起来与原始照片相同。

我使用以下帖子作为参考:Cropping AVAsset video with AVFoundation not working iOS 8

func suqareCropVideo(inputURL: NSURL, completion: @escaping (_ outputURL : NSURL?) -> ())
{
    let videoAsset: AVAsset = AVAsset( url: inputURL as URL )
    let clipVideoTrack = videoAsset.tracks( withMediaType: AVMediaTypeVideo ).first! as AVAssetTrack

    let composition = AVMutableComposition()
    composition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())

    let videoComposition = AVMutableVideoComposition()
    videoComposition.renderSize = CGSize( width: clipVideoTrack.naturalSize.height, height: clipVideoTrack.naturalSize.height )
    videoComposition.frameDuration = CMTimeMake(1, 30)

    let transformer = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)

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


    let transform1: CGAffineTransform = CGAffineTransform(translationX: clipVideoTrack.naturalSize.height, y: (clipVideoTrack.naturalSize.width - clipVideoTrack.naturalSize.height) / 2)
    let transform2 = transform1.rotated(by: .pi/2)
    let finalTransform = transform2


    transformer.setTransform(finalTransform, at: kCMTimeZero)

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

    // Export
    let exportSession = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPresetHighestQuality)!
    print ("random id = \(NSUUID().uuidString)")

    let croppedOutputFileUrl = URL( fileURLWithPath: getOutputPath( NSUUID().uuidString) ) // CREATE RANDOM FILE NAME HERE
    exportSession.outputURL = croppedOutputFileUrl
    exportSession.outputFileType = AVFileTypeQuickTimeMovie

    exportSession.exportAsynchronously() { handler -> Void in
        if exportSession.status == .completed {
            print("Export complete")
            DispatchQueue.main.async(execute: {
                completion(croppedOutputFileUrl as NSURL)
            })
            return
        } else if exportSession.status == .failed {
            print("Export failed - \(String(describing: exportSession.error))")
        }

        completion(nil)
        return
    }
}

用法如下所示:

    if videoURL != nil {
        print ("crop video")
        suqareCropVideo(inputURL: self.videoURL, completion: { (outputURL) -> () in
            print ("compressed url = \(String(describing: outputURL))")

            // Save video to photo library
            PHPhotoLibrary.shared().performChanges({
                PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL:outputURL! as URL)
            }) { saved, error in
                if saved {
                    print ("save successful")
                }
                else {
                    print ("save failed")
                }
            }
        })
    }

2 个答案:

答案 0 :(得分:4)

您实际上并未在导出器上设置视频合成

所以试试

exportSession.videoComposition = videoComposition

开始导出之前。

答案 1 :(得分:1)

presetName: AVAssetExportPresetHighestQuality,

更改当前名称,您可以从多个选项中进行选择。 就我而言: let preferredPreset = AVAssetExportPresetLowQuality有用。