简体   繁体   English

使用 Google Cloud 在视频中添加水印 function python

[英]Add watermark in video with Google Cloud function python

I am adding watermark in uploaded video by http google cloud function but my code is return on ffmpeg non zero.我在 http 谷歌云 function 上传的视频中添加水印,但我的代码返回 ffmpeg 非零。 my python code我的 python 代码

import os
from google.cloud import storage
from subprocess import check_output
from videoprops import get_video_properties


def hello_world(request):

client = storage.Client()
bucket = client.get_bucket('bucket_name')
request_json = request.get_json()
req_data = request.get_json()
name = req_data['file']
videofile_name = req_data['file_name']
os.makedirs('/tmp/'+os.path.dirname(name), exist_ok=True)
file_name = '/tmp/' + name
output_file_name = '/tmp/' + name.split('.')[0] + '_.'+name.split('.')[1]
print(output_file_name)
logo_path = '/temp/watermark.png'
logo_name = 'watermark.png'
print('logo found')

print(file_name)

try:
    os.remove(file_name)
except OSError:
    pass

try:
    os.remove(logo_path)
except OSError:
    pass

print("File has been removed")

# Downloading the video to the cloud functions
blob = bucket.get_blob(name)
blob.download_to_filename(file_name)

blob_logo = bucket.get_blob(logo_name)
blob_logo.download_to_filename(logo_path)

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)
    # 19-7-2020
    check_output('ffmpeg  -i '+file_name+' -i '+logo_path +
                 ' -filter_complex overlay=10:10 -codec:a copy -preset ultrafast -async 1 '+output_file_name, shell=True)
    thumbnail_blob = bucket.blob(
        os.path.dirname(name) + '/'+videofile_name)
    thumbnail_blob.upload_from_filename(output_file_name)
    # -------------------------------------
else:
    print("MP4 not created")

print("uploaded")

In this code accessing video add watermark also accessing from bucket and applying with ffmpeg and uploading.在此访问视频的代码中,添加水印也从存储桶访问并使用 ffmpeg 应用并上传。

error is:-错误是:-

raise CalledProcessError(retcode, process.args, subprocess.CalledProcessError: Command 'ffmpeg -i /tmp/Upload/Video/1060/ad69ec74-49db-4fdb-b118-d23b9468a7b8.mp4 -i /temp/watermark.png -filter_complex overlay=10:10 -codec:a copy -preset ultrafast -async 1 /tmp/Upload/Video/1060/ad69ec74-49db-4fdb-b118-d23b9468a7b8_.mp4' returned non-zero exit status 1.引发 CalledProcessError(retcode, process.args, subprocess.CalledProcessError: Command 'ffmpeg -i /tmp/Upload/Video/1060/ad69ec74-49db-4fdb-b118-d23b9468a7b8.mp4 -i /temp/watermark.png -filter_complex overlay= 10:10 -codec:a copy -preset ultrafast -async 1 /tmp/Upload/Video/1060/ad69ec74-49db-4fdb-b118-d23b9468a7b8_.mp4' 返回非零退出状态 1。

I have solved and using with http request base trigger in google cloud function.我已经解决并使用了谷歌云 function 中的 http 请求基触发器。 May be help to someone in future将来可能对某人有所帮助

import os
import os.path
from google.cloud import storage
from subprocess import check_output
from videoprops import get_video_properties


def hello_world(request):
    """Responds to any HTTP request.
    Args:
        request (flask.Request): HTTP request object.
    Returns:
        The response text or any set of values that can be turned into a
        Response object using
        # flask.Flask.make_response>`.
        `make_response <http://flask.pocoo.org/docs/1.0/api/
    """
    client = storage.Client()
    bucket = client.get_bucket('showtalentbucket')
    request_json = request.get_json()
    req_data = request.get_json()
    name = req_data['file']
    videofile_name = req_data['file_name']
    os.makedirs('/tmp/'+os.path.dirname(name), exist_ok=True)
    file_name = '/tmp/' + name
    output_file_name = '/tmp/' + name.split('.')[0] + '_.'+name.split('.')[1]
    mp4_output_file_name = '/tmp/' + \
        name.split('.')[0] + '__.'+name.split('.')[1]
    print(output_file_name)
    logo_path = '/tmp/watermark.png'
    logo_name = 'watermark.png'
    print('logo found')

    print(file_name)

    try:
        os.remove(file_name)
    except OSError:
        pass

    try:
        os.remove(logo_path)
    except OSError:
        pass

    print("File has been removed")

    # Downloading the video to the cloud functions
    blob = bucket.get_blob(name)
    blob.download_to_filename(file_name)

    blob_logo = bucket.get_blob(logo_name)
    blob_logo.download_to_filename(logo_path)

    print("Video Downloaded")

    props = get_video_properties(file_name)

    if os.path.exists(file_name):
        print("NEW MP4 EXISTS")
      
        check_output('ffmpeg  -i '+file_name+' -i '+logo_path +
                     ' -filter_complex overlay=10:10 -codec:a copy -preset ultrafast -async 1 '+output_file_name, shell=True)
        if os.path.splitext(name)[1].lower().startswith('.mov'):
            check_output('ffmpeg  -itsoffset -4  -i '+output_file_name +
                         ' -acodec copy -vcodec copy -f mov '+mp4_output_file_name, shell=True)
            thumbnail_blob = bucket.blob(
                os.path.dirname(name) + '/'+videofile_name)
            thumbnail_blob.upload_from_filename(mp4_output_file_name)
        else:
            thumbnail_blob = bucket.blob(
                os.path.dirname(name) + '/'+videofile_name)
            thumbnail_blob.upload_from_filename(output_file_name)
        # -------------------------------------
    else:
        print("MP4 not created")

    print("uploaded")
   

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

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