iphone - 视频修剪失败并阻止 AVAssetExportSessionStatus.Failed

标签 iphone swift video trim

我已转换this代码为 Swift 语言,但我得到了这个

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).

请帮我解决这个问题

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")
            }

        }


    }

最佳答案

找到解决方案: 我已经更改了这条线,它对我有用

 let FinalUrlTosave = NSURL(fileURLWithPath: outputURL)

而不是

let FinalUrlTosave = NSURL(string: outputURL)

我没有得到确切的路径。

关于iphone - 视频修剪失败并阻止 AVAssetExportSessionStatus.Failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31163082/

相关文章:

iphone - 将 UIPasteBoard 中的 vCard 粘贴到邮件中

ios - X代码 : Multiple Target

ios - 呈现一个新的 View Controller ,然后关闭它,在父 View Controller 上调用 init()

ios - 如何通过命令行对 Swift 应用程序进行代码设计?

android - Android 上的 MediaCodec 音频/视频混合问题

audio - 当 ffmpeg 中的音频较长时,将黑帧附加到视频中

ios - 跟踪 ABRecordRef 中的某些电子邮件更改

ios - UITableView 部分标题与 UITableviewCell 重叠并且没有背景色

iphone - 如何创建新的CMAttitude引用系以使重力位于Y轴上

javascript - 机器上的背景图像 Bigvideo.js 不起作用