我正在尝试通过ios应用程序通过Swift使用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上遇到视频失败
答案 0 :(得分:1)
通常发生这种情况的原因有两个:
如果视频太大,则有时在上传时可能会丢失一些数据包。这将导致视频编码失败。
它确实可以正确上传,但是视频编码在youtube端失败。
可以检查很少的内容来调试/修复问题:
您已在响应中收到视频的ID。通过ID拨打电话,并检查响应中是否提及失败原因。
如果视频文件很大,则最好使用chunk上传而不是分段上传。