是否可以使用 discord.py 搜索流式 YouTube 音频(从视频中的给定时间戳开始播放)?
Is it possible to seek through streamed youtube audio with discord.py (play from a given timestamp in the video)?
不幸的是,传入带有 &t= 标记的 URL 不会导致 discord.py
的 VoiceClient
在该时间戳开始播放。我正在使用 youtube_dl
.
是否可以在 discord.py 中搜索音频以便从开始以外的某个地方开始流式传输 YouTube 视频?
我知道一些像 Groovy 这样的专业机器人可以搜索流式 YouTube 视频的命令,所以 Discord API 本身就可以做到这一点。
我使用的代码来自 here.
在 ffmpeg_options
中,您可以使用 -ss
标志查找特定时间戳。
如果您希望从 40 秒开始,选项应该是这样的:
ffmpeg_options = {
'options': '-vn -ss 40'
}
当然你可以在stream
命令中添加一个可选变量:
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
我只添加了我从音乐机器人示例中编辑的代码,所以我希望我编辑的内容很清楚。如果有任何进一步的说明 needed/how 有用,那么我很乐意进行编辑。
参考文献:
- FFMPEG Docs -
-ss
. 的 Ctrl + F
- Optional arguments in discord commands
- f-strings - Python 3.6.0+
不幸的是,传入带有 &t= 标记的 URL 不会导致 discord.py
的 VoiceClient
在该时间戳开始播放。我正在使用 youtube_dl
.
是否可以在 discord.py 中搜索音频以便从开始以外的某个地方开始流式传输 YouTube 视频?
我知道一些像 Groovy 这样的专业机器人可以搜索流式 YouTube 视频的命令,所以 Discord API 本身就可以做到这一点。
我使用的代码来自 here.
在 ffmpeg_options
中,您可以使用 -ss
标志查找特定时间戳。
如果您希望从 40 秒开始,选项应该是这样的:
ffmpeg_options = {
'options': '-vn -ss 40'
}
当然你可以在stream
命令中添加一个可选变量:
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
我只添加了我从音乐机器人示例中编辑的代码,所以我希望我编辑的内容很清楚。如果有任何进一步的说明 needed/how 有用,那么我很乐意进行编辑。
参考文献:
- FFMPEG Docs -
-ss
. 的 Ctrl + F
- Optional arguments in discord commands
- f-strings - Python 3.6.0+