【问题标题】:Video file is stuck at 0% when being uploaded via YouTube API V3 with Python (Resumable Uploads)使用 Python 通过 YouTube API V3 上传视频文件时卡在 0%(可恢复上传)
【发布时间】:2020-12-17 17:29:39
【问题描述】:

我正在使用 YouTube API V3 在 YouTube 上上传可恢复的视频。执行代码时,响应的 status_code 为 200(但根据官方文档,如果 API 工作正常,则需要 201)。 当我打开 YouTube 频道时,那里的视频显示“0% 处理”。 此外,一段时间后,会出现一条消息“处理已放弃”(如所附屏幕截图所示)。 我试图在脚本中包含所需的参数,但没有任何方法可以改变 YouTube 频道上视频的处理级别。 这里的代码如下:

import requests
import json
import httplib2
import sys

class youtube:
    #Access token obtained from OAuth 2.0 Playground
    token = 'ya29.a0Af................................'

    def resumable_uploads(self):
    
        token = self.token
        url = 'https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status,contentDetails'
        headers = {'Authorization': 'Bearer {}'.format(token), 'host': 'www.googleapis.com', 'content-type': 'application/json; charset=UTF-8', 'X-Upload-Content-Type': 'video/*'}
    
        body = {
                "snippet": {
                    "title": "Video 2 by Sidra ",
                    "description": "This is a description of my video",
                    "tags": ["cool", "video", "more keywords"],
                    "categoryId": 22
                },
                "status": {
                    "privacyStatus": "public",
                    "embeddable": True,
                    "license": "youtube",
                    "selfDeclaredMadeForKids" : True
                }
            }
        response = requests.request('POST', url, headers=headers, data=json.dumps(body))
        print("status_code: ", response.status_code)
    
        if response.status_code == 200: 
        
            location = response.headers['location']
            headers = {'Authorization': 'Bearer {}'.format(token),'content-type': 'video/*'}
        
            #converting file into Bytes
            video_file= open("testVideo.mp4", "rb")
            file_content = video_file.read()
            video_file.close()

            body = {
                "videoFile" : file_content
            }
        
            response = requests.request('PUT', location, headers=headers, data=body)
            print(response.text)
            print("status_code: ", response.status_code)

obj = youtube()
obj.resumable_uploads()

控制台的响应如下:

status_code:  200    
{
  "kind": "youtube#video",
  "etag": "0rZIqeGvCifiyfQymb8CKXsC6CI",
  "id": "YF2DQDACB4M",
  "snippet": {
    "publishedAt": "2020-08-28T17:29:12Z",
    "channelId": "UCXonQaqsiqTV_YqYsB6W4Dg",
    "title": "Video 2 by Sidra",
    "description": "This is a description of my video",
    "thumbnails": {
      "default": {
        "url": "https://i.ytimg.com/vi/YF2DQDACB4M/default.jpg",
        "width": 120,
        "height": 90
      },
      "medium": {
        "url": "https://i.ytimg.com/vi/YF2DQDACB4M/mqdefault.jpg",
        "width": 320,
        "height": 180
      },
      "high": {
        "url": "https://i.ytimg.com/vi/YF2DQDACB4M/hqdefault.jpg",
        "width": 480,
        "height": 360
      }
    },
    "channelTitle": "Sidra Sial",
    "tags": [
      "cool",
      "video",
      "more keywords"
    ],
    "categoryId": "22",
    "liveBroadcastContent": "none",
    "localized": {
      "title": "Video 2 by Sidra",
      "description": "This is a description of my video"
    }
  },
  "contentDetails": {
    "duration": "P0D",
    "dimension": "2d",
    "definition": "sd",
    "caption": "false",
    "licensedContent": false,
    "contentRating": {},
    "projection": "rectangular",
    "hasCustomThumbnail": false
  },
  "status": {
    "uploadStatus": "uploaded",
    "privacyStatus": "public",
    "license": "youtube",
    "embeddable": true,
    "publicStatsViewable": true,
    "selfDeclaredMadeForKids": true
  }
}

status_code:  200

The screenshot from YouTube Channel:

【问题讨论】:

    标签: python youtube-data-api


    【解决方案1】:

    我个人不建议您使用纯 HTTP 请求方法实现可恢复视频上传。要做到这一点非常棘手。

    fully working example 开始并修改它以满足您的需求更可行。

    无论如何,这里是Resumable Upload Protocol 的官方(相当重的)文档。


    只看一眼你的代码,我就注意到你错过了传递给第一个 HTTP 请求方法调用——POST——标头X-Upload-Content-Length。 (请注意,我没有完整地分析您的代码;请遵循我上面的建议。)

    【讨论】:

    • 我按照文档编写了这段代码。此外,您分享的示例(在上面的链接中)是针对本机应用程序的,但我正在为“网络类型”编写此示例。
    • 另外,在 --POST-- 方法的标头中,我故意错过了 X-Upload-Content-Length,因为这会导致文件大小问题(在运行时)。
    • 什么样的问题?官方文档说X-Upload-Content-Length是必需的标头。
    • 我建议您从工作代码开始,根据您的需要进行调整;注意:您在上面发布的原始问题中没有指定您的应用要求
    猜你喜欢
    • 2016-01-10
    • 2013-05-16
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 2014-12-30
    相关资源
    最近更新 更多