简体   繁体   中英

Video Trimming failed with block AVAssetExportSessionStatus.Failed

I have converted this code to Swift language but i am getting this

Error: Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x174278600 {NSUnderlyingError=0x170241d10 "The operation couldn't be completed. (OSStatus error -12780.)", NSLocalizedFailureReason=An unknown error occurred (-12780), NSLocalizedDescription=The operation could not be completed} in (case AVAssetExportSessionStatus.Failed).

Kindly help me to resolved this

func cropVideo(sourceURL: NSURL)
    {
        let asset = AVURLAsset(URL: sourceURL, options: nil)

        let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)
                var error : NSError?
   let file = "Finaloutput.mp4"
     /*   let paths : AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0]
        let outputURL1 = paths[0] as? String*/
        let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
        let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask
        let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)

        let outputURL1 = paths[0] as? String

        let filemgr = NSFileManager.defaultManager()
        filemgr.createDirectoryAtPath(outputURL1!, withIntermediateDirectories: true, attributes: nil, error: &error)
        var outputURL = outputURL1!.stringByAppendingPathComponent(file)
        filemgr.removeItemAtPath(outputURL, error: &error)

        let FinalUrlTosave = NSURL(string: outputURL)
        exportSession.outputURL=FinalUrlTosave
        exportSession.shouldOptimizeForNetworkUse = true
        // exportSession.outputFileType = AVFileTypeQuickTimeMovie
        exportSession.outputFileType = AVFileTypeQuickTimeMovie;
        let start:CMTime
        let duration:CMTime
         start = CMTimeMakeWithSeconds(1.0, 600)
         duration = CMTimeMakeWithSeconds(19.0, 600)
        // let timeRangeForCurrentSlice = CMTimeRangeMake(start, duration)
        let range = CMTimeRangeMake(start, duration);
        exportSession.timeRange = range

        let destinationURL1 = NSURL(string: outputURL)

        exportSession.exportAsynchronouslyWithCompletionHandler({
            switch exportSession.status{
            case  AVAssetExportSessionStatus.Failed:

                println("failed \(exportSession.error)")
            case AVAssetExportSessionStatus.Cancelled:
                println("cancelled \(exportSession.error)")
            default:
                println("complete....complete")
                self.SaveVideoToPhotoLibrary(destinationURL1!)

            }
        })
    }

  func SaveVideoToPhotoLibrary(outputFileURL: NSURL)

    {


        assetsLibrary = ALAssetsLibrary()

        let videoURL = outputFileURL as NSURL?

        if let library = assetsLibrary{

            if let url = videoURL{

                library.writeVideoAtPathToSavedPhotosAlbum(url,
                    completionBlock: {(url: NSURL!, error: NSError!) in

                        print(url)

                        if let theError = error{
                            print("Error happened while saving the video")
                            print("The error is = \(theError)")
                        } else {
                            print("no errors happened")
                        }

                })
            } else {
                print("Could not find the video in the app bundle")
            }

        }


    }

Found Solution : I have change this line and it works for me

 let FinalUrlTosave = NSURL(fileURLWithPath: outputURL)

instead of

let FinalUrlTosave = NSURL(string: outputURL)

I was not getting exact path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM