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 に答える