1

我是 iOS 新手。

我有一个用于录制视频并在 iPad 上播放的应用程序。

现在我必须在视图中打开相机。所以我为此使用了avcapturesession。

现在通过我的编码,我可以录制和播放视频,但录制的视频处于旋转模式。

我使用 LandscapeRight 进行录制。

这是我的编码:

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
                    captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

                    captureVideoPreviewLayer.orientation=AVCaptureVideoOrientationLandscapeRight;
                    captureVideoPreviewLayer.frame = vwvideo.bounds;

                    [vwvideo.layer addSublayer:captureVideoPreviewLayer];

                    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

                    NSError *error = nil;
                    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
                    if (!input) {
                        // Handle the error appropriately.
                        NSLog(@"ERROR: trying to open camera: %@", error);
                    }

                    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

                    AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];

                    NSString *documentsDirectory = [paths objectAtIndex:0];

                    NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
                    [dateFormat setDateFormat:@"yyyy-MM-dd HH.mm.SS"];
                    NSDate *now = [[[NSDate alloc] init] autorelease];
                    theDate = [dateFormat stringFromDate:now];

                    NSString *tempPath = [NSString stringWithFormat:@"%@/%@.mp4",documentsDirectory,theDate];
                    [tempPath retain];
                    NSLog(@"Path::%@",tempPath);

                    NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:tempPath];
                                        [session addInput:input];
                    [session addOutput:movieFileOutput];
                    [session commitConfiguration];
                    [session startRunning];

                    [movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];
4

1 回答 1

6

http://www.raywenderlich.com/13418/how-to-play-record-edit-videos-in-ios/videoplayrecord

如果您仔细阅读示例项目的代码,该教程中给出的内容很容易通过以下代码片段将旋转的视频修复到正确的方向。(RecordVideo.m 文件)

        //FIXING ORIENTATION//
    AVMutableVideoCompositionLayerInstruction *FirstlayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:firstTrack];
    AVAssetTrack *FirstAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    UIImageOrientation FirstAssetOrientation_  = UIImageOrientationUp;
    BOOL  isFirstAssetPortrait_  = NO;
    CGAffineTransform firstTransform = FirstAssetTrack.preferredTransform;
    if(firstTransform.a == 0 && firstTransform.b == 1.0 && firstTransform.c == -1.0 && firstTransform.d == 0)  {FirstAssetOrientation_= UIImageOrientationRight; isFirstAssetPortrait_ = YES;}
    if(firstTransform.a == 0 && firstTransform.b == -1.0 && firstTransform.c == 1.0 && firstTransform.d == 0)  {FirstAssetOrientation_ =  UIImageOrientationLeft; isFirstAssetPortrait_ = YES;}
    if(firstTransform.a == 1.0 && firstTransform.b == 0 && firstTransform.c == 0 && firstTransform.d == 1.0)   {FirstAssetOrientation_ =  UIImageOrientationUp;}
    if(firstTransform.a == -1.0 && firstTransform.b == 0 && firstTransform.c == 0 && firstTransform.d == -1.0) {FirstAssetOrientation_ = UIImageOrientationDown;}
    CGFloat FirstAssetScaleToFitRatio = 1.0;
    if(isFirstAssetPortrait_){
        FirstAssetScaleToFitRatio = 1.0;
        CGAffineTransform FirstAssetScaleFactor = CGAffineTransformMakeScale(FirstAssetScaleToFitRatio,FirstAssetScaleToFitRatio);
        [FirstlayerInstruction setTransform:CGAffineTransformConcat(FirstAssetTrack.preferredTransform, FirstAssetScaleFactor) atTime:kCMTimeZero];
    }else{
        CGAffineTransform FirstAssetScaleFactor = CGAffineTransformMakeScale(FirstAssetScaleToFitRatio,FirstAssetScaleToFitRatio);
        [FirstlayerInstruction setTransform:CGAffineTransformConcat(CGAffineTransformConcat(FirstAssetTrack.preferredTransform, FirstAssetScaleFactor),CGAffineTransformMakeTranslation(0, 160)) atTime:kCMTimeZero];
    }

    [FirstlayerInstruction setOpacity:0.0 atTime:videoAsset.duration];

    AVMutableVideoCompositionLayerInstruction *SecondlayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:AudioTrack];


    MainInstruction.layerInstructions = [NSArray arrayWithObjects:FirstlayerInstruction,nil];;

    AVMutableVideoComposition *MainCompositionInst = [AVMutableVideoComposition videoComposition];
    MainCompositionInst.instructions = [NSArray arrayWithObject:MainInstruction];
    MainCompositionInst.frameDuration = CMTimeMake(1, 30);
    MainCompositionInst.renderSize = CGSizeMake(360.0, 480.0);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"orientationFixedVideo-%d.mov",arc4random() % 1000]];

    NSURL *url = [NSURL fileURLWithPath:myPathDocs];

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
    exporter.outputURL=url;
    exporter.outputFileType = AVFileTypeQuickTimeMovie;
    exporter.videoComposition = MainCompositionInst;
    exporter.shouldOptimizeForNetworkUse = YES;
    [exporter exportAsynchronouslyWithCompletionHandler:^
     {
         dispatch_async(dispatch_get_main_queue(), ^{
             [self exportDidFinish:exporter];
         });
     }];
于 2012-11-16T10:20:17.260 回答