rac*_*t99 5 python ffmpeg amazon-s3 moviepy
知道如何将 S3 mp4 文件直接读取到 moviepy 中吗?
我努力了,
import boto3
from io import BytesIO
from moviepy.editor import *
client = boto3.client('s3')
obj = client.get_object(Bucket='some-bucket', Key='some-file')
VideoFileClip(BytesIO(obj['Body'].read()))
Run Code Online (Sandbox Code Playgroud)
但我越来越,
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/<path>/lib/python3.6/site-packages/moviepy/video/io/VideoFileClip.py", line 91, in __init__
fps_source=fps_source)
File "/<path>/lib/python3.6/site-packages/moviepy/video/io/ffmpeg_reader.py", line 33, in __init__
fps_source)
File "/<path>/lib/python3.6/site-packages/moviepy/video/io/ffmpeg_reader.py", line 243, in ffmpeg_parse_infos
is_GIF = filename.endswith('.gif')
AttributeError: '_io.BytesIO' object has no attribute 'endswith'
Run Code Online (Sandbox Code Playgroud)
其中路径是我的虚拟环境
小智 2
这是一个老问题,提前为延迟表示歉意,但对于将来寻找潜在解决方案的任何人来说,您可以将带有直接 url 的视频加载到 VideoFileClip 中。
首先,您需要生成存储桶中对象的预签名 URL:
import boto3
import logging
def create_presigned_url(bucket_name, object_name, expiration=60):
"""Generate a presigned URL to share an S3 object
:param bucket_name: string
:param object_name: string
:param expiration: Time in seconds for the presigned URL to remain valid
:return: Presigned URL as string. If error, returns None.
"""
my_config = Config(
signature_version = 's3v4',)
# Generate a presigned URL for the S3 object
s3_client = boto3.client('s3',config=my_config)
try:
response = s3_client.generate_presigned_url('get_object',
Params={'Bucket':bucket_name,
'Key': object_name},
ExpiresIn=expiration,
)
except ClientError as e:
logging.error(e)
return None
# The response contains the presigned URL
return response
Run Code Online (Sandbox Code Playgroud)
从那里,您可以将该确切的网址放入VideoFileClip:
import moviepy.editor as mpe
presignedurl=create_presigned_url(bucket,key)
vidclip=mpe.VideoFileClip(presignedurl)
Run Code Online (Sandbox Code Playgroud)