【问题标题】:With Google Cloud Function creating thumbnail of video with sub dir path使用 Google Cloud Function 创建具有子目录路径的视频缩略图
【发布时间】:2020-11-06 16:07:44
【问题描述】:

我正在通过 rest api 将视频上传到谷歌云存储桶中,并添加一个函数来使用 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 这样的文件夹。

谢谢 达梅什

【问题讨论】:

  • 你好 Dharmesh,欢迎来到 Stack Overflow,我对你的问题有点困惑,这是我的理解。目前您的视频正在上传到您的存储桶的“/tmp”目录中,您想将其上传到该目录的子目录中,对吗?该子目录是否已经存在或者您是否需要使用您的代码创建它?如果存在,缩略图是否正确上传?
  • 当视频上传完成时我在事件中设置了这个功能,所以我的视频总是上传到子目录,所以需要访问和更新。所以我已经更正了我的代码,所以当有人需要时我会上传。
  • 谢谢!。下面的代码工作正常,但下一步是在视频中添加水印,使其无法正常工作并崩溃。

标签: python-3.x google-app-engine google-cloud-storage thumbnails


【解决方案1】:

这里我的子目录视频代码可以生成缩略图并上传到同一个目录。

    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

  • 谷歌云存储
  • 获取视频属性

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 2015-01-24
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 2018-04-10
    相关资源
    最近更新 更多