使用 Youtube REST API 上传视频失败
Posted
技术标签:
【中文标题】使用 Youtube REST API 上传视频失败【英文标题】:Uploded Video using Youtube rest API getting Failed 【发布时间】:2019-03-15 02:22:29 【问题描述】:我正在尝试通过 Swift 从 ios 应用程序使用 YouTube 的 REST API 将视频上传到 YouTube,但我很难弄清楚该怎么做。我已使用以下代码从应用程序在 youtube 上上传视频。
func postVideoToYouTube(uploadUrl: String, videoData: Data, title: String, description: String, callback: @escaping PostVideoCallback)
if (self.authorizer != nil) && (self.authorizer?.canAuthorize)!
self.servive?.apiKey = kGoogleAPIKey
self.servive?.authorizer = authorizer
let status = GTLRYouTube_VideoStatus()
status.privacyStatus = "public"
let snippet = GTLRYouTube_VideoSnippet()
snippet.title = title
let desc = description
if desc.count > 0
snippet.descriptionProperty = desc
var video = GTLRYouTube_Video()
video.status = status
video.snippet = snippet
// ---------------------
let accessToken = UserDefaults.standard.string(forKey: kAccessToken) ?? ""
let headers: HTTPHeaders = ["Authorization": "Bearer \(accessToken)"]
Alamofire.upload(
multipartFormData: multipartFormData in
let metadata = "'snippet':'title' : '\(title)', 'description': '\(description)'".data(using: .utf8, allowLossyConversion: false)!
multipartFormData.append(metadata, withName: "snippet", mimeType: "application/json")
multipartFormData.append(videoData, withName: "video", fileName: "demo2.mov", mimeType: "application/octet-stream")
,
to: "https://www.googleapis.com/upload/youtube/v3/videos?part=snippet",
headers: headers,
encodingCompletion: encodingResult in
switch encodingResult
case .success(let upload, _, _):
upload.responseJSON response in
print(response.result.value)
do
let jsonData = try JSONSerialization.jsonObject(with: response.data!, options: .allowFragments) as! JSON
catch
print("error serializing JSON: \(error)")
callback("", false)
print("Success")
case .failure(_):
print("Failure")
callback("", false)
)
我收到了来自 Youtube 的以下回复
`Optional(
etag = "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/CAh7qTdnefHn2x9Fdt7z11TW1HM\"";
id = muradZ51g78;
kind = "youtube#video";
snippet =
categoryId = 22;
channelId = UCfI1223CGZiSGVKhapRfc4Q;
channelTitle = "Khushbu Desai";
description = Test;
liveBroadcastContent = none;
localized =
description = Test;
title = Video;
;
publishedAt = "2018-10-10T07:09:40.000Z";
thumbnails =
default =
height = 90;
url = "https://i.ytimg.com/vi/muradZ51g78/default.jpg";
width = 120;
;
high =
height = 360;
url = "https://i.ytimg.com/vi/muradZ51g78/hqdefault.jpg";
width = 480;
;
medium =
height = 180;
url = "https://i.ytimg.com/vi/muradZ51g78/mqdefault.jpg";
width = 320;
;
;
title = Video;
;
)
Success`
但我没有在我的频道上找到该视频。因为我在 youtube Studio 上收到视频失败
Please check This Image 请帮忙
【问题讨论】:
【参考方案1】:这通常有两个原因:
如果视频太大,有时上传时可能会丢失少量数据包。这将导致视频编码失败。
确实可以正确上传,但在 youtube 端视频编码失败。
可以检查一些东西来调试/修复问题:
您在响应中收到了视频的 ID。通过 Id 进行 get 调用,并检查响应中是否提到了失败原因。
如果视频文件很大,那么最好选择chunk 上传而不是分段上传。
【讨论】:
该视频的大小为 344KB以上是关于使用 Youtube REST API 上传视频失败的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 C# 中的 Youtube API 将视频上传到 YouTube?