AVAssetExportSession - 导出的视频与相机胶卷不兼容
AVAssetExportSession - exported video not compatible with camera roll
我有一个错误和一个问题。我想将修改后的视频导出到相机胶卷,但导出的视频与相机胶卷不兼容。
我也想删除最初录制的视频,这样我就可以录制不止一次,但是它会产生错误并且没有意义。如果我取消注释代码,则会出现错误,告诉我最终路径不存在。我假设这会在导出修改版本之前删除初始电影。但我不明白为什么会这样,因为删除代码低于导出代码。
代码如下:
// 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
。
我有一个错误和一个问题。我想将修改后的视频导出到相机胶卷,但导出的视频与相机胶卷不兼容。
我也想删除最初录制的视频,这样我就可以录制不止一次,但是它会产生错误并且没有意义。如果我取消注释代码,则会出现错误,告诉我最终路径不存在。我假设这会在导出修改版本之前删除初始电影。但我不明白为什么会这样,因为删除代码低于导出代码。
代码如下:
// 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
。