我可以使用以下代码通过REST API将视频成功上传到youtube:
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,
"https://www.googleapis.com/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)
}
})
}
我现在想编辑上面的代码,以便在初始上传时设置一些snippet
数据,特别是title
和description
。
我尝试使用与上述方法非常相似的替代Alamofire
方法,只是将NSMutableURLRequest
作为参数。我精心设计了我的代码段字典,并将其设置为可变请求的HTTPBody
属性。视频上传仍然有效,但我的代码段的标题和说明值仍未设置。
如何在将视频上传到youtube时设置一些代码段信息?
答案 0 :(得分:2)
在multipartFormData块中为参数值添加一行,如下所示(将代码放在视频项之前):
multipartFormData.appendBodyPart(data:"{'snippet':{'title' : 'TITLE_TEXT', 'description': 'DESCRIPTION_TEXT'}}".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"snippet", mimeType: "application/json")
邮政网址也应更改为part = snippet
https://www.googleapis.com/upload/youtube/v3/videos?part=snippet
即
.POST,
"https://www.googleapis.com/upload/youtube/v3/videos?part=snippet",
headers: headers,
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data:"{'snippet':{'title' : 'TITLE_TEXT', 'description': 'DESCRIPTION_TEXT'}}".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"snippet", mimeType: "application/json")
multipartFormData.appendBodyPart(data: videodata, name: "video", fileName: "video.mp4", mimeType: "application/octet-stream")
},
答案 1 :(得分:2)
我能够使用片段数据上传视频的唯一方法是通过POST和PUT调用的组合。 POST 上传没有元数据的视频, PUT 编辑视频的元数据。这样做需要做几件事:
1. Enable the necessary scopes
-https://www.googleapis.com/auth/youtube.upload for POST
-https://www.googleapis.com/auth/youtube for PUT
2. Be mindful of the URL's you use for each request
-https://www.googleapis.com/upload/youtube/v3/videos?part=snippet for POST
-https://www.googleapis.com/youtube/v3/videos?part=snippet&key=\(ios_key) for PUT
现在,如果您正在使用Alamofire,则您的问题中包含的POST可以保持原样。对于PUT部分,您必须注意Alamofire希望如何传递参数:
request(<#T##method: Method##Method#>, <#T##URLString: URLStringConvertible##URLStringConvertible#>, parameters: <#T##[String : AnyObject]?#>, encoding: <#T##ParameterEncoding#>, headers: <#T##[String : String]?#>)
注意参数:&lt; [String:AnyObject]?&gt;这是完全您必须如何定义代码段字典:
let dictionarySnippet :Dictionary<String, AnyObject> = [
"title" : "something in the way",
"description" : "is this finally gonna work?",
"tags" : ["whisky","tango","fox"],
"categoryId" : "1"
]
您还必须发送上传的视频ID(您可以在另一个词典中从POST中捕获该内容:
let dictionaryParameters :Dictionary<String, AnyObject> = [
"id" : "\(returnedId)",
"snippet" : dictionarySnippet,
]
最后,您现在可以发送PUT请求来更新您的视频元数据了:
let putRequest = request(.PUT, "https://www.googleapis.com/youtube/v3/videos?part=snippet&key=\(ios_key)", parameters: dictionaryParameters, encoding: .JSON , headers: headers)
答案 2 :(得分:0)
有关Qt / C ++示例代码,请参见https://gist.github.com/forderud/941e1d2014c43a0e3f5f0af1d7972075,该示例代码显示了如何将视频上传到YouTube,包括摘要元数据。 C ++当然不是Swift,但是应该类似于Swift的实现。