voice_player.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:plumeria 作者: sk89q 项目源码 文件源码
def play(message, url):
    """
    Play a URL.

    Example::

        /play https://www.youtube.com/watch?v=U9DZkj8Rq6g

    """

    m = LINK_PATTERN.search(url)
    if not m:
        raise CommandError("You need to provide a URL to play.")
    url = m.group(1)

    # check to see if we can play this URL
    opts = {
        'format': 'webm[abr>0]/bestaudio/best',
        'prefer_ffmpeg': True,
        'source_address': source_address(),
        'noplaylist': True,
    }
    ydl = youtube_dl.YoutubeDL(opts)
    func = functools.partial(ydl.extract_info, url, download=False)
    try:
        info = await asyncio.get_event_loop().run_in_executor(None, func)

        # get metadata
        is_twitch = 'twitch' in url
        if is_twitch:
            # twitch has 'title' and 'description' sort of mixed up
            title = info.get('description')
            description = None
        else:
            title = info.get('title')
            description = info.get('description')
    except DownloadError as e:
        raise CommandError("Can't play <{}>. It might not be a supported site.".format(url))

    # get the voice channel
    voice_client = await get_voice_client(message.author)
    queue = queue_map.get(voice_client.channel)

    # queue that stuff up
    async def factory(entry: QueueEntry):
        return await voice_client.create_ytdl_player(url, ytdl_options=opts, after=entry.on_end,
                                                     options=['-af', 'loudnorm=I=-16:TP=-1.5:LRA=11'])

    meta = EntryMeta(title=title, description=description, url=url)
    entry = await queue.add(factory, channel=voice_client.channel, meta=meta)

    # tell the user
    entries = queue.entries()
    index = entries.index(entry)
    if index == 0:
        return "Now playing **{}**...".format(meta)
    else:
        return "Queued **{}** at position #{} for play.".format(meta, index)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号