Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

http - 如何通过HTTP请求将视频上传到YouTube?(How do I upload a video to YouTube via an HTTP request?)

I've been trying to figure this out for hours now.

(我已经尝试了好几个小时了。)

Consulting the official documentation It says I need to make a post request to https://www.googleapis.com/upload/youtube/v3/videos with a content type header set to video/* or application/octet-stream (I've used the latter).

(查阅官方文档它说我需要对https://www.googleapis.com/upload/youtube/v3/videos发出发布请求,其内容类型标头设置为video/*application/octet-stream (我ve使用了后者)。)

Turns out if I just post a buffer of a video file to that url it'll work.

(原来,如果我只是将视频文件的缓冲区发布到该URL,它将起作用。)

But the documentation also says I can specify a whole bunch of options about the video (title, description, tags, etc.) However, it says to attach that information to the request body!

(但是文档还说我可以指定有关视频的一整套选项(标题,描述,标签等)。但是,它表示要将这些信息附加到请求正文中!)

I'm confused on how I'm supposed to send both the video bytes and the options in the same request.

(我对如何在同一请求中发送视频字节和选项感到困惑。)

Maybe it's not supposed to be the same request, but they don't mention anything about using multiple.

(也许不应该是相同的请求,但是他们没有提及使用多的要求。)

  ask by Mason translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can actually use the sdk or some youtube library relevant to the language you are using to write API's.

(实际上,您可以使用与您用于编写API的语言相关的sdk或某些youtube库。)

In the documentation, it isn't clear what to specify for passing both at same time.

(在文档中,不清楚要为同时传递两者指定什么内容。)

var youtubeService = new YouTubeService(new BaseClientService.Initializer() {
    HttpClientInitializer = credential,
    ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});

  var video = new Video();
  video.Snippet = new VideoSnippet();
  video.Snippet.Title = "Default Video Title";
  video.Snippet.Description = "Default Video Description";
  video.Snippet.Tags = new string[] { "tag1", "tag2" };
  video.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
  video.Status = new VideoStatus();
  video.Status.PrivacyStatus = "unlisted"; // or "private" or "public"
  var filePath = @"REPLACE_ME.mp4"; // Replace with path to actual movie file.

  using (var fileStream = new FileStream(filePath, FileMode.Open))
  {
    var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
    videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
    videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;

    await videosInsertRequest.UploadAsync();
  }
}

Here, we are passing both the file stream and the metadata in the same function.

(在这里,我们将文件流和元数据传递到同一函数中。)

You can also use the library provided by google to do the same.

(您也可以使用Google提供的库来执行相同的操作。)

I hope this helps.

(我希望这有帮助。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...