从Ustream或Qik上传来自iPhone的直播视频

0pc*_*pcl 40 iphone video streaming http live

如何直播视频从iPhone到服务器,如Ustream或Qik?我知道苹果有一种叫做Http Live Streaming的东西,但我发现的大多数资源都只谈到从服务器到iPhone的视频流.

Apple的Http Living Streaming是我应该使用的吗?或者是其他东西?谢谢.

jab*_*jab 46

据我所知,没有内置的方法可以做到这一点.正如您所说,HTTP Live Streaming用于下载到iPhone.

我这样做的方法是实现一个AVCaptureSession,它有一个带有回调的委托,该回调在每一帧上运行.该回调通过网络将每个帧发送到服务器,该服务器具有接收它的自定义设置.

以下是该流程:https://developer.apple.com/library/content/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html#//apple_ref/doc/uid/TP40010188-CH5-SW2

这里是一些代码:

// make input device
NSError *deviceError;
AVCaptureDevice *cameraDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *inputDevice = [AVCaptureDeviceInput deviceInputWithDevice:cameraDevice error:&deviceError];

// make output device
AVCaptureVideoDataOutput *outputDevice = [[AVCaptureVideoDataOutput alloc] init];
[outputDevice setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

// initialize capture session
AVCaptureSession *captureSession = [[[AVCaptureSession alloc] init] autorelease];
[captureSession addInput:inputDevice];
[captureSession addOutput:outputDevice];

// make preview layer and add so that camera's view is displayed on screen
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
previewLayer.frame = view.bounds;
[view.layer addSublayer:previewLayer];

// go!
[captureSession startRunning];
Run Code Online (Sandbox Code Playgroud)

然后输出设备的委托(这里,self)必须实现回调:

-(void) captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection
{
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer( sampleBuffer );
    CGSize imageSize = CVImageBufferGetEncodedSize( imageBuffer );
    // also in the 'mediaSpecific' dict of the sampleBuffer

   NSLog( @"frame captured at %.fx%.f", imageSize.width, imageSize.height );
}
Run Code Online (Sandbox Code Playgroud)

编辑/ UPDATE

有些人已经问过如何在不将帧逐个发送到服务器的情况下执行此操作.答案很复杂......

基本上,在didOutputSampleBuffer上面的函数中,您将样本添加到AVAssetWriter.实际上我有三个资产编写者一次活动 - 过去,现在和未来 - 在不同的线程上管理.

过去的作者正在关闭电影文件并上传它.当前作者正在从相机接收样本缓冲区.未来的作家正在打开一个新的电影文件并为数据做准备.每隔5秒,我设置past=current; current=future并重新启动序列.

然后,它将视频以5秒的块上传到服务器.您可以根据需要将视频拼接在一起ffmpeg,或将它们转码为MPEG-2传输流以进行HTTP直播.视频数据本身由资产编写者进行H.264编码,因此转码只会改变文件的标题格式.

  • 您可以分享/协助上传视频机制的代码吗?有什么提示吗? (3认同)
  • 那么,为了加速数据传输,必须压缩视频.因此,有两种可能:1)动态压缩,需要编解码器库和大量CPU; 或者2)使用iPhone的内置硬件加速mp4压缩 - 但这只支持流式传输到磁盘.我正在流式传输到磁盘,每隔几秒更改一次目标文件并上传完成的文件.即使没有我发现的几个Apple漏洞的变通方法,它也非常棘手和复杂.您不能轻易地将单个文件用作管道,因为在文件关闭之前不会写入帧索引. (2认同)
  • @kashifmehmood 我认为你在谈论下载(即观看视频)?这个问题是关于将视频从 iPhone 传输到服务器。视频到达服务器后如何处理是一个单独的主题。 (2认同)

Yor*_*xxx -4

我不确定您是否可以使用 HTTP Live Streaming 来做到这一点。HTTP Live Streaming 以 10 秒(大约)长度对视频进行分段,并使用这些分段创建播放列表。因此,如果您希望 iPhone 成为 HTTP Live Streaming 的流服务器端,您将必须找到一种方法来分割视频文件并创建播放列表。

如何做到这一点超出了我的知识范围。对不起。