在应用程序中将 Youtube 视频保存到 iPhone

发布于 2024-10-18 11:23:42 字数 259 浏览 9 评论 0原文

在应用程序中播放 Youtube 视频非常简单,并且有详细的文档记录。

这样做有两个问题:

  1. 关闭 Youtube 播放器后,如果用户想再次播放,则必须再次等待在线流媒体
  2. 无法离线播放(在家加载视频以便在路上观看)

有人有代码可以:

  1. 下载YouTube 视频到文档文件夹并显示下载进度
  2. 通过从文档文件夹加载文件来播放下载的视频(意味着即使未连接到互联网)

Playing Youtube video in the app is easy and well documented around.

There are two problems with that:

  1. after closing Youtube player, if user wants to play it again it has to wait for online streaming again
  2. can't play offline (load video at home to watch on the road)

Does anyone have code to:

  1. download Youtube video to documents folder and show progress of download
  2. play downloaded video by loading file from documents folder (meaning even when not connected to the internet)

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

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

发布评论

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

评论(6

时光清浅 2024-10-25 11:23:42

要从 YouTube 下载视频:

  1. 通过 YouTube API 或任何其他方法获取要下载的 URL。
  2. 创建一个 NSOutputStreamNSFileHandle 在临时文件(在 NSTemporaryDirectory() 中或文档目录中的临时命名文件中)打开。
  3. 设置进度条以及您需要执行的任何其他操作。
  4. 分配并启动 NSURLConnection 以从 URL 获取文件。当然,不要使用 sendSynchronousRequest:returningResponse:error:
  5. connection:didReceiveResponse:委托方法中,读出要下载的数据长度,以便正确更新进度条。
  6. connection:didReceiveData: 委托方法中,将数据写入输出流/文件句柄并根据需要更新进度条。
  7. connectionDidFinishLoading:connection:didFailWithError: 中,关闭输出流/文件句柄并根据需要重命名或删除临时文件。

要播放它,只需使用 NSURL 的 fileURLWithPath: 创建一个指向 Documents 目录中本地文件的 URL,然后像播放任何远程视频一样播放它。

To download the video from YouTube:

  1. Get the URL to download from, via the YouTube API or whatever other method.
  2. Create an NSOutputStream or NSFileHandle opened on a temporary file (in NSTemporaryDirectory() or a temp-named file in your Documents directory).
  3. Set up your progress bar and whatever else you need to do.
  4. Allocate and start an NSURLConnection to fetch the file from the URL. Do not use sendSynchronousRequest:returningResponse:error:, of course.
  5. In the connection:didReceiveResponse: delegate method, read out the length of data to be downloaded for proper updating of the progress bar.
  6. In the connection:didReceiveData: delegate method, write the data to the output stream/file handle and update the progress bar as necessary.
  7. In connectionDidFinishLoading: or connection:didFailWithError:, close the output stream/file handle and rename or delete the temporary file as appropriate.

To play it back, just use NSURL's fileURLWithPath: to create a URL pointing to the local file in the Documents directory and play it as you would any remote video.

忆悲凉 2024-10-25 11:23:42

我使用过此项目中的类:https://github.com/larcus94/LBYouTubeView
它对我来说效果很好。
我可以下载 youtube 视频。

我使用了这段代码:

LBYouTubeExtractor *extractor = [[[LBYouTubeExtractor alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:(@"http://www.youtube.com/watch?v=%@"), self.videoID ]] quality:LBYouTubeVideoQualityLarge] autorelease];
[extractor extractVideoURLWithCompletionBlock:^(NSURL *videoURL, NSError *error) {
    if(!error) {
        NSLog(@"Did extract video URL using completion block: %@", videoURL);

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSData *data = [NSData dataWithContentsOfURL: videoURL];
            NSString *pathToDocs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            NSString *filename = [NSString stringWithFormat:(@"video_%@.mp4"), self.videoID ];
            [data writeToFile:[pathTODocs stringByAppendingPathComponent:filename] atomically:YES];
            NSLog(@"File %@ successfully saved", filename);
        });
    } else {
        NSLog(@"Failed extracting video URL using block due to error:%@", error);
    }
}];

您可以使用上面帖子中描述的技术显示下载进度。

Ive used classes from this project: https://github.com/larcus94/LBYouTubeView
It works fine for me.
I can download youtube videos.

I used this code:

LBYouTubeExtractor *extractor = [[[LBYouTubeExtractor alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:(@"http://www.youtube.com/watch?v=%@"), self.videoID ]] quality:LBYouTubeVideoQualityLarge] autorelease];
[extractor extractVideoURLWithCompletionBlock:^(NSURL *videoURL, NSError *error) {
    if(!error) {
        NSLog(@"Did extract video URL using completion block: %@", videoURL);

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSData *data = [NSData dataWithContentsOfURL: videoURL];
            NSString *pathToDocs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            NSString *filename = [NSString stringWithFormat:(@"video_%@.mp4"), self.videoID ];
            [data writeToFile:[pathTODocs stringByAppendingPathComponent:filename] atomically:YES];
            NSLog(@"File %@ successfully saved", filename);
        });
    } else {
        NSLog(@"Failed extracting video URL using block due to error:%@", error);
    }
}];

You can show progress of downloading using technique described in the posts above.

深爱不及久伴 2024-10-25 11:23:42

这是我的示例: https://github.com/comonitos/youtube_video

我使用 PSYouTubeExtractor.h 类Peter Steinberger 它可以获取 youtube mp4 视频 url,并且下载和观看不是问题

NSURLConnection
+
NS通知中心
+
PSYouTube提取器
+
NSMutableData

Here is my example: https://github.com/comonitos/youtube_video

I used PSYouTubeExtractor.h class by Peter Steinberger It can get youtube mp4 video url and than downloading and viewing is not a problem

NSURLConnection
+
NSNotificationCenter
+
PSYouTubeExtractor
+
NSMutableData

捶死心动 2024-10-25 11:23:42

检查这些项目 -

https://github.com/iosdeveloper/MyTube
https://github.com/pvinis/mytube

这些绝对会对你有帮助!

check these projects -

https://github.com/iosdeveloper/MyTube
https://github.com/pvinis/mytube

these will definitely help you!!

以往的大感动 2024-10-25 11:23:42

我个人不知道如何下载 youtube 视频(代码太大,无法在此处给出答案)。

不过,此处提供了完整的 YouTube 下载示例。
这是一个名为 LoadTube 的开源 YouTube 下载器:这里是源代码的链接

I don't personally know how to download youtube videos (and the code is too big to put in an answer here).

However, here's a complete youtube downloading example here.
It's an open source youtube downloader called LoadTube: here's the a link to the source code.

故事还在继续 2024-10-25 11:23:42

我会播放视频并找出临时文件的存储位置。如果您可以访问它,请将其复制到某个文档文件夹中以供离线查看。

I would play the video and figure out where the temp file is being stored. If you can get access to it, copy it into some document folder for offline viewing.

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