<p>我做了一个<a href="https://github.com/amirashabani/MusicophileBot" rel="noreferrer">Telegram robot</a>,它的工作之一是从音频文件创建<em>样本</em>。现在,对于发送给它的大多数音频来说,样本是非常好的;类似这样:</p>
<p><a href="https://i.stack.imgur.com/ZUPhs.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ZUPhs.png" alt="enter image description here"/></a></p>
<p>然而,对于一些<em>音频,示例看起来有点奇怪:</p>
<p><a href="https://i.stack.imgur.com/Df6Ec.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Df6Ec.png" alt="enter image description here"/></a></p>
<p>如您所见,此文件中的波不显示在<em>中!(我可以向你保证,声音不是空的)</p>
<p>为了创建示例,我使用<code>pydub</code>(谢谢,<a href="https://github.com/jiaaro/pydub" rel="noreferrer">James</a>!)。下面是我创建示例的部分:</p>
<pre><code>song = AudioSegment.from_mp3('song.mp3')
sliced = song[start*1000:end*1000]
sliced.export('song.ogg', format='ogg', parameters=["-acodec", "libopus"])
</code></pre>
<p>然后我用<code>bot.send_voice</code>方法发送样本。<a href="https://github.com/amirashabani/MusicophileBot/blob/4e3e0d82a79910b450460d0faaa2321a5689e71b/commands.py#L241" rel="noreferrer">Like this</a>:</p>
^{2}$
<p><a href="https://core.telegram.org/bots/api#sendvoice" rel="noreferrer">Telegram Bot API</a>的文档说明:</p>
<blockquote>
<p>Use this method to send audio files, if you want Telegram clients to
display the file as a playable voice message. For this to work, your
audio must be in an .ogg file encoded with <strong>OPUS</strong> (other formats may be
sent as Audio or Document).</p>
</blockquote>
<p>这就是为什么在这一行代码中:</p>
<pre><code>sliced.export('song.ogg', format='ogg', parameters=["-acodec", "libopus"])
</code></pre>
<p>我用了<code>parameters=["-acodec", "libopus"]</code>。在</p>
<p>谁能告诉我我做错了什么吗?提前谢谢!在</p>