python-3.x - 使用 Google Cloud 函数 python 在视频中添加水印

标签 python-3.x video ffmpeg google-cloud-functions

我正在通过 http google cloud 功能在上传的视频中添加水印,但我的代码在非零的 ffmpeg 上返回。我的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")
在这段访问视频的代码中,添加水印也可以从存储桶访问并使用 ffmpeg 应用并上传。
错误是:-
提高 CalledProcessError(retcode, process.args,
subprocess.CalledProcessError:命令'ffmpeg -i/tmp/Upload/Video/1060/ad69ec74-49db-4fdb-b118-d23b9468a7b8.mp4 -i/temp/watermark.png -filter_complex overlay=10:10 -codec:a copy -预设超快 -async 1/tmp/Upload/Video/1060/ad69ec74-49db-4fdb-b118-d23b9468a7b8_.mp4' 返回非零退出状态 1。

最佳答案

我已经解决并在谷歌云功能中使用了 http 请求基础触发器。将来可能对某人有所帮助

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")
   

关于python-3.x - 使用 Google Cloud 函数 python 在视频中添加水印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63180955/

相关文章:

c - 如何对音频流使用混合和连接过滤器?

ffmpeg - 如何减慢 ffmpeg 编码速度?

c# - 使用 ffmpeg 和 C# 创建转换队列

python - 导入关系中使用的 SQLAlchemy 模型?

java - 有没有办法获取 MPEG 文件的元数据(如高度、帧率、比特率)?

python - 为什么 `max` 和 `min` 在使用空可迭代对象调用时不返回适当的无穷大?

image - 视频文件到 flutter 中的图像流

batch-file - ffmpeg 为视频的每一秒覆盖相同的文件

python - python中如何将二进制转换为字符串?

python - Discord.py 错误 : TypeError: __new__() got an unexpected keyword argument 'deny_new'