ios - AVAssetExportSession - 导出的视频与相机胶卷不兼容

标签 ios swift avfoundation

我有一个错误和一个问题。我想将修改后的视频导出到相机胶卷,但导出的视频与相机胶卷不兼容。

我也想删除最初录制的视频,以便我可以录制不止一次,但它会产生错误并且没有意义。如果我取消注释代码,则会出现错误,告诉我最终路径不存在。我假设这会在导出修改版本之前删除初始电影。但我不明白为什么会发生这种情况,因为删除代码低于导出代码。

代码如下:

    // Create Date Formatter
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd-hh-mm-ss"

    // Create Export Session
    let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)
    exportSession?.videoComposition = videoComposition
    do {
        try exportSession?.outputURL = NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true).URLByAppendingPathComponent(dateFormatter.stringFromDate(NSDate())).URLByAppendingPathExtension("mov")
    }
    catch {
        print(error)
    }
    exportSession?.outputFileType = AVFileTypeQuickTimeMovie
    exportSession?.exportAsynchronouslyWithCompletionHandler({
        print("Output File Type: \(exportSession?.outputFileType)")
        print("Output URL: \(exportSession?.outputURL?.absoluteString)")
        print("Video Compatible W/ Camera Roll: \(exportSession?.asset.compatibleWithSavedPhotosAlbum)")
        //-----SAVE-----
        if exportSession?.status == AVAssetExportSessionStatus.Completed
            {
            print("Export Finished")
            if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum((exportSession?.outputURL?.absoluteString)!) //Returns false...
            {
                UISaveVideoAtPathToSavedPhotosAlbum((exportSession?.outputURL?.absoluteString)!, self, nil, nil)
                print("Video Saved")
                // Show Message
                self.showMessage()
            }
            else
            {
                print("Video Not Saved")
            }
        }
        else if exportSession?.status == AVAssetExportSessionStatus.Failed
        {
            print("Export Error: \(exportSession?.error)")
            print("Export Failed")
        }
        else
        {
            print("Export Cancelled")
        }
    })

    // The code below generates an error
    // Remove Temporary Video
//        do
//        {
//            try NSFileManager.defaultManager().removeItemAtURL(initialOutputURL)
//        }
//        catch
//        {
//            print(error)
//        }

那么不兼容是从哪里来的呢?我要注意日志显示的是 QuickTime 格式和 720x720 分辨率。

最佳答案

好吧,所以我最终找到了答案......这也没有多大意义。也许它会帮助遇到这个问题的其他人。

因此,删除初始视频的代码必须放在 exportAsynchronouslyWithCompletionHandler内部,而不是之后。

为了保存视频,我刚刚使用了 ALAssetLibrary.videoAtPathIsCompatibleWithSavedPhotosAlbum.writeVideoAtPathToSavedPhotosAlbum。我知道它已被弃用,但我也希望与 iOS 7 兼容。如果您需要 iOS 8 及更高版本,请使用 PHPhotoLibrary。这很奇怪,因为我的初始代码和 ALAssetLibrary 版本都做完全相同的事情。另外,有趣的是,日志仍然显示视频与相机胶卷不兼容。

最后的修复是在旋转视频时使用 CGFloat(M_PI_2) 而不是 90

关于ios - AVAssetExportSession - 导出的视频与相机胶卷不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36809115/

相关文章:

ios - Objective-C - UIActivityIndi​​catorView 没有动画

iOS View 导航最佳实践

ios - 从 Firestore 中选择一个用户

swift - 在 Picker Swift 上数组选择第一个数据

ios - SwiftUI AVCapturePhotoOutput 不工作

ios - 如何将 UIImageView 设置为手机壁纸

ios - 在另一个版本中更改 NSManagedObject 父实体

swift - Cocoapod swifty json 问题

swift - 在 iOS13.1 上播放音频时应用程序崩溃

ios - AVAudioPlayer 仅在 iOS 模拟器上失败