Mam pewien kod online, który przechwytuje wideo z aparatu iPhone, a następnie zapisuje go do pliku wideo i działa dobrze. Ale moim celem nie jest zapisanie go w pamięci, ale wysłanie go na serwer. Dowiedziałem się, że istnieje wolny serwer multimedialny o nazwie WOWZA, który umożliwia przesyłanie strumieniowe, a także ma funkcję HSL (HTTP Live Streaming) i że serwery oczekują, że wideo będzie w formacie h.264 dla wideo i mp3 dla audio. Czytając niektóre dokumenty dotyczące Apple HSL, dowiedziałem się również, że daje inny adres URL w pliku listy odtwarzania dla każdego segmentu pliku multimedialnego, który jest następnie odtwarzany we właściwej kolejności na urządzeniu przez przeglądarkę. Nie wiem, jak uzyskać małe fragmenty pliku rejestrowane przez aparat telefonu, a także przekonwertować go na wymagany format. Poniżej znajduje się kod do przechwytywania wideo:Jak wysłać film wideo z kamery iPhone'a na serwer w celu przesyłania strumieniowego na żywo?
Realizacja pliku
#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
Znalazłem link który pokazuje w jaki sposób przechwytywania wideo w ramkach. Ale nie jestem pewien, czy przechwytywanie wideo w ramkach pomoże mi przesłać wideo w formacie h.264 na serwer. Czy można to zrobić, jeśli tak, to w jaki sposób?
Here osoba, która zadała pytanie, mówi (w komentarzach poniżej pytania), że był w stanie to zrobić z powodzeniem, ale nie wspomniał o tym, w jaki sposób zdobył film.
Proszę mi powiedzieć, jaki typ danych powinien zostać użyty do uzyskania małych fragmentów wideo, a także jak przekonwertować przechwycone dane w wymaganym formacie i wysłać je na serwer.
Proszę odnieść się do tego adresu URL, będzie to pomocne. http://stackoverflow.com/questions/15518925/how-to-save-video-in-documents-folder-then-upload-to-server – saravanan
Chyba prosi o transmisji na żywo nie oszczędzając wideo w katalogu dokumentów, a następnie wysyłanie na serwer. – Leena
Tak. Chcę przesłać strumieniowo wideo na żywo. –