Reputation: 304
It is trimming the audio, but the video is blank.
This is the function that initiates trimming
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor blackColor];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(finishedTrimming:)
name:@"videoFinishedTrimming"
object:nil];
NSURL *furl = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"capture0.mp4"]];
//[self playVideoWithURL:furl]; //Play original video
//Play trimmed video
CMTime startTrim = CMTimeMake(0, 1);
CMTime endTrim = CMTimeMake(2,1);
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTrim, endTrim);
[ProcessingHelper trimAssetWithURL:furl andRange:exportTimeRange];
}
This is the function that exports and trims the video.
+(void)trimAssetWithURL:(NSURL *)urlIn andRange:(CMTimeRange)timeRangeIn
{
AVAsset *videoAsset = [AVAsset assetWithURL:urlIn];
//Creates the session with the videoasset
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:videoAsset presetName:AVAssetExportPresetHighestQuality];
//Creates the path to export to - Saving to temporary directory
NSString* filename = [NSString stringWithFormat:@"TrimmedCapture%d.mp4", 0];
NSString* path = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
//Checks if there is already a file at the output URL. session will not overwrite previous data
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSLog(@"Removing item at path: %@", path);
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
//Set the output url
exportSession.outputURL = [NSURL fileURLWithPath:path];
//Set the output file type
exportSession.outputFileType = AVFileTypeMPEG4; //AVFileTypeAC3; // AVFileTypeMPEGLayer3; // AVFileTypeWAVE; // AVFileTypeQuickTimeMovie;
exportSession.timeRange = timeRangeIn;
exportSession.metadata = nil;
//Exports!
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch (exportSession.status) {
case AVAssetExportSessionStatusCompleted:{
NSLog(@"Export Complete");
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:exportSession.outputURL, @"outputURL", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"videoFinishedTrimming" object:self userInfo:options];
break;
}
case AVAssetExportSessionStatusFailed:
NSLog(@"Export Error: %@", [exportSession.error description]);
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export Cancelled");
break;
default:
break;
}
}];
exportSession = nil;
}
This is the function that initiates the playing of the video
-(void)finishedTrimming:(NSNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
NSURL *outputURL = [userInfo objectForKey:@"outputURL"];
[self playVideoWithURL:outputURL];
}
This is the function that plays the video
-(void)playVideoWithURL:(NSURL *)furl
{
NSData *movieData;
NSError *dataReadingError = nil;
movieData = [NSData dataWithContentsOfURL: furl options:NSDataReadingMapped error:&dataReadingError];
if(movieData != nil)
NSLog(@"Successfully loaded the data.");
else
NSLog(@"Failed to load the data with error = %@", dataReadingError);
//AVPlayer
self.avPlayer = [AVPlayer playerWithURL:furl];
AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
avPlayerLayer.frame = self.vPlayBackMovie.bounds;
avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
avPlayerLayer.needsDisplayOnBoundsChange = YES;
[self.vPlayBackMovie.layer addSublayer:avPlayerLayer];
self.vPlayBackMovie.layer.needsDisplayOnBoundsChange = YES;
[self.avPlayer play];
}
Upvotes: 1
Views: 3146
Reputation: 304
Thanks ChrisH, you were right! The Export was taking place on another thread so in the handler I need to get the main queue...
I needed to get the main thread after
case AVAssetExportSessionStatusCompleted:{
dispatch_async(dispatch_get_main_queue(), ^{
//post the notification!
});
break;
}
Upvotes: 1