gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:12:28 27 4
gpt4 key购买 nike

我正在通过 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com