繁体   English   中英

使用AVAssetExportSession导出电影时,为什么没有视频?

[英]Why don't I get video when exporting a movie using AVAssetExportSession?

它正在修剪音频,但视频为空白。

这是启动修剪的功能

- (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];
}

谢谢ChrisH,您说对了! 导出发生在另一个线程上,因此在处理程序中,我需要获取主队列...

我需要获得主线程后

case AVAssetExportSessionStatusCompleted:{
    dispatch_async(dispatch_get_main_queue(), ^{
        //post the notification!
    });
    break;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM