16

我正在尝试录制视频(不显示相机)并保存。但是正在保存的视频没有以正确的方向保存。我尝试强制 UIViewController 成为某个方向,但这没有帮助。所有视频都是纵向录制的。我的代码如下:

session = [[AVCaptureSession alloc] init];
[session beginConfiguration];
session.sessionPreset = AVCaptureSessionPresetHigh;


AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *cam in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo])
{
    if (cam.position == AVCaptureDevicePositionFront)
        device = cam;
}

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

AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput * audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];


NSString *outputpathofmovie = [[documentsDirectoryPath stringByAppendingPathComponent:@"RecordedVideo"] stringByAppendingString:@".mp4"];
outputURL = [[NSURL alloc] initFileURLWithPath:outputpathofmovie];

[self deleteTempVideos];

[session addInput:input];
[session addInput:audioInput];
[session commitConfiguration];
[session startRunning];

movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
NSLog(@"%@", movieFileOutput.connections);

AVCaptureConnection *videoConnection = nil;

for ( AVCaptureConnection *connection in [movieFileOutput connections] )
{
    NSLog(@"%@", connection);
    for ( AVCaptureInputPort *port in [connection inputPorts] )
    {
        NSLog(@"%@", port);
        if ( [[port mediaType] isEqual:AVMediaTypeVideo] )
        {
            videoConnection = connection;
        }
    }
}

if([videoConnection isVideoOrientationSupported]) // **Here it is, its always false**
{
    [videoConnection setVideoOrientation:[[UIDevice currentDevice] orientation]];
}

NSLog(@"%@", videoConnection);

[session addOutput:movieFileOutput];
[movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];
4

4 回答 4

21

事实证明,在将连接的方向添加到会话后,您必须将其添加到 AVCaptureMovieFileOutput。

session = [[AVCaptureSession alloc] init];
[session beginConfiguration];
session.sessionPreset = AVCaptureSessionPresetHigh;


AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *cam in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo])
{
    if (cam.position == AVCaptureDevicePositionFront)
        device = cam;
}

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

AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput * audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];


NSString *outputpathofmovie = [[documentsDirectoryPath stringByAppendingPathComponent:@"RecordedVideo"] stringByAppendingString:@".mp4"];
outputURL = [[NSURL alloc] initFileURLWithPath:outputpathofmovie];

[self deleteTempVideos];

[session addInput:input];
[session addInput:audioInput];
[session commitConfiguration];
[session startRunning];

movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
[session addOutput:movieFileOutput];

AVCaptureConnection *videoConnection = nil;

for ( AVCaptureConnection *connection in [movieFileOutput connections] )
{
    NSLog(@"%@", connection);
    for ( AVCaptureInputPort *port in [connection inputPorts] )
    {
        NSLog(@"%@", port);
        if ( [[port mediaType] isEqual:AVMediaTypeVideo] )
        {
            videoConnection = connection;
        }
    }
}

if([videoConnection isVideoOrientationSupported]) // **Here it is, its always false**
{
    [videoConnection setVideoOrientation:[[UIDevice currentDevice] orientation]];
}

[movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];
于 2013-03-22T22:47:05.333 回答
10

**

尝试在开始录制之前更改其方向。

**

    if let videoConnection = fileOutput.connection(with: .video) {

    let newOrientation: AVCaptureVideoOrientation
    switch UIDevice.current.orientation {
    case .portrait:
        newOrientation = .portrait
    case .portraitUpsideDown:
        newOrientation = .portraitUpsideDown
    case .landscapeLeft:
        newOrientation = .landscapeRight
    case .landscapeRight:
        newOrientation = .landscapeLeft
    default :
        newOrientation = .portrait
    }
    videoConnection.videoOrientation = newOrientation
    self.fileOutput.startRecording(to: URL(fileURLWithPath: filePath), recordingDelegate: self)
}

其中,

var fileOutput : AVCaptureMovieFileOutput!
于 2016-05-26T10:45:53.520 回答
2

尝试这样的事情:

#import <AVFoundation/AVFoundation.h>

AVCaptureConnection *captureConnection = <#A capture connection#>;
if ([captureConnection isVideoOrientationSupported])
{
    AVCaptureVideoOrientation orientation = AVCaptureVideoOrientationLandscapeLeft;
    [captureConnection setVideoOrientation:orientation];
}

http://developer.apple.com/library/ios/#qa/qa1744/_index.html#//apple_ref/doc/uid/DTS40011134

于 2013-03-22T22:02:25.840 回答
0

谢谢 Destiny Dawn,以防有人在寻找 Xamarin ios 的代码:

AVCaptureConnection videoConnection = null;

foreach(AVCaptureConnection connection in _videoOutput.Connections)
{
    foreach ( AVCaptureInputPort port in connection.InputPorts)
    {
        if (port.MediaType == AVMediaType.Video)
        {
            videoConnection = connection;
            break;
        }
    }
}

if (videoConnection != null) {
    if (videoConnection.SupportsVideoOrientation) {
        videoConnection.VideoOrientation = AVCaptureVideoOrientation.LandscapeRight;
    }
}
于 2015-05-22T11:07:35.333 回答