使用 Google Cloud Function 创建带有子目录路径的视频缩略图
With Google Cloud Function creating thumbnail of video with sub dir path
我正在通过休息 api 将视频上传到 google 云存储桶并添加一个功能以使用 python 生成缩略图。代码在主目录上工作,但我的视频上传到 sub/sub/ 目录,所以我的代码不工作。
import os
from google.cloud import storage
from subprocess import check_output
from videoprops import get_video_properties
client = storage.Client()
def hello_gcs(data, context):
print(context)
print(data)
if data['contentType'].startswith('video/'):
bucket = client.get_bucket(data['bucket'])
name = data['name']
file_name = '/tmp/'+ name
print(file_name)
thumbnail_file_name = '/tmp/' + name.split('.')[0] + '.jpg'
print(thumbnail_file_name)
try:
os.remove(file_name)
except OSError:
pass
try:
os.remove(thumbnail_file_name)
except OSError:
pass
print("File has been removed")
blob = bucket.get_blob(name)
blob.download_to_filename(file_name)
print("Video Downloaded")
props = get_video_properties(file_name)
if os.path.exists(file_name):
print("NEW MP4 EXISTS")
check_output('ffmpeg -itsoffset -4 -i '+file_name+' -vcodec mjpeg -vframes 1 -an -f rawvideo -s '+str(props['width'])+'x'+str(props['height'])+' '+thumbnail_file_name, shell=True)
thumbnail_blob = bucket.blob('thumbnail.jpg')
thumbnail_blob.upload_from_filename(thumbnail_file_name)
else:
print("MP4 not created")
print("uploaded")
else :
print("Not a Video")
所以我只能访问 tmp 但不能创建像 /tmp/Upload/Video/232/video.mp4.
这样的文件夹
谢谢
达美什
这里我的子目录视频代码可以生成缩略图并在同一目录中上传。
import os
from google.cloud import storage
from subprocess import check_output
from videoprops import get_video_properties
client = storage.Client()
def hello_gcs(data, context):
print(context)
print(data)
if data['contentType'].startswith('video/'):
bucket = client.get_bucket(data['bucket'])
name = data['name']
os.makedirs('/tmp/'+os.path.dirname(name), exist_ok=True)
file_name = '/tmp/'+ name
print(file_name)
thumbnail_file_name = '/tmp/' + name.split('.')[0] + '.jpg'
print(thumbnail_file_name)
try:
os.remove(file_name)
except OSError:
pass
try:
os.remove(thumbnail_file_name)
except OSError:
pass
print("File has been removed")
blob = bucket.get_blob(name)
blob.download_to_filename(file_name)
print("Video Downloaded")
props = get_video_properties(file_name)
if os.path.exists(file_name):
print("NEW MP4 EXISTS")
check_output('ffmpeg -itsoffset -4 -i '+file_name+' -vcodec mjpeg -vframes 1 -an -f rawvideo -s '+str(props['width'])+'x'+str(props['height'])+' '+thumbnail_file_name, shell=True)
thumbnail_blob = bucket.blob(os.path.dirname(name)+'/thumbnail.jpg')
thumbnail_blob.upload_from_filename(thumbnail_file_name)
else:
print("MP4 not created")
print("uploaded")
else :
print("Not a Video")
Requirement.txt
- google-云存储
- 获取视频属性
我正在通过休息 api 将视频上传到 google 云存储桶并添加一个功能以使用 python 生成缩略图。代码在主目录上工作,但我的视频上传到 sub/sub/ 目录,所以我的代码不工作。
import os
from google.cloud import storage
from subprocess import check_output
from videoprops import get_video_properties
client = storage.Client()
def hello_gcs(data, context):
print(context)
print(data)
if data['contentType'].startswith('video/'):
bucket = client.get_bucket(data['bucket'])
name = data['name']
file_name = '/tmp/'+ name
print(file_name)
thumbnail_file_name = '/tmp/' + name.split('.')[0] + '.jpg'
print(thumbnail_file_name)
try:
os.remove(file_name)
except OSError:
pass
try:
os.remove(thumbnail_file_name)
except OSError:
pass
print("File has been removed")
blob = bucket.get_blob(name)
blob.download_to_filename(file_name)
print("Video Downloaded")
props = get_video_properties(file_name)
if os.path.exists(file_name):
print("NEW MP4 EXISTS")
check_output('ffmpeg -itsoffset -4 -i '+file_name+' -vcodec mjpeg -vframes 1 -an -f rawvideo -s '+str(props['width'])+'x'+str(props['height'])+' '+thumbnail_file_name, shell=True)
thumbnail_blob = bucket.blob('thumbnail.jpg')
thumbnail_blob.upload_from_filename(thumbnail_file_name)
else:
print("MP4 not created")
print("uploaded")
else :
print("Not a Video")
所以我只能访问 tmp 但不能创建像 /tmp/Upload/Video/232/video.mp4.
这样的文件夹谢谢 达美什
这里我的子目录视频代码可以生成缩略图并在同一目录中上传。
import os
from google.cloud import storage
from subprocess import check_output
from videoprops import get_video_properties
client = storage.Client()
def hello_gcs(data, context):
print(context)
print(data)
if data['contentType'].startswith('video/'):
bucket = client.get_bucket(data['bucket'])
name = data['name']
os.makedirs('/tmp/'+os.path.dirname(name), exist_ok=True)
file_name = '/tmp/'+ name
print(file_name)
thumbnail_file_name = '/tmp/' + name.split('.')[0] + '.jpg'
print(thumbnail_file_name)
try:
os.remove(file_name)
except OSError:
pass
try:
os.remove(thumbnail_file_name)
except OSError:
pass
print("File has been removed")
blob = bucket.get_blob(name)
blob.download_to_filename(file_name)
print("Video Downloaded")
props = get_video_properties(file_name)
if os.path.exists(file_name):
print("NEW MP4 EXISTS")
check_output('ffmpeg -itsoffset -4 -i '+file_name+' -vcodec mjpeg -vframes 1 -an -f rawvideo -s '+str(props['width'])+'x'+str(props['height'])+' '+thumbnail_file_name, shell=True)
thumbnail_blob = bucket.blob(os.path.dirname(name)+'/thumbnail.jpg')
thumbnail_blob.upload_from_filename(thumbnail_file_name)
else:
print("MP4 not created")
print("uploaded")
else :
print("Not a Video")
Requirement.txt
- google-云存储
- 获取视频属性