What is wrong with this method. The filePath being passed is an nsstring path of a movie file from tmp folder and the outputPath is a renamed file path in the document folder. The exportSession is failing with a status code of 4 and description - Unknown. I want the last 5 seconds of the video.
-(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;
}
Error Details :-
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}