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 あなたのfileURLを置くことから始めます

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

のように追加する必要がある選択したレイヤーにアニメーションを追加しsublayerますparentLayer

言及アニメーション レイヤーと時間の長さでビデオ構成に指示を追加します。

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と を使用してAVMutableComposition作成中AVMutableVideoComposition

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

//エクスポートされたビデオ ファイルの一時パスを作成します。.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 に答える