1

我正在录制视频并将其保存在应用程序的文档文件夹中。我想翻转视频帧并将其保存,以便视频像镜像视频一样以翻转模式播放。我不知道如何实现这一点。我试图翻转视频,但它保存为空白视频。这是翻转视频的代码

NSURL *assetURL = self.outPutUrl;
    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;
    NSArray *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [filePath objectAtIndex:0];
    NSString *newPath =  [documentsDirectory stringByAppendingPathComponent:
                             [NSString stringWithFormat:@"FinalVideo-%d.mov",arc4random() % 1000]];
    [[NSFileManager defaultManager] removeItemAtPath:newPath error:nil];
    exporter.outputURL = [NSURL fileURLWithPath:newPath];
    exporter.outputFileType = AVFileTypeQuickTimeMovie;
    NSLog(@"Starting export%@",exporter.outputURL);
    [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

1 回答 1

-1

检查这个链接..我想你想反转视频剪辑..它对我有用..

于 2013-09-11T06:08:15.803 回答