5

我正在尝试从 UIImagePickerController 创建的源视频中导出 .mov 文件。问题是 AVAssetExportSession 创建的输出文件只有 668 字节。为什么会失败?我的代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSURL *imagePickerVideoURL = [info objectForKey:UIImagePickerControllerMediaURL];
    NSString *filename = @"vid1.mov";

    AVAsset *video = [AVAsset assetWithURL:imagePickerVideoURL];
    AVAssetExportSession *exportSession
      = [AVAssetExportSession exportSessionWithAsset:video presetName:AVAssetExportPresetMediumQuality];
    exportSession.shouldOptimizeForNetworkUse = YES;
    exportSession.outputFileType = AVFileTypeQuickTimeMovie;
    exportSession.outputURL = [pathToSavedVideosDirectory URLByAppendingPathComponent:filename];
    NSLog(@"processing video...: %@", exportSession);
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
                       NSLog(@"done processing video!");
                   }];
}
4

5 回答 5

5

我猜这是因为exportSession没有保留在内存中,因此在完成后被杀死didFinishPickingMediaWithInfo(在导出会话完成处理程序运行之前)。

存储exportSession@property或更改您的完成处理程序以复制引用,exportSession如下所示:

[exportSession exportAsynchronouslyWithCompletionHandler:^{
       if (AVAssetExportSessionStatusCompleted == exportSession.status)
       {
           NSLog(@"done processing video!");
       }
}];
于 2012-08-09T05:12:22.100 回答
3

您需要使用 AVAssetExportSession. 写下视频文件的步骤。

Asset 从放置你的文件URL开始

AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:fileURL options:nil];

创造AVMutableComposition

AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo  preferredTrackID:kCMPersistentTrackID_Invalid];

AVAssetTrack *clipVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)    ofTrack:clipVideoTrack  atTime:kCMTimeZero error:nil];
[compositionVideoTrack setPreferredTransform:[[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]];

    AVAssetTrack *clipAudioTrack = [[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
    AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,videoAsset.duration) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil];

AVMutableVideoComposition如果需要,可以使用 Animation 图层创建

AVMutableVideoComposition* videoComp = [[AVMutableVideoComposition videoComposition] retain];
    videoComp.renderSize = CGSizeMake(videoSize.width, videoSize.height);
    videoComp.frameDuration = CMTimeMake(1, 30);
    videoComp.renderScale = 1.0;
    videoComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];

动画层在这里,如果你需要,否则通过nil

CGSize videoSize = [videoAsset naturalSize];

//layer mixing
CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
videoLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
[parentLayer addSublayer:videoLayer];

将动画添加到您需要添加的所选图层sublayerparentLayer

在视频合成中添加说明,并提及动画层和持续时间。

AVMutableVideoCompositionInstruction

AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, [mixComposition duration]);
AVAssetTrack *videoTrack = [[mixComposition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];
videoComp.instructions = [NSArray arrayWithObject: instruction];

现在AVAssetExportSession使用AVMutableCompositionand创建AVMutableVideoComposition

_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
_assetExport.videoComposition = videoComp;

//为导出的视频文件创建tmepory路径。.m4a 将是 AVFoundation 的首选扩展名。

NSString *videoName = @"demo.m4a";
NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];
NSURL *exportUrl = [NSURL fileURLWithPath:exportPath];
if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) 
{
    [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
}

在导出会话中添加 url 和一些必需的属性。

_assetExport.outputFileType = AVFileTypeQuickTimeMovie; 
_assetExport.outputURL = exportUrl;
_assetExport.shouldOptimizeForNetworkUse = YES;

开始导出会话并在您收到完整消息时执行操作。

[_assetExport exportAsynchronouslyWithCompletionHandler:
 ^(void ) {
     switch (_assetExport.status)
     {
         case AVAssetExportSessionStatusUnknown:
             NSLog(@"Unknown");
         case AVAssetExportSessionStatusExporting:
             NSLog(@"Exporting");
         case AVAssetExportSessionStatusFailed:
             NSLog(@"exporting failed");
             break;
         case AVAssetExportSessionStatusCompleted:
             NSLog(@"exporting completed");
             [self performSelectorOnMainThread:@selector(completeVideoExport) withObject:nil waitUntilDone:YES];
             break;
         case AVAssetExportSessionStatusCancelled:
             NSLog(@"export cancelled");
             break;
     }
     _assetExport = nil;
     [_assetExport release];
 }       
 ];   

如果你需要更多让我知道..

于 2012-09-12T05:39:44.020 回答
1

对于调试,试试这个...

[exportSession exportAsynchronouslyWithCompletionHandler:^
{
    dispatch_async(dispatch_get_main_queue(), ^{
                 NSLog(@"exportSessionMetaData:%@", exportSession.metadata);

                 if(exportSession.status == AVAssetExportSessionStatusCompleted){
                     NSError *dataReadingError = nil;
                     NSData *videoData = nil;
                     videoData = 
                                [NSData dataWithContentsOfURL:[pathToSavedVideosDirectory  
                                                       URLByAppendingPathComponent:filename];

                                                       options:NSDataReadingMapped 
                                                         error:&dataReadingError];
                     if (videoData != nil) {
                         // You got video data, do you work here...
                     } else {
                         NSLog(@"Failed to load the video data. ERROR:%@", dataReadingError);
                     }
                 }
             });
}];
于 2012-08-10T03:56:27.167 回答
1

exportSession 有一个“错误”属性,您可以检查该属性以确定错误是什么。很多时候这些错误并不是特别有用,但它至少是一个开始的地方。

请注意,我发现 .mov 文件存在问题,其中的编解码器不受 AVFoundation 支持。

于 2012-08-03T23:39:04.553 回答
0

查看 TSLibraryImport - 导入代码有效。

于 2012-08-09T02:29:24.617 回答