简体   繁体   English

使用 Google Cloud Function 创建具有子目录路径的视频缩略图

[英]With Google Cloud Function creating thumbnail of video with sub dir path

I am uploading Videos by rest api into google Cloud bucket and add one function to generate thumbnail with python.我正在将 rest api 的视频上传到谷歌云存储桶中,并添加一个 function 以生成带有 Z23EEEB4347BDD7BDD75DZ23EEEB4347BDD756533EEEB4347BDD756 的缩略图Code working on main directory but my video's uploaded in sub/sub/ directory so my code is not working.代码在主目录上工作,但我的视频上传到 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")

So im only accessing tmp but not able to create folder like /tmp/Upload/Video/232/video.mp4.所以我只能访问 tmp 但不能创建像 /tmp/Upload/Video/232/video.mp4 这样的文件夹。

Thanks Dharmesh谢谢 Dharmesh

Here my code for sub dir videos can generate thumbnail and upload in same 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需求.txt

  • google-cloud-storage谷歌云存储
  • get-video-properties获取视频属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM