通过 python 将大文件上传到视频索引器

Large file upload to Video Indexer through python

我正在尝试通过视频索引器 API 上传一个大视频(大约 1.5 GB)。然而,我的机器占用了大量内存。部署系统有相当少量的 RAM。我想使用 API 以便将视频分成多个部分上传而不会占用太多内存(大约 100MB 就足够了)。

我尝试使用 ffmpeg 将视频分成块并逐块上传,但视频索引器将它们识别为不同的视频并为每个视频提供单独的见解。视频要是在线聚合就更好了

如何将分块视频上传到 MS Video Indexer?

让我猜猜。之前,您按照官方教程Tutorial: Use the Video Indexer API and the Upload Video API参考(API参考页末尾的Python示例代码如下图)上传了您的大视频。

因为下面的代码发送了从内存读取的数据块{body},它的值来自于代码open("<your local file name>").read().

,因此占用了大量内存
conn.request("POST", "/{location}/Accounts/{accountId}/Videos?name={name}&accessToken={accessToken}&%s" % params, "{body}", headers)

但是,如果您仔细阅读 videoUrl of the document Upload and index your videos 小节和以下 C# 代码,即使是 API 参考中对 videoUrl 的解释,您也会看到视频文件作为multipart/form正文内容不是唯一的方式。

videoUrl

A URL of the video/audio file to be indexed. The URL must point at a media file (HTML pages are not supported). The file can be protected by an access token provided as part of the URI and the endpoint serving the file must be secured with TLS 1.2 or higher. The URL needs to be encoded.

If the videoUrl is not specified, the Video Indexer expects you to pass the file as a multipart/form body content.

带有videoUrl

的C#代码的屏幕截图

API 参考中 videoUrl 参数的屏幕截图

您可以先通过 Python streaming upload code or other tools like azcopy or Azure Storage Explorer 将大型视频文件上传到 Azure Blob Storage 或其他满足 videoUrl 要求的在线服务,然后以 Azure Blob Storage 为例生成 blob url 使用 sas 令牌(Python 代码如下)将其作为 videoUrl 传递给 API 上传请求。

Python 使用 sas 令牌

生成 blob url 的代码
from azure.storage.blob.baseblobservice import BaseBlobService
from azure.storage.blob import BlockBlobService, BlobPermissions
from datetime import datetime, timedelta

account_name = '<your account name>'
account_key = '<your account key>'
container_name = '<your container name>'
blob_name = '<your blob name>'
service = BaseBlobService(account_name=account_name, account_key=account_key)

token = service.generate_blob_shared_access_signature(container_name, blob_name, BlobPermissions.READ, datetime.utcnow() + timedelta(hours=1),)
blobUrlWithSas = f"https://{account_name}.blob.core.windows.net/{container_name}/{blob_name}?{token}"

希望对您有所帮助。