是否可以通过discord.py(从视频中的给定时间戳播放)流式传输YouTube音频? [英] Is it possible to seek through streamed youtube audio with discord.py (play from a given timestamp in the video)?

查看:167
本文介绍了是否可以通过discord.py(从视频中的给定时间戳播放)流式传输YouTube音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,传递带有& 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 中,您可以使用<$ c $来查找特定的时间戳。 c> -ss 标志。

In the ffmpeg_options, you're able to seek to a specific timestamp with the use of the -ss flag.

如果您希望从40秒开始,这就是选项的外观: / p>

This is just how the options should look if you wish to start from, for example, 40 seconds:

ffmpeg_options = {
    'options': '-vn -ss 40'
}

d当然,您可以向 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 Docs - Ctrl + F for -ss.
  • Optional arguments in discord commands
  • f-strings - Python 3.6.0+

这篇关于是否可以通过discord.py(从视频中的给定时间戳播放)流式传输YouTube音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆