2

AVFoundation フレームワークを使用して、ビデオを正しい方向に反転しようとしています。何らかの理由で、以下のコードは毎回空白のビデオを生成しています。

AVAssetExportSession に videoComposition 設定を適用しない場合、ビデオは通過します (フォト ライブラリで表示および再生可能) が、レイヤー命令が適用されないため反転されません。したがって、問題はコンポジションに渡されるレイヤー命令にあると想定しましたが、適切に設定されています。何が欠けているか、または表示されていませんか?

コードは次のとおりです。

    NSURL *assetURL = [NSURL fileURLWithPath:path];
    AVAsset *movieAsset = [AVAsset assetWithURL:assetURL];
    NSLog(@"Asset: %0.1f",CMTimeGetSeconds(movieAsset.duration));
    NSLog(@"Asset Preferred Transform %@", NSStringFromCGAffineTransform(movieAsset.preferredTransform));



    //Output Composition
    AVMutableComposition *outputComposition = [[AVMutableComposition alloc] init];
    AVMutableCompositionTrack *videoTrack = [outputComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                           preferredTrackID:kCMPersistentTrackID_Invalid];
    [videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, movieAsset.duration)
                        ofTrack:[[movieAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                         atTime:kCMTimeZero
                          error:nil];
    [videoTrack setPreferredTransform:CGAffineTransformMakeScale(1, -1)];

    AVMutableVideoCompositionLayerInstruction *videoLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
    [videoLayerInstruction setTransform:CGAffineTransformMakeScale(1, -1) atTime:kCMTimeZero];

    AVMutableVideoCompositionInstruction *videoInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    videoInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, movieAsset.duration);
    videoInstruction.layerInstructions = [NSArray arrayWithObjects:videoLayerInstruction, nil];


    AVMutableVideoComposition *outputVideoComposition = [AVMutableVideoComposition videoComposition];
    outputVideoComposition.instructions = [NSArray arrayWithObjects:videoInstruction, nil];
    outputVideoComposition.frameDuration = CMTimeMake(1, 30);
    outputVideoComposition.renderSize = CGSizeMake(480, 640);


    //Export
    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:outputComposition presetName:AVAssetExportPresetHighestQuality] ;
    exporter.videoComposition = outputVideoComposition;
    NSString *newPath = [documentsDirectory stringByAppendingPathComponent:[self.filePath stringByAppendingString:@"-fb.mov"]];
    [[NSFileManager defaultManager] removeItemAtPath:newPath error:nil];
    exporter.outputURL = [NSURL fileURLWithPath:newPath];
    exporter.outputFileType = AVFileTypeQuickTimeMovie;

    NSLog(@"Starting export");



    [exporter exportAsynchronouslyWithCompletionHandler:^(void){

        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Video exported");
            NSLog(@"%@", newPath);
            NSURL *newAssetURL = [NSURL fileURLWithPath:newPath];
            AVAsset *newMovieAsset = [AVAsset assetWithURL:newAssetURL];
            NSLog(@"New Asset %@",newMovieAsset);
            NSLog(@"New Asset Duration: %0.1f",CMTimeGetSeconds(newMovieAsset.duration));
            NSLog(@"New Asset Preferred Transform %@", NSStringFromCGAffineTransform(newMovieAsset.preferredTransform));


            UISaveVideoAtPathToSavedPhotosAlbum(newPath, nil, nil, nil);}}];
4

2 に答える 2

2

outputVideoComposition に指定しているレンダリング サイズが正しくありません。それはiphoneの画面に従っているはずです

于 2013-03-14T05:47:34.587 に答える
0

私は同じ問題を抱えていました、それはエクスポートフレームからビデオを回転させています。左上隅にある回転アンカーのもの。

修正するには、次のような別の変換を適用します。

CGAffineTransformMakeTranslation(exportingVideoWidth、exportingVideoHeight);

これがお役に立てば幸いです。ご不明な点がありましたらお知らせください。

于 2013-03-19T06:52:28.627 に答える