3

オーディオ ファイルとビデオ ファイルをマージしようとしています。これが私の努力です:

AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audioUrl options:nil];
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoUrl options:nil];

AVMutableComposition* mixComposition = [AVMutableComposition composition];

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

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

AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition 
                                                                      presetName:AVAssetExportPresetHighestQuality];   

NSString* videoName = @"export.mov";

NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];
NSURL    *exportUrl = [NSURL fileURLWithPath:exportPath];

if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) 
{
    [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
}

_assetExport.outputFileType = @"com.apple.quicktime-movie";
_assetExport.outputURL = exportUrl;
_assetExport.shouldOptimizeForNetworkUse = YES;

[_assetExport exportAsynchronouslyWithCompletionHandler:
 ^(void ) {      
            // your completion code here
     }       
 }
 ];

すべてが正常に機能しているように見えますが、このコードが機能しない理由がわかりません! オーディオとビデオの両方のアセットも取得しています。しかし、エクスポート セッションを作成できません。

4

1 に答える 1

2

使ってみてAVAssetExportPresetPassthrough

完了コードを使用してエラーを取得できるかどうかも確認してください

 [_assetExport exportAsynchronouslyWithCompletionHandler:
     ^(void ) { 
         switch (_assetExport.status) 
         {
             case AVAssetExportSessionStatusFailed:
             {
                 NSLog (@"FAIL %@",_assetExport.error);
                 break;
             }
             case AVAssetExportSessionStatusCompleted: 
             {
                 break;
             }
             case AVAssetExportSessionStatusCancelled: 
             {
                 NSLog (@"CANCELED");
                 break;
             }
         }
         NSLog(@"Export Status %d-- %@", _assetExport.status, _assetExport.outputURL);
        }
     ];    
于 2012-04-27T19:17:23.327 に答える