使用 Swift 将视频上传到 YouTube

编程入门 行业动态 更新时间:2024-10-26 07:30:36
本文介绍了使用 Swift 将视频上传到 YouTube的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

仍然经常检查这个,当我或其他人帮助我弄清楚时将标记为已解决!

Still checking this frequently, will mark as solved when I or someone else helps me figure it out!

我正在尝试通过 Swift 使用 YouTube 的 REST API 将视频上传到 YouTube,但我很难弄清楚该怎么做.我目前有一个有效的 GET 请求.

I am trying to upload a video to YouTube with YouTube’s REST API via Swift but I am having great difficulty figuring out what to do. I currently have a working GET request.

我对 POST 请求 URL 应该如何构建以及文件位置在请求中的位置感到困惑.另外我认为我应该使用可恢复上传协议?

I'm confused on how the POST request URL should be constructed and where the file location is meant to go in the request. Also I think I should be using the resumbable upload protocol?

我已经为各种 API 和文档苦苦挣扎了 2 天,现在感到绝望.

I have been struggling through various API's and documentation for 2 days now and am feeling hopeless.

这是我的 GET 请求的工作代码.

Here is my working code for a GET request.

func getRequestVideoInfo(){ // Set up your URL let youtubeApi = "www.googleapis/youtube/v3/videos?part=contentDetails%2C+snippet%2C+statistics&id=AKiiekaEHhI&key=" + apiKey let url = NSURL(string: youtubeApi) // Create your request let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in do { if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as? [String : AnyObject] { print("Response from YouTube: \(jsonResult)") } } catch { print("json error: \(error)") } }) // Start the request task.resume() }

推荐答案

HEADS UP(截至 2019 年 10 月 6 日)YouTube 已将 API 配额减少到每天 10,000 个单位.这相当于在 24 小时内上传 4 个 YouTube 视频.如果您的应用程序用例依赖于上传许多 YouTube 视频,我强烈建议您重新考虑.您可以申请扩大您的每日配额,但众所周知,Google 回复您的速度很慢,如果他们这样做的话.

HEADS UP (as of October 6, 2019) YouTube has decreased the API quota to 10,000 units per day. This is the equivalent of uploading 4 YouTube videos during a 24 hour time span. If your application use case relies on uploading many YouTube videos, I strongly advise you to reconsider. You can apply for an expansion of your daily quota, but Google is notoriously slow at getting back to you, if they do at all.

是的,您需要在 YouTube 中创建一个应用/项目,并使用 OAuth 2.0 Flow 将视频发布/插入到您获得授权访问的频道.

Yes, you need to create an app/project in YouTube and use the OAuth 2.0 Flow to post/insert videos to a channel to which you receive authorized access.

一旦您从 GOOGLE 获得访问令牌

ONCE YOU HAVE YOUR ACCESS TOKEN FROM GOOGLE

按如下方式使用 Alamofire:

use Alamofire as follows:

func postVideoToYouTube(token: String, callback: Bool -> Void){ let headers = ["Authorization": "Bearer \(token)"] let path = NSBundle.mainBundle().pathForResource("video", ofType: "mp4") let videodata: NSData = NSData.dataWithContentsOfMappedFile(path!)! as! NSData upload( .POST, "www.googleapis/upload/youtube/v3/videos?part=id", headers: headers, multipartFormData: { multipartFormData in multipartFormData.appendBodyPart(data: videodata, name: "video", fileName: "video.mp4", mimeType: "application/octet-stream") }, encodingCompletion: { encodingResult in switch encodingResult { case .Success(let upload, _, _): upload.responseJSON { request, response, error in print(response) callback(true) } case .Failure(_): callback(false) } }) }

像这样调用post函数:

Call the post function like this:

postVideoToYouTube(accessToken, callback: { success in if success { } })

更多推荐

使用 Swift 将视频上传到 YouTube

本文发布于:2023-11-17 07:37:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视频   Swift   YouTube

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!