Как отправить потоковое видео с устройства iOS на сервер?

Я должен отправлять видео в реальном времени с iPhone на сервер. Я создаю сеанс захвата и использую AVCaptureMovieFileOutput.

NSError *error = nil;

captureSession = [[AVCaptureSession alloc] init];
// find, attach devices
AVCaptureDevice *muxedDevice = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeMuxed];
if (muxedDevice) {
    NSLog (@"got muxedDevice");
    AVCaptureDeviceInput *muxedInput = [AVCaptureDeviceInput deviceInputWithDevice:muxedDevice
                                                                             error:&error];
    if (muxedInput) {
        [captureSession addInput:muxedInput];
    }
} else {
    AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo];
    if (videoDevice) {
        NSLog (@"got videoDevice");
        AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice
                                                                                 error:&error];
        if (videoInput) {
            [captureSession addInput: videoInput];
        }
    }
    AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeAudio];
    if (audioDevice) {
        NSLog (@"got audioDevice");
        AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice
                                                                                 error:&error];
        if (audioInput) {
            [captureSession addInput: audioInput];
        }
    }
}

// create a preview layer from the session and add it to UI
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
previewLayer.frame = view.layer.bounds;
previewLayer.videoGravity = AVLayerVideoGravityResizeAspect;
previewLayer.orientation = AVCaptureVideoOrientationPortrait;
[view.layer addSublayer:previewLayer];

// create capture file output

captureMovieOutput = [[AVCaptureMovieFileOutput alloc] init];
if (! captureMovieURL) {
    captureMoviePath = [[self getMoviePathWithName:MOVIE_FILE_NAME] retain];
    captureMovieURL = [[NSURL alloc] initFileURLWithPath:captureMoviePath];
}
NSLog (@"recording to %@", captureMovieURL);
[captureSession addOutput:captureMovieOutput];

Я использую AVAssetExportSession для получения видео продолжительностью 10 секунд.

     AVURLAsset *asset = [AVURLAsset URLAssetWithURL:captureMovieURL options:[NSDictionary  dictionaryWithObject:@"YES" forKey:AVURLAssetPreferPreciseDurationAndTimingKey]];

AVMutableComposition *composition = [AVMutableComposition composition];

CMTime endTime;
CMTime duration = CMTimeMake(6000, 600);
if (asset.duration.value - startFragment.value < 6000)
{
    endTime = asset.duration;
}
else
{
    endTime = CMTimeMake(startFragment.value + 6000, 600);        
}
CMTimeRange editRange = CMTimeRangeMake(startFragment, duration);
startFragment = CMTimeMake(endTime.value, 600);
  NSError *editError = nil;
// and add into your composition 

[composition insertTimeRange:editRange ofAsset:asset atTime:composition.duration error:&editError];

AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:composition presetName:AVAssetExportPresetPassthrough]; exportSession.shouldOptimizeForNetworkUse = YES; NSString *name = [NSString stringWithFormat:MOVUE_SEGMENT_NAME, countMovies]; NSString *path = [NSString stringWithFormat:@"file://localhost%@", [self getMoviePathWithName:name]]; NSURL *url = [NSURL URLWithString:path]; NSLog(@"urlsegment = %@", url); exportSession.outputFileType = AVFileTypeMPEG4; exportSession.outputURL = url; [exportSession exportAsynchronouslyWithCompletionHandler:^{ if (AVAssetExportSessionStatusCompleted == exportSession.status) { countMovies++; NSLog(@"AVAssetExportSessionStatusCompleted"); } else if (AVAssetExportSessionStatusFailed == exportSession.status) { NSLog(@"AVAssetExportSessionStatusFailed: %@", [exportSession.error localizedDescription]); } else { NSLog(@"Export Session Status: %d", exportSession.status); } }];

Я отправляю видео на сервер, если статус сеанса экспорта завершен. Но это очень медленно. Чтобы получить фильм длительностью 10 секунд и затем отправить его на сервер необходимо 15 секунд. Если размер фильма меньше 10 секунд, то ничего не меняется. Как я могу решить эту проблему? Как лучше всего это сделать? Как я могу решить эту проблему? Что лучше использовать для потокового видео на сервере?


person Forder    schedule 22.02.2012    source источник


Ответы (1)


использование ffmpeg для кодирования метаданных может быть лучше, чем AVAssetExportSession. Но кодирование ffmpeg намного сложнее, чем AVAssetExportSession;

person coolcloud    schedule 10.04.2012