【问题标题】:Processing abandoned The video could not be processed - Youtube API?放弃处理视频无法处理 - Youtube API?
【发布时间】:2020-10-24 04:40:12
【问题描述】:

我正在使用 youtube data v3 api 通过我的网站上传 mp4 视频,http 发布请求如下所示:

access_token = request.session['access_token']
url = "https://www.googleapis.com/upload/youtube/v3/videos?part=snippet"

    payload = {
        'snippet':{
            "categoryId": "22",
            "description": "Description of uploaded video.",
            "title": "Test video upload."
        }
    }
    files = [
        ('media_body', request.FILES['media_body'])
    ]
    headers = {
        'Content-Type': 'video/mp4',
        'Authorization': 'Bearer '+access_token,
    }

    response = requests.request("POST", url, headers=headers, data=payload, files=files)

    print(response.text.encode('utf8'))

    context = {
        'r': response
    }
    return render_to_response('dashboard/manage_content/youtube.html', context)

这是我得到的回应:

{ "kind": "youtube#video", "etag": "yS7DhsHOhsDM-vXZiGUmLXcKST0", "id": "dII224dYC2o", "snippet": { "publishedAt": "2020-07-03T12:11:43Z", "channelId": "UCmv2Sec30iBc13b7ntf93Ow", "title": "unknown", "description": "", "thumbnails": { "default": { "url": "https://i.ytimg.com/vi/dII224dYC2o/default.jpg", "width": 120, "height": 90 }, "medium": { "url": "https://i.ytimg.com/vi/dII224dYC2o/mqdefault.jpg", "width": 320, "height": 180 }, "high": { "url": "https://i.ytimg.com/vi/dII224dYC2o/hqdefault.jpg", "width": 480, "height": 360 } }, "channelTitle": "Nikhil Dhoot", "categoryId": "20", "liveBroadcastContent": "none", "localized": { "title": "unknown", "description": "" } } }

但在视频选项卡上,我收到错误:处理已放弃视频无法处理。 视频响应成功消息的事实意味着它之前的所有身份验证过程都已成功。但是,如果需要,我也可以编辑和共享该代码。

【问题讨论】:

  • 实际上,我们放弃了整个想法,因为 youtube api 有很多复杂性,现在他们从 6 月 29 日开始制定了一些严格的规定(我认为)。但是您的答案似乎很恰当,因此我将其标记为正确。感谢您的宝贵时间。

标签: django youtube-api youtube-data-api


【解决方案1】:

将视频上传到 YouTube 需要两个步骤:

  1. 调用Videos.insert API 端点,将正确的JSON text describing 视频发布到该端点。

  2. 在 API 调用时,从 API 端点响应中获取实际上传视频内容的位置。然后进行实际的内容上传操作。

请仔细检查 Google 在 YouTube 上上传视频时公开提供的 source code。您将了解我上面概述的算法的所有具体细节。

如果您想深入了解函数resumable_upload 中的调用request.next_chunk() 的工作原理,请阅读相关部分——方法HttpRequest.next_chunk——在Google API 客户端库的文件http.py 中用于 Python。

【讨论】:

  • 所以我需要为上传过程调用两个端点,一个用于有关视频的相关信息,下一个应该具有上传整个视频的 media_body ?我实际上在邮递员上尝试了上面的代码,它工作得非常好——视频信息和实际文件在同一个 API 调用中。我要进行简单的上传而不是可恢复的上传。可恢复是更好的选择吗?
  • 我上面描述的两步过程构成了任何 API 客户端必须以一种或另一种方式实现的基本逻辑。 (附带说明:official docs 根本不用费心记录这个过程)。请注意,不同的客户端库可能会选择实现上传过程(步骤 (1) 和 (2)),以便为用户减轻繁琐的细节 - 从而隐藏步骤 (2),使库用户的生活更轻松。
  • 关于是否使用可恢复上传功能,我会说这取决于。如果您有可靠且快速的 Internet 连接,那么您可以一次性上传视频。但是,使用该功能,您可以重试上传之前失败的任何块(从而避免整个上传过程中止)。还要考虑视频文件的长度。在 (1) + (2) 结束时,任何失败的上传都被 API 计为一次完成的上传操作,因此会花费您 1600(加上更多额外)单位的配额。
  • 此外,以下是 Google 开发人员在 upload_video.py 示例代码 [第一部分] 中的 initialize_upload 函数中注意到的内容:The chunksize parameter specifies the size of each chunk of data, in bytes, that will be uploaded at a time. Set a higher value for reliable connections as fewer chunks lead to faster uploads. Set a lower value for better recovery on less reliable connections.
  • 第二部分:Setting 'chunksize' equal to -1 in the code below means that the entire file will be uploaded in a single HTTP request. (If the upload fails, it will still be retried where it left off.) This is usually a best practice, but if you're using Python older than 2.6 or if you're running on App Engine, you should set the chunksize to something like 1024 * 1024 (1 megabyte).
猜你喜欢
  • 2021-10-12
  • 2020-02-26
  • 1970-01-01
  • 2012-01-02
  • 1970-01-01
  • 2014-09-02
  • 1970-01-01
  • 2017-07-28
  • 1970-01-01
相关资源
最近更新 更多