iOS 视频流传输并随后存储在设备上

发布于 2024-11-14 12:10:10 字数 93 浏览 2 评论 0原文

到目前为止,我知道如何流式传输视频以及如何下载它然后流式传输它,但这里有一个棘手的地方:流式传输一次,将其存储在设备上,并在将来从设备上播放它。

这可能吗?

So far I know how to stream a video and how to download it and afterwards stream it, but here's the tricky bit: streaming it once, storing it on the device and in the future play it from the device.

Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

行至春深 2024-11-21 12:10:10

不太确定如何获取流,但请查看 AVAssetWriter、AVAssetWriterInput 和 AVAssetWriterPixelBufferAdaptor,一旦收到数据,您应该能够使用以下方法将数据附加到像素缓冲区适配器:

appendPixelBuffer:withPresentationTime:

不确定它是否适合您但通过一些摆弄,您应该能够调整您的输入以匹配此方法。有很多用于设置 writer 的示例代码

Not quite sure here how you get your stream but look in to the AVAssetWriter, AVAssetWriterInput and AVAssetWriterPixelBufferAdaptor and as soon as you receive data you should be able to append the data to the to the pixel buffer adaptor using:

appendPixelBuffer:withPresentationTime:

not sure it will work for you but with some fiddling you should be able to adapt your input to match this method. There are lots of example code for setting up the writer

神妖 2024-11-21 12:10:10

保存视频非常容易。执行与此类似的操作:

//Saving Movie
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];          
[archiver encodeObject:*MovieObject* forKey:@"MovieObjectDataKey"];
[archiver finishEncoding];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"MovieObjectDefaultsDataKey"];
[archiver release];
[data release];

//Retrieving movie 
NSData *savedMovieData = [[NSUserDefaults standardUserDefaults] objectForKey:@"MovieObjectDefaultsDataKey"];
if (savedMovieData != nil) {
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:savedMovieData];
    *MovieObject* = [[unarchiver decodeObjectForKey:@"MovieObjectDataKey"] retain];
    [unarchiver finishDecoding];
    [savedMovieData release];
    [unarchiver release];
} else {
    //Download Stream of Your Movie
}

您真正需要更改的唯一内容是 * MovieObject *,在每个步骤中更改一次。

It's quite easy to save the video. Do something similar to this:

//Saving Movie
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];          
[archiver encodeObject:*MovieObject* forKey:@"MovieObjectDataKey"];
[archiver finishEncoding];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"MovieObjectDefaultsDataKey"];
[archiver release];
[data release];

//Retrieving movie 
NSData *savedMovieData = [[NSUserDefaults standardUserDefaults] objectForKey:@"MovieObjectDefaultsDataKey"];
if (savedMovieData != nil) {
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:savedMovieData];
    *MovieObject* = [[unarchiver decodeObjectForKey:@"MovieObjectDataKey"] retain];
    [unarchiver finishDecoding];
    [savedMovieData release];
    [unarchiver release];
} else {
    //Download Stream of Your Movie
}

The only thing you really have to change there is * MovieObject *, once in each step.

孤千羽 2024-11-21 12:10:10

我知道你想要实现什么,我只有一个解决方法。我必须实现相同的行为,最终从服务器流式传输视频并在流式传输旁边下载它。下次用户尝试流式传输视频时,请确定它是否已下载到磁盘,否则再次流式传输。正常情况下视频可以正常下载并可以离线查看。

I know what you want to achieve, I only got a workaround. I had to implement the same behavior and ended up with streaming the video from the server and downloading it next to streaming. Next time the user tries to stream the video determine whether it was downloaded to disk, otherwise stream it again. In a normal case the video was downloaded properly and could be reviewed offline.

时光沙漏 2024-11-21 12:10:10
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:somePath];

fileURLWithPath

:isDirectory:

初始化并返回新创建的 NSURL 对象作为具有指定路径的文件 URL。

+ (id)fileURLWithPath:(NSString *)path isDirectory:(BOOL)isDir

参数

path

NSURL 对象将表示的路径。路径应该是有效的系统路径。如果路径以波形符开头,则必须首先使用 stringByExpandingTildeInPath 对其进行扩展。如果路径是相对路径,则将其视为相对于当前工作目录。
对此参数传递 nil 会产生异常。

isDir

一个布尔值,指定在解析相对路径组件时是否将路径视为目录路径。如果路径指示目录,则传递 YES,否则传递 NO。
返回值
使用路径初始化的 NSURL 对象。

可用性

适用于 iOS 2.0 及更高版本。

您无法同时进行流式传输和保存,尤其是对于大型视频文件,因为 Apple 文档表示您必须使用传输流进行 HTTP 实时流式传输。

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:somePath];

and

fileURLWithPath:isDirectory:

Initializes and returns a newly created NSURL object as a file URL with a specified path.

+ (id)fileURLWithPath:(NSString *)path isDirectory:(BOOL)isDir

Parameters

path

The path that the NSURL object will represent. path should be a valid system path. If path begins with a tilde, it must first be expanded with stringByExpandingTildeInPath. If path is a relative path, it is treated as being relative to the current working directory.
Passing nil for this parameter produces an exception.

isDir

A Boolean value that specifies whether path is treated as a directory path when resolving against relative path components. Pass YES if the path indicates a directory, NO otherwise.
Return Value
An NSURL object initialized with path.

Availability

Available in iOS 2.0 and later.

You can't stream it and save it at the same time, especially with large video files as the Apple doc sais that you must use a transport stream for HTTP Live Streaming.

梦冥 2024-11-21 12:10:10

ASIHttpRequest 可能会让您的生活更轻松。

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:@"video.m4v"]; // use [NSBundle mainBundle] to find a better place

从您的委托中处理此问题:

- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data;

在获取数据时使用 data 进行任何数据转码,并将其实时推送到您的 AVAssetWriter 或电影播放器​​层,无论您使用什么。完成后,资产仍应保存,以便您稍后获取。

ASIHttpRequest might make your life easier.

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:@"video.m4v"]; // use [NSBundle mainBundle] to find a better place

From your delegate, handle this:

- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data;

Do whatever data transcoding with data as you get it and push it off to your AVAssetWriter or movie player layer in real time, whatever you are using. When you're done, the asset should still be saved so you can get it later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文