正在修剪音频,但视频是空白的。
这是启动修剪的功能
- (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];
}
这是导出和修剪视频的功能。
+(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;
}
这是启动视频播放的功能
-(void)finishedTrimming:(NSNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
NSURL *outputURL = [userInfo objectForKey:@"outputURL"];
[self playVideoWithURL:outputURL];
}
这是播放视频的功能
-(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];
}
答案 0 :(得分:1)
谢谢ChrisH,你是对的! Export正在另一个线程上进行,因此在处理程序中我需要获取主队列...
我需要在
之后获得主线程case AVAssetExportSessionStatusCompleted:{
dispatch_async(dispatch_get_main_queue(), ^{
//post the notification!
});
break;
}