【问题标题】:How to input audio as bytes in moviepy如何在moviepy中将音频作为字节输入
【发布时间】:2020-01-15 11:45:24
【问题描述】:

我有如下形式的音频字节:

b'ID3\x04\x00\x00\x00\x00\x00#TSSE\x00\x00\x00\x0f\x00\x00\x03Lavf57.71.100\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\...

我从亚马逊网络服务获得的:

import boto3

client = boto3.client('polly')
response = client.synthesize_speech(
    Engine='neural',
    LanguageCode='en-GB',
    OutputFormat='mp3',
    SampleRate='8000',
    Text='hey whats up this is a test',
    VoiceId='Brian'
)

我想将它输入到moviepy音频文件中使用

AudioFileClip()

AudioFileClip 采用文件名或代表声音的数组。我知道我可以将音频保存为文件并读取它,但我想让 AudioFileClip 获取上面显示的字节输出。

我试过了:

AudioFileClip(response['AudioStream'].read())

但这给出了错误:

TypeError: endswith first arg must be bytes or a tuple of bytes, not 字符串

我能做什么?

【问题讨论】:

    标签: python-3.x amazon-web-services audio boto3 audio-streaming


    【解决方案1】:

    您需要将音频流转换为不同的类型。 (这就是为什么它被称为 TypeError)。你把它作为一个字符串,它需要一个字节格式。

    您可以使用 bytearray 函数将 str 转换为字节! https://docs.python.org/3/library/functions.html#func-bytearray

    你也可以看看这个问题: Best way to convert string to bytes in Python 3?

    如需更多帮助,请在此回答者上发表评论,我会尽快为您提供帮助。

    希望这对您的项目有所帮助,

    PythonMasterLua

    【讨论】:

    • 但是 response['AudioStream'].read() 的输出是 b'ID3\x04\x00\x0... 是字节类型。我没有输入字符串。
    • 代码认为你在输入一个str,所以你必须做上面的函数。
    猜你喜欢
    • 2010-09-21
    • 2022-07-21
    • 2013-07-04
    • 1970-01-01
    • 2022-07-31
    • 1970-01-01
    • 2023-01-26
    • 2018-12-21
    • 2021-06-29
    相关资源
    最近更新 更多