問題描述
不幸的是,傳入帶有 &t= 標記的 URL 不會導致 discord.py
的 VoiceClient
在該時間戳開始播放.我正在使用 youtube_dl
.
Unfortunately passing in a URL with a &t= tag does not cause discord.py
's VoiceClient
to start playing at that timestamp. I'm using youtube_dl
.
是否可以在 discord.py 中搜索音頻,以便從開頭以外的某個地方開始流式傳輸 YouTube 視頻?
Is is possible to seek through audio within discord.py in order to start streaming a YouTube video from somewhere besides the start?
我知道像 Groovy 之類的一些專業機器人具有用于流式 YouTube 視頻的搜索命令,因此 Discord API 本身能夠這個.
I know some professional bots like Groovy have seek commands for streamed YouTube videos, so the Discord API itself is capable of this.
我使用的代碼來自 這里一個>.
推薦答案
在 ffmpeg_options
中,您可以使用 -ss 查找特定的時間戳代碼>標志.
In the ffmpeg_options
, you're able to seek to a specific timestamp with the use of the -ss
flag.
如果您希望從例如 40 秒開始,這就是選項的外觀:
This is just how the options should look if you wish to start from, for example, 40 seconds:
ffmpeg_options = {
'options': '-vn -ss 40'
}
當然你可以在 stream
命令中添加一個可選變量:
And of course you can add an optional variable to the stream
command:
import typing # for the optional argument of the timestamp
@classmethod
async def from_url(cls, url, *, loop=None, stream=False, timestamp=0):
# moved the options from outside the class to inside the method.
# this allows the use of variables in the options
ffmpeg_options = {
'options': f'-vn -ss {timestamp}'
}
# rest of the from_url code
@commands.command()
async def stream(self, ctx, timestamp: typing.Optional[int]=0, *, url): # add the arg
"""Streams from a url (same as yt, but doesn't predownload)"""
async with ctx.typing():
player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True, timestamp=timestamp)
# other code
我只添加了我從音樂機器人示例編輯的代碼,所以我希望我編輯的內容很清楚.如果需要任何進一步的說明/某些東西是如何工作的,那么我很樂意進行編輯.
I only added in the code that I edited from the music bot example, so I hope it's clear what I edited. If any further clarification is needed/how something works, then I'll be happy to make edits.
參考資料:
- FFMPEG 文檔 -
-ss
的 Ctrl + F.李> - discord 命令中的可選參數
- f-strings - Python 3.6.0+
- FFMPEG Docs - Ctrl + F for
-ss
. - Optional arguments in discord commands
- f-strings - Python 3.6.0+
這篇關于是否可以使用 discord.py(從視頻中的給定時間戳播放)搜索流式 youtube 音頻?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!