iphone - 如何将 iPhone 摄像头拍摄的视频发送到服务器进行直播?

标签 iphone ios avfoundation http-live-streaming live-streaming

我有一些在线代码可以从 iPhone 的摄像头捕获视频,然后将其存储到视频文件中,并且运行良好。但我的目的不是将它保存在内存中,而是将它发送到服务器。我发现有一个名为 WOWZA 的免费媒体服务器允许流式传输,而且 Apple 还具有 (HSL) HTTP Live Streaming 功能,并且服务器希望视频为 h.264 格式的视频和 mp3 格式的音频。通过阅读有关 Apple HSL 的一些文档,我还了解到它在播放列表文件中为媒体文件的每个片段提供了不同的 url,然后通过浏览器在设备上以正确的顺序播放。我不确定如何获取手机摄像头记录的文件的一小部分以及如何将其转换为所需的格式。 以下是捕获视频的代码:

执行文件

#import "THCaptureViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "THPlayerViewController.h"

#define VIDEO_FILE @"test.mov"

@interface THCaptureViewController ()
@property (nonatomic, strong) AVCaptureSession *captureSession;
@property (nonatomic, strong) AVCaptureMovieFileOutput *captureOutput;
@property (nonatomic, weak) AVCaptureDeviceInput *activeVideoInput;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;
@end

@implementation THCaptureViewController

- (void)viewDidLoad 
{
[super viewDidLoad];

    #if TARGET_IPHONE_SIMULATOR
    self.simulatorView.hidden = NO;
        [self.view bringSubviewToFront:self.simulatorView];
    #else
    self.simulatorView.hidden = YES;
    [self.view sendSubviewToBack:self.simulatorView];
    #endif

// Hide the toggle button if device has less than 2 cameras. Does 3GS support iOS 6?
self.toggleCameraButton.hidden = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo] count] < 2;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), 
    ^{
    [self setUpCaptureSession];
});
}

#pragma mark - Configure Capture Session

- (void)setUpCaptureSession 
{
self.captureSession = [[AVCaptureSession alloc] init];


NSError *error;

// Set up hardware devices
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (videoDevice) {
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
    if (input) {
        [self.captureSession addInput:input];
        self.activeVideoInput = input;
    }
}
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
if (audioDevice) {
    AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error];
    if (audioInput) {
        [self.captureSession addInput:audioInput];
    }
}

//Create a VideoDataOutput and add it to the session
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[self.captureSession addOutput:output];

// Setup the still image file output
AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
[stillImageOutput setOutputSettings:@{AVVideoCodecKey : AVVideoCodecJPEG}];

if ([self.captureSession canAddOutput:stillImageOutput]) {
    [self.captureSession addOutput:stillImageOutput];
}

// Start running session so preview is available
[self.captureSession startRunning];

// Set up preview layer
dispatch_async(dispatch_get_main_queue(), ^{
    self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
    self.previewLayer.frame = self.previewView.bounds;
    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

    [[self.previewLayer connection] setVideoOrientation:[self currentVideoOrientation]];
    [self.previewView.layer addSublayer:self.previewLayer];
});

}

#pragma mark - Start Recording

- (IBAction)startRecording:(id)sender {

if ([sender isSelected]) {
    [sender setSelected:NO];
    [self.captureOutput stopRecording];

} else {
    [sender setSelected:YES];

    if (!self.captureOutput) {
        self.captureOutput = [[AVCaptureMovieFileOutput alloc] init];
        [self.captureSession addOutput:self.captureOutput];
    }

    // Delete the old movie file if it exists
    //[[NSFileManager defaultManager] removeItemAtURL:[self outputURL] error:nil];

    [self.captureSession startRunning];

    AVCaptureConnection *videoConnection = [self connectionWithMediaType:AVMediaTypeVideo fromConnections:self.captureOutput.connections];

    if ([videoConnection isVideoOrientationSupported]) {
        videoConnection.videoOrientation = [self currentVideoOrientation];
    }

    if ([videoConnection isVideoStabilizationSupported]) {
        videoConnection.enablesVideoStabilizationWhenAvailable = YES;
    }

    [self.captureOutput startRecordingToOutputFileURL:[self outputURL] recordingDelegate:self];
}

// Disable the toggle button if recording
self.toggleCameraButton.enabled = ![sender isSelected];
}

- (AVCaptureConnection *)connectionWithMediaType:(NSString *)mediaType fromConnections:(NSArray *)connections {
for (AVCaptureConnection *connection in connections) {
    for (AVCaptureInputPort *port in [connection inputPorts]) {
        if ([[port mediaType] isEqual:mediaType]) {
            return connection;
        }
    }
}
return nil;
}

#pragma mark - AVCaptureFileOutputRecordingDelegate

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error {
if (!error) {
    [self presentRecording];
} else {
    NSLog(@"Error: %@", [error localizedDescription]);
}
}

#pragma mark - Show Last Recording

- (void)presentRecording 
{
    NSString *tracksKey = @"tracks";
    AVAsset *asset = [AVURLAsset assetWithURL:[self outputURL]];
    [asset loadValuesAsynchronouslyForKeys:@[tracksKey] completionHandler:^{
    NSError *error;
            AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error];
            if (status == AVKeyValueStatusLoaded) {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
                            THPlayerViewController *controller = [mainStoryboard instantiateViewControllerWithIdentifier:@"THPlayerViewController"];
                            controller.title = @"Capture Recording";
                            controller.asset = asset;
                            [self presentViewController:controller animated:YES completion:nil];
                    });
            }
    }];
}

#pragma mark - Recoding Destination URL

- (NSURL *)outputURL 
{
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSLog(@"documents Directory: %@", documentsDirectory);
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:VIDEO_FILE];

    NSLog(@"output url: %@", filePath);
    return [NSURL fileURLWithPath:filePath];
}

@end

我找到了这个 link它显示了如何以帧为单位捕获视频。但我不确定以帧为单位捕获视频是否会帮助我将 h.264 格式的视频发送到服务器。这可以做到吗,如果可以,那么怎么做?

Here提出问题的人说(在问题下方的评论中)他能够成功地做到这一点,但他没有提到他是如何拍摄视频的。

请告诉我应使用哪种数据类型来获取捕获的视频的小片段,以及如何将捕获的数据转换为所需格式并将其发送到服务器。

最佳答案

您可以使用 live sdk。您必须设置 nginx 支持的流媒体服务器。 请点击此链接。我已经使用过它,它是非常有效的解决方案。 https://github.com/ltebean/Live

关于iphone - 如何将 iPhone 摄像头拍摄的视频发送到服务器进行直播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17402662/

相关文章:

iphone - 类型转换 UIView 警告

avfoundation - CoreAudio 和 AVFoundation 之间的性能

iphone - 如何为从一个 Controller 到另一个 Controller 的webview创建对象

ios - 从 nib 文件所有者加载 ViewController

ios - 如何为图像捕捉计时相机闪光灯?

ios - UITabBarController 委托(delegate)与 Storyboard

objective-c - shouldOptimizeForNetworkUse 实际上做了什么?

iphone - 更改 "sounder"的音频音高

iphone - UIImagePicker 收到名为 viewDidLoad 的内存警告并清除 UITextField

iphone - 使用自定义对象对 NSMutableArray 进行排序 'overwrites' 一些对象