Python 3.x 使用谷歌云功能创建具有子目录路径的视频缩略图

Python 3.x 使用谷歌云功能创建具有子目录路径的视频缩略图,python-3.x,google-app-engine,google-cloud-storage,thumbnails,Python 3.x,Google App Engine,Google Cloud Storage,Thumbnails,我正在通过RESTAPI将视频上传到google Cloud bucket中,并添加了一个使用python生成缩略图的函数。代码在主目录下工作,但我的视频上传到了sub/sub/目录,所以我的代码不工作 import os from google.cloud import storage from subprocess import check_output from videoprops import get_video_properties client = storage.Client(

我正在通过RESTAPI将视频上传到google Cloud bucket中,并添加了一个使用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

在这里,我的sub-dir视频代码可以生成缩略图并在同一目录中上传

    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
  • 谷歌云存储
  • 获取视频属性

您好,达尔梅什,欢迎来到Stack Overflow,您的问题让我有点困惑,以下是我的理解。当前您的视频正在上载到bucket的“/tmp”目录,您希望将其上载到该目录的子目录,对吗?该子目录是否已经存在,或者是否需要用代码创建它?如果它确实存在,缩略图上传是否正确?此功能在视频上传完成时设置为事件,因此我的视频始终以sub dir方式上传,因此需要访问和更新。所以我已经更正了我的代码,所以我会在有人需要时上传。谢谢!。下面的代码工作正常,但下一步是在视频中添加水印,使其无法工作并崩溃。