【发布时间】:2019-12-10 19:10:32
【问题描述】:
我已将视频存储在谷歌云存储浏览器上。我想在我的前端播放这些视频。为此,我需要视频 URL。但问题是,每当我导航到该 URL 时,文件就会被下载。
我需要做什么来获取我存储的对象的流视频 URL?
【问题讨论】:
标签: google-cloud-platform video-streaming google-cloud-storage
我已将视频存储在谷歌云存储浏览器上。我想在我的前端播放这些视频。为此,我需要视频 URL。但问题是,每当我导航到该 URL 时,文件就会被下载。
我需要做什么来获取我存储的对象的流视频 URL?
【问题讨论】:
标签: google-cloud-platform video-streaming google-cloud-storage
Cloud Storage 支持使用gcloud 进行流式传输。您还可以使用第三方Boto python lib 在应用程序的后端进行流传输。
但由于您希望使用前端实现此解决方案,我猜您实际上是在寻找媒体播放器。
所以,你需要:
1.- 生成对象的SignedUrl(您的视频):
使用 gcloud 命令:
gsutil signurl -d 10m Desktop/private-key.json gs://example-bucket/cat.jpeg
使用 python 库:
from google.cloud import storage
import datetime
def generate_download_signed_url_v4(bucket_name, blob_name):
"""Generates a signed URL for downloading a blob.
Note that this method requires a service account key file. You can not use
this if you are using Application Default Credentials from Google Compute
Engine or from the Google Cloud SDK.
"""
storage_client = storage.Client.from_service_account_json('key.json')
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(blob_name)
url = blob.generate_signed_url(
# This URL is valid for 15 minutes
expiration=datetime.timedelta(minutes=15),
# Allow GET requests using this URL.
method='GET'
)
print('Generated GET signed URL:')
print(url)
print('You can use this URL with any user agent, for example:')
print('curl \'{}\''.format(url))
return url
generate_download_signed_url_v4("example-id", "example.mp4")
2.- 使用该 url 初始化 HMTL5 媒体播放器:
<!DOCTYPE html>
<html>
<body>
<video width="320" height="240" controls>
<source src="video-signedurl.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</body>
【讨论】:
Content-Type 上传的可能性。这将导致浏览器下载而不是播放它。
我使用 video.js 完成了这项工作。
<html>
<head>
<link href="https://unpkg.com/video.js/dist/video-js.css" rel="stylesheet">
<style>
.video-js {
width: 600px;
height: 600px;
}
</style>
</head>
<body>
<video-js controls data-setup="{}">
<source src="https://storage.cloud.google.com/bucketName/folderName/recordingName.flv" type="video/flv">
</video-js>
<script src="https://unpkg.com/video.js/dist/video.min.js"></script>
<script src="https://unpkg.com/videojs-flash/dist/videojs-flash.min.js"></script>
</body>
</html>
【讨论】: