修剪后AVAssetExportSession失败

时间:2015-09-29 12:48:20

标签: objective-c avfoundation ios9 avassetexportsession

这种方法有什么问题。传递的filePath是来自tmp文件夹的电影文件的nsstring路径,outputPath是文档文件夹中的重命名文件路径。 exportSession失败,状态代码为4,描述为“未知”。我想要视频的最后5秒。

-(BOOL)trimVideofileFile:(NSString*)filePath toFileURL:(NSString*)outputPath
   {

NSURL *sourceMovieURL = [NSURL fileURLWithPath:filePath];
AVURLAsset *sourceAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];
Float64 videoLength = CMTimeGetSeconds(sourceAsset.duration);
NSLog(@"Duration of Video is %f",videoLength);

float videoStartTime = 0.0f;//define start time of video
float videoEndTime = 5.0f;//define end time of video

videoEndTime = videoLength ;
NSLog(@"Video end times - %f",videoEndTime);

if (videoLength>5.000f) {
    videoStartTime = videoEndTime - 5.000f;
    NSLog(@"Video Start Time - %f",videoStartTime);
}


NSURL *videoFileInput = [NSURL fileURLWithPath:filePath];//<Path of orignal Video file>
NSURL *videoFileOutPut = [NSURL fileURLWithPath:outputPath];


NSLog(@"Original File - %@",videoFileInput);
NSLog(@"Output Path - %@",videoFileOutPut);


if (!videoFileInput || !videoFileOutPut)
{
    return NO;
}

[[NSFileManager defaultManager] removeItemAtURL:[NSURL URLWithString:filePath] error:NULL];
AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:filePath]];

AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset
                                                                        presetName:AVAssetExportPresetLowQuality];
if (exportSession == nil)
{
    return NO;
}
CMTime startTime = CMTimeMake((int)(floor(videoStartTime * 100)), 100);
CMTime stopTime = CMTimeMake((int)(ceil(videoEndTime * 100)), 100);
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);

exportSession.outputURL = videoFileOutPut;
exportSession.timeRange = exportTimeRange;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;


[exportSession exportAsynchronouslyWithCompletionHandler:^
 {
     if (AVAssetExportSessionStatusCompleted == exportSession.status)
     {
         NSLog(@"Export OK");
     }
     else if (AVAssetExportSessionStatusFailed == exportSession.status)
     {
         NSLog(@"Export failed: %@ - %ld", [[exportSession error] localizedDescription],(long)exportSession.status);
     }

 }];
return YES;

}

错误详情: -

Export failed: Error Domain=NSURLErrorDomain Code=-1 "unknown error" UserInfo={NSErrorFailingURLStringKey=/private/var/mobile/Containers/Data/Application/489B0A94-3620-41D6-A254-454025AC32B5/tmp/movie.mov, NSErrorFailingURLKey=/private/var/mobile/Containers/Data/Application/489B0A94-3620-41D6-A254-454025AC32B5/tmp/movie.mov, NSLocalizedDescription=unknown error, NSUnderlyingError=0x15d7a990 {Error Domain=NSOSStatusErrorDomain Code=-12935 "(null)"}, NSURL=/private/var/mobile/Containers/Data/Application/489B0A94-3620-41D6-A254-454025AC32B5/tmp/movie.mov}

1 个答案:

答案 0 :(得分:0)

上面的代码非常好。问题是我在让exportSession完成其工作之前重新启动视频录制。因此,正在处理exportSession的异步任务。输入的movie.mov文件被相机覆盖。因此问题。不要重复!!