Python 通过 YouTube 数据上传大视频时出现 MemoryError API
Python MemoryError when uploading big video through YouTube Data API
尝试将大型视频文件上传到 YouTube 时出现 MemoryError
。上传较小的文件时没问题,但当文件较大时,我得到 MemoryError
。我猜这是因为代码试图将视频文件加载到 RAM。有没有什么方法可以上传视频而不加载到RAM。
import os
from googleapiclient.http import MediaFileUpload
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube.upload"]
def main():
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "0"
client_secret_file = 'client_credentials.json'
api_name = 'youtube'
api_version = 'v3'
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(client_secret_file, scopes)
credentials = flow.run_console()
youtube = googleapiclient.discovery.build(api_name, api_version, credentials=credentials)
request = youtube.videos().insert(
part='snippet,status',
body={
'snippet': {
'categoryId': "20",
'title': 'test title',
'tags': ['tag1', 'tag2']
},
'status': {
'privacyStatus': 'private'
}
},
media_body=MediaFileUpload(File Path)
)
response = request.execute()
print(response)
if __name__ == '__main__':
main()
您的 MemoryError
异常的原因如下:您的输入视频文件 gets loaded in memory 在被处理以发送到远程服务之前完整:
def createMethod(methodName, methodDesc, rootDesc, schema):
...
def method(self, **kwargs):
...
if media_filename:
...
if media_upload.resumable():
...
else:
# A non-resumable upload
if body is None:
...
else:
...
payload = media_upload.getbytes(0, media_upload.size())
...
与其一次性上传视频,我强烈建议您也使用 by means of the time-tested public Google script upload_video.py
. (This script has an official documentation。要从脚本中获取不言自明的使用信息页面,只需使用命令行选项 --help
.)
如果您正在使用 Python 3(您的代码表明),那么您必须将该脚本转换为 Python 3,因为它是为 Python 2 编写的。为此,请参阅 的 修补 upload_video.py
部分。
尝试将大型视频文件上传到 YouTube 时出现 MemoryError
。上传较小的文件时没问题,但当文件较大时,我得到 MemoryError
。我猜这是因为代码试图将视频文件加载到 RAM。有没有什么方法可以上传视频而不加载到RAM。
import os
from googleapiclient.http import MediaFileUpload
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube.upload"]
def main():
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "0"
client_secret_file = 'client_credentials.json'
api_name = 'youtube'
api_version = 'v3'
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(client_secret_file, scopes)
credentials = flow.run_console()
youtube = googleapiclient.discovery.build(api_name, api_version, credentials=credentials)
request = youtube.videos().insert(
part='snippet,status',
body={
'snippet': {
'categoryId': "20",
'title': 'test title',
'tags': ['tag1', 'tag2']
},
'status': {
'privacyStatus': 'private'
}
},
media_body=MediaFileUpload(File Path)
)
response = request.execute()
print(response)
if __name__ == '__main__':
main()
您的 MemoryError
异常的原因如下:您的输入视频文件 gets loaded in memory 在被处理以发送到远程服务之前完整:
def createMethod(methodName, methodDesc, rootDesc, schema):
...
def method(self, **kwargs):
...
if media_filename:
...
if media_upload.resumable():
...
else:
# A non-resumable upload
if body is None:
...
else:
...
payload = media_upload.getbytes(0, media_upload.size())
...
与其一次性上传视频,我强烈建议您也使用 upload_video.py
. (This script has an official documentation。要从脚本中获取不言自明的使用信息页面,只需使用命令行选项 --help
.)
如果您正在使用 Python 3(您的代码表明),那么您必须将该脚本转换为 Python 3,因为它是为 Python 2 编写的。为此,请参阅 upload_video.py
部分。