无法导出AVPlayerItem [英] Unable to export AVPlayerItem

查看:170
本文介绍了无法导出AVPlayerItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AVQueuePlayer播放我们应用中的视频列表.我正在尝试缓存AVQueuePlayer播放的视频,以便不必每次都下载视频.

I'm using AVQueuePlayer to do playback of a list of videos in our app. I'm trying to cache the videos that are played by AVQueuePlayer so that the video doesn't have to be downloaded each time.

因此,在视频播放完毕后,我尝试将AVPlayerItem保存到磁盘,以便下次使用本地URL对其进行初始化.

So, after the video finishes playing, I attempt to save the AVPlayerItem into disk so next time it's initialized with a local URL.

我试图通过两种方法来实现这一目标:

I've tried to achieve this through two approaches:

  1. 使用AVAssetExportSession
  2. 使用AVAssetReader和AVAssetWriter.

1)AVAssetExportSession方法

这种方法是这样的:

1) AVAssetExportSession approach

This approach works like this:

  1. AVPlayerItem使用AVPlayerItemDidPlayToEndTimeNotification完成播放时进行观察.
  2. 收到上述通知(视频已播放完毕,因此已完全下载),请使用AVAssetExportSession将该视频导出到磁盘中的文件中.
  1. Observe when an AVPlayerItem finishes playing using AVPlayerItemDidPlayToEndTimeNotification.
  2. When the above notification is received (a video finished playing, so it's fully downloaded), use AVAssetExportSession to export that video into a file in disk.

代码:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

然后

- (void)videoDidFinishPlaying:(NSNotification*)note { AVPlayerItem *itemToSave = [note object]; AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:itemToSave.asset presetName:AVAssetExportPresetHighestQuality]; exportSession.outputFileType = AVFileTypeMPEG4; exportSession.outputURL = [NSURL fileURLWithPath:@"/path/to/Documents/video.mp4"]; [exportSession exportAsynchronouslyWithCompletionHandler:^{ switch(exportSession.status){ case AVAssetExportSessionStatusExporting: NSLog(@"Exporting..."); break; case AVAssetExportSessionStatusCompleted: NSLog(@"Export completed, wohooo!!"); break; case AVAssetExportSessionStatusWaiting: NSLog(@"Waiting..."); break; case AVAssetExportSessionStatusFailed: NSLog(@"Failed with error: %@", exportSession.error); break; } }

- (void)videoDidFinishPlaying:(NSNotification*)note { AVPlayerItem *itemToSave = [note object]; AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:itemToSave.asset presetName:AVAssetExportPresetHighestQuality]; exportSession.outputFileType = AVFileTypeMPEG4; exportSession.outputURL = [NSURL fileURLWithPath:@"/path/to/Documents/video.mp4"]; [exportSession exportAsynchronouslyWithCompletionHandler:^{ switch(exportSession.status){ case AVAssetExportSessionStatusExporting: NSLog(@"Exporting..."); break; case AVAssetExportSessionStatusCompleted: NSLog(@"Export completed, wohooo!!"); break; case AVAssetExportSessionStatusWaiting: NSLog(@"Waiting..."); break; case AVAssetExportSessionStatusFailed: NSLog(@"Failed with error: %@", exportSession.error); break; } }

该代码在控制台中的结果是:

The result in console of that code is:

Failed with error: Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x98916a0 {NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x99ddd10 "The operation couldn’t be completed. (OSStatus error -12109.)", NSLocalizedFailureReason=An unknown error occurred (-12109)}

2)AVAssetReader,AVAssetWriter方法

代码:

- (void)savePlayerItem:(AVPlayerItem*)item { NSError *assetReaderError = nil; AVAssetReader *assetReader = [[AVAssetReader alloc] initWithAsset:assetToCache error:&assetReaderError]; //(algorithm continues) }

- (void)savePlayerItem:(AVPlayerItem*)item { NSError *assetReaderError = nil; AVAssetReader *assetReader = [[AVAssetReader alloc] initWithAsset:assetToCache error:&assetReaderError]; //(algorithm continues) }

当尝试使用以下信息分配/初始化AVAssetReader时,该代码引发异常:

That code throws an exception when trying to alloc/init the AVAssetReader with the following information:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVAssetReader initWithAsset:error:] Cannot initialize an instance of AVAssetReader with an asset at non-local URL 'https://someserver.com/video1.mp4'' ***

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVAssetReader initWithAsset:error:] Cannot initialize an instance of AVAssetReader with an asset at non-local URL 'https://someserver.com/video1.mp4'' ***

非常感谢您的帮助.

推荐答案

我最终设法做到的是创建通常由AVPlayer> AVPlayerItem> AVURLAsset组成的数组.但同时,在AVPlayer从远程URL完全加载视频之前,立即创建AVExportSession.

How I ended up manage to do this is to create the usually array of AVPlayer > AVPlayerItem > AVURLAsset. But also at the same time create an AVExportSession right away before AVPlayer have completely loaded the video from the remote URL.

当AVPlayer尝试预缓冲URL时,AVExportSession实际上将缓慢导出.这与用户是否开始播放AVPlayer无关.但是,只有预先缓冲了整个URL之后,输出文件才能真正完成.届时AVExportSession将随着完成情况回调.这也完全独立于用户是否正在播放视频或是否已经播放完视频.

The AVExportSession actually will export slowly as the AVPlayer tries to pre-buffer the URL. This is irregardless of whether the user starts playing the AVPlayer or not. However the output file will not actually complete until the entire URL is pre-buffered. At that point AVExportSession will callback with the completion. This is also completely independent of whether the user is playing, or have completed playing the video or not.

因此,通过尽早创建AVPlayer/AVExportSession组合,它成为了我的预缓冲机制

As such by creating the AVPlayer/AVExportSession combo early, it became my pre-buffer mechanism

-更新2018-01-10-

-- Update 2018-01-10 --

想要在部署了上述机制之后添加它.我们遇到了1个重要警告,我认为值得一提.

Want to add that after having deployed the above mechanism. We have encountered 1 big caveat that I think deserves some mention.

内存中不能有太多视频管道(已连接AVPlayerItem/AVAsset的AVPlayer). AVFoundation将拒绝播放.因此,请使用上述机制进行预缓冲.但是将视频下载到文件后,如果用户不观看视频,请取消分配AVPlayer/AVAsset.当用户决定再次播放视频时,这次重新分配AVPlayer,这次AVURLAsset指向视频的本地缓冲副本.

You cannot have too many video pipelines in memory (AVPlayer with AVPlayerItem/AVAsset hooked up). AVFoundation will refusing to play. So do use the above mechanism to pre-buffer. But once the video is downloaded to a file, if the user is not viewing the video, dealloc the AVPlayer/AVAsset. Reallocate an AVPlayer when the user decides to play the video again, this time with the AVURLAsset pointed to your local buffered copy of the video.

这篇关于无法导出AVPlayerItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆