2013-07-01 12 views
7

iPhoneのカメラからビデオをキャプチャしてビデオファイルに保存するコードをオンラインで手に入れました。うまくいきます。しかし私の目的は、それを記憶に保存するのではなく、それをサーバーに送ることです。私はストリーミングを可能にするWOWZAという名前のフリーメディアサーバーと、Appleが(HSL)HTTPライブストリーミング機能を備えており、サーバーはビデオをh.264形式で、オーディオをmp3形式にすると予想しています。 Apple HSLに関するドキュメントの一部を読んだところ、メディアファイルの各セグメントのプレイリストファイルに異なるURLが与えられ、ブラウザを介してデバイス上で正しい順序で再生されることもわかりました。私は電話のカメラで記録されたファイルの小さなセグメントを取得する方法と、それを必要な形式に変換する方法もわかりません。続き がビデオをキャプチャするためのコードです:ライブストリーミングのためにiPhoneのカメラからキャプチャしたビデオをサーバーに送る方法は?

実装ファイル

#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質問をした人は、彼はそれを成功裏に実行できたと答えていますが、彼はビデオをどのようにキャプチャしたのかは述べていません。

キャプチャしたビデオの小部分を取得するために使用するデータの種類と、取得したデータを必要な形式で変換してサーバーに送信する方法を教えてください。

+0

このURLを参考にしてください。 http://stackoverflow.com/questions/15518925/how-to-save-video-in-documents-folder-then-upload-to-server – saravanan

+0

私は彼が保存していないライブストリーミングを求めていると思いますドキュメントのディレクトリにビデオを保存し、サーバーに送信します。 – Leena

+0

はい。私はビデオをライブストリーミングしたい。 –

答えて

0

live sdkを使用できます。nginx powered streaming serverをセットアップする必要があります。 このリンクに従ってください。私はそれを使用しており、非常に効率的なソリューションです。 https://github.com/ltebean/Live

関連する問題