0

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}
Rjv
  • 19
  • 6
  • Have you looked at the values of `startTime` and `stopTime` when you run this code? – ChrisH Sep 29 '15 at 13:31
  • Yes. I tried 3 other ways too now. I am getting this error .. Please see the edited error details @ChrisH – Rjv Sep 29 '15 at 13:46
  • Take a look here: http://stackoverflow.com/questions/4001755/trying-to-understand-cmtime-and-cmtimemake – ChrisH Sep 29 '15 at 14:10
  • i am trying this now . CMTime startTime = CMTimeMake(videoStartTime, 1); CMTime stopTime = CMTimeMake(videoEndTime, 1); CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime); exportSession.timeRange = exportTimeRange; . is it right ? .. not working – Rjv Sep 29 '15 at 14:14
  • Thanks @ChrisH. I learnt and optimised my code. However i found the solution outside of this. – Rjv Sep 29 '15 at 15:29

1 Answers1

0

The above code is totally good. The problem was that i was restarting the video recording before letting the exportSession finish its job. Hence while the asynchronous task of exportSession was being processed. The input movie.mov file was being overwritten by the camera. Hence the problem. Do not repeat!!

Rjv
  • 19
  • 6