7

Ich habe etwas Code online, der Video von der Kamera des iPhone erfasst und speichert es dann in eine Videodatei und es funktioniert gut. Aber meine Absicht ist es nicht, es in der Erinnerung zu speichern, sondern es zu einem Server zu senden. Ich habe herausgefunden, dass es einen kostenlosen Medienserver namens WOWZA gibt, der Streaming erlaubt und auch Apple hat (HSL) HTTP Live Streaming Feature und dass die Server erwarten, dass das Video im h.264 Format für Video und im mp3 Format für Audio ist. Durch das Lesen einiger Dokumente über Apple HSL habe ich auch erfahren, dass es in der Playlist-Datei für jedes Segment der Mediendatei eine andere URL gibt, die dann in der richtigen Reihenfolge auf einem Gerät über den Browser abgespielt wird. Ich bin mir nicht sicher, wie man kleine Segmente der Datei, die von der Kamera des Telefons aufgezeichnet wird, erhält und auch, wie man sie in das erforderliche Format konvertiert. Im Folgenden finden Sie den Code für die Videoaufnahme:Wie kann ich das von der Kamera des iPhones aufgenommene Video an einen Server für Live-Streaming senden?

Implementierungsdatei

#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 

ich diesen link gefunden, die zeigt, wie das Video in Frames zu erfassen. Aber ich bin mir nicht sicher, ob das Aufnehmen des Videos in Frames mir helfen wird, das Video im h.264-Format an den Server zu senden. Kann das gemacht werden, wenn ja wie?

Here Die Person, die die Frage gestellt hat, sagt (in den Kommentaren unten die Frage), dass er es erfolgreich machen konnte, aber er hat nicht erwähnt, wie er das Video aufgenommen hat.

Bitte teilen Sie mir mit, welcher Datentyp verwendet werden soll, um kleine Segmente des aufgenommenen Videos zu erhalten und wie die erfassten Daten im gewünschten Format konvertiert und an den Server gesendet werden.

+0

Bitte beachten Sie diese URL wird es hilfreich für Sie. http://stackoverflow.com/questions/15518925/how-to-save-video-in-documents-folder-then-upload-to-server – saravanan

+0

ich denke, er für Live-Streaming wird nicht gefragt, Speichern Video in Dokumente Verzeichnis und dann an den Server senden. – Leena

+0

Ja. Ich möchte mein Video live streamen. –

Antwort

0

Sie können Live-SDK verwenden. Sie müssen Nginx powered Streaming-Server einrichten. Bitte folgen Sie diesem Link. Ich habe es verwendet und es ist sehr effiziente Lösung. https://github.com/ltebean/Live