gpt4 book ai didi

swift - 使用 swift 和 alamofire 将视频(和属性)上传到 youtube

转载 作者:可可西里 更新时间:2023-11-01 00:36:48 24 4
gpt4 key购买 nike

我有兴趣将视频连同标题、描述和关键字上传到 youtube。下面的代码将没有任何属性的视频上传到 youtube:

func postVideoToYouTube(token: String, callback: Bool -> Void){

let headers = ["Authorization": "Bearer \(token)"]
let urlYoutube = "https://www.googleapis.com/upload/youtube/v3/videos?part=id"

let path = NSBundle.mainBundle().pathForResource("video", ofType: "mp4")
let videodata: NSData = NSData.dataWithContentsOfMappedFile(path!)! as! NSData
upload(
.POST,
urlYoutube,
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)
}
})

我一直在尝试修改 urlYoutube 以包含必要的片段信息,但无济于事:

let snippetTitle = "The Best Video Ever"
let snippetDesc = "first video upload with title"
let snippetTags = "best,video,ever,snoopy,monkey,charlie"
let urlYoutube = "https://www.googleapis.com/upload/youtube/v3/videos?part=id&snippet.title=%@&snippet.description=%@&snippet.keywords=%@", snippetTitle, snippetDesc, snippetTags)"

我尝试的另一种方法(感谢@adjuremods 的建议)是使用基于 Youtube-API 的 Request Body 来编辑以前上传的视频。所以,首先,我定义了一个视频资源:

let parms = [
"kind": "youtube#video",
"id" : returnedId,
"snippet.title" : "summer vacation cali",
"snippet.description" : "had fun in the sun",
"snippet.tags" : ["surf","fun","sun"],
"snippet.categoryId" : "1"
]

并将其作为 PUT 请求发送,如下所示:

do {
let parmsJson = try NSJSONSerialization.dataWithJSONObject(parameters, options: .PrettyPrinted)

let putURL = NSURL(string: "https://www.googleapis.com/upload/youtube/v3/videos")!

let request = NSMutableURLRequest(URL: putURL)
request.HTTPMethod = "PUT"
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
request.setValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type")
request.HTTPBody = parmsJson

let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in
if error != nil{
print("Error -> \(error)")
return
}

do {
let result = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject]

print("Result -> \(result)")

} catch {
print("Error -> \(error)")
}
}

task.resume()
//return task


} catch {
print ("...could not accomplish put request")
}

不幸的是,无论我如何修改视频资源,我得到的结果总是一样的:

Result -> Optional(["error": {
code = 400;
errors = (
{
domain = global;
message = "Unsupported content with type: application/json; charset=UTF-8";
reason = badContent;
}
);
message = "Unsupported content with type: application/json; charset=UTF-8";

有人可以建议我哪里可能出错吗?我对如何设置 API 定义的这些参数不是很清楚:

https://developers.google.com/youtube/v3/docs/videos/insert#parameters

最佳答案

为参数值添加一行到 multipartFormData block ,如下所示(将代码放在视频项之前 - 并 - 根据隐含结构添加任何其他代码段属性值):

multipartFormData.appendBodyPart(data:"{'snippet':{'title' : 'TITLE_TEXT', 'description': 'DESCRIPTION_TEXT'}}".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"snippet", mimeType: "application/json")

post url也应该改成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")
},

关于swift - 使用 swift 和 alamofire 将视频(和属性)上传到 youtube,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36120525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com