Python Youtube ffmpeg会话已失效 [英] Python Youtube ffmpeg Session Has Been Invalidated

查看:120
本文介绍了Python Youtube ffmpeg会话已失效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用机器人播放YouTube音频时收到以下错误

I get the following error while I'm playing YouTube audio with my bot

[tls @ 0000024ef8c4d480] Error in the pull function.
[matroska,webm @ 0000024ef8c4a400] Read error
[tls @ 0000024ef8c4d480] The specified session has been invalidated for some reason.
    Last message repeated 1 times

YouTube链接似乎已过期?我真的不知道,但是我需要解决这个问题。这是我的代码:

It seems like YouTube links expire? I don't really know but I need to fix this issue. This is my code:

    class YTDLSource(discord.PCMVolumeTransformer):

        def __init__(self, source, *, data, requester):
            super().__init__(source)
            self.requester = requester

            self.title = data['title']
            self.description = data['description']
            self.uploader = data['uploader']
            self.duration = data['duration']
            self.web_url = data['webpage_url']
            self.thumbnail = data['thumbnail']

        def __getitem__(self, item: str):
            return self.__getattribute__(item)

        @classmethod
        async def create_source(cls, ctx, player, search: str, *, loop, download=True):
            async with ctx.typing():
                loop = loop or asyncio.get_event_loop()
                to_run = partial(ytdl.extract_info, url=search, download=download)
                raw_data = await loop.run_in_executor(None, to_run)

                if 'entries' in raw_data:
                    # take first item from a playlist
                    if len(raw_data['entries']) == 1:
                        data = raw_data['entries'][0]
                    else:
                        data = raw_data['entries']
                        #loops entries to grab each video_url
                        total_duration = 0
                        try:
                            for i in data:
                                webpage = i['webpage_url']
                                title = i['title']
                                description = i['description']
                                uploader = i['uploader']
                                duration = i['duration']
                                thumbnail = i['thumbnail']
                                total_duration += duration

                                if download:
                                    source = ytdl.prepare_filename(i)
                                    source = cls(discord.FFmpegPCMAudio(source), data=i, requester=ctx.author)
                                else:
                                    source = {'webpage_url': webpage, 'requester': ctx.author, 'title': title, 'uploader': uploader, 'description': description, 'duration': duration, 'thumbnail': thumbnail}

                                player.queue.append(source)

                        except Exception as e:
                            print(e)
                            return

                        embed=discord.Embed(title="Playlist", description="Queued", color=0x30a4fb, timestamp=datetime.now(timezone.utc))
                        embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar_url)
                        embed.set_thumbnail(url=data[0]['thumbnail'])
                        embed.add_field(name=raw_data['title'], value=f"{len(data)} videos queued.", inline=True)
                        embed.set_footer(text=raw_data["uploader"] + ' - ' + '{0[0]}m {0[1]}s'.format(divmod(total_duration, 60)))
                        await ctx.send(embed=embed)
                        return

                embed=discord.Embed(title="Playlist", description="Queued", color=0x30a4fb, timestamp=datetime.now(timezone.utc))
                embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar_url)
                embed.set_thumbnail(url=data['thumbnail'])
                embed.add_field(name=data['title'], value=(data["description"][:72] + (data["description"][72:] and '...')), inline=True)
                embed.set_footer(text=data["uploader"] + ' - ' + '{0[0]}m {0[1]}s'.format(divmod(data["duration"], 60)))
                await ctx.send(embed=embed)

                if download:
                    source = ytdl.prepare_filename(data)
                else:
                    source = {'webpage_url': data['webpage_url'], 'requester': ctx.author, 'title': data['title'], 'uploader': data['uploader'], 'description': data['description'], 'duration': data['duration'], 'thumbnail': data['thumbnail']}
                    player.queue.append(source)
                    return

                source = cls(discord.FFmpegPCMAudio(source), data=data, requester=ctx.author)
                player.queue.append(source)


        @classmethod
        async def regather_stream(cls, data, *, loop):
            loop = loop or asyncio.get_event_loop()
            requester = data['requester']

            to_run = partial(ytdl.extract_info, url=data['webpage_url'], download=True)
            data = await loop.run_in_executor(None, to_run)

            return(cls(discord.FFmpegPCMAudio(data['url']), data=data, requester=requester))

我正在为机器人使用discord.py的重写分支。
我不确定是否需要提供更多详细信息?请让我知道,我真的需要修复此问题。

I'm using the rewrite branch of discord.py for the bot. I'm not sure if I need to provide more details? Please let me know, I really need to get this fixed...

推荐答案

事实上,这并不是问题所在您的代码(很多人抱怨这个错误)。

In fact it isn't really a problem with your code (and many people complain of this error).

这只是视频时可能出现的问题。如果您绝对要流式传输,则必须将其视为潜在问题。注意每个音乐机器人如何(几乎)为您想听的视频/音乐设置限制。

This is just a possible issue when streaming a video. If you absolutely want to stream it, you have to accept this as a potential issue. Note how (almost) every music bots set limitations for the video/music you want to listen to.

如果您需要确保自己没有遇到这个问题,则可以完全下载音乐。 (这还会使机器人在游戏之前加载更长的时间。)

If you need to ensure you do not get this issue, you have to fully download the music. (Which will also make the bot loading longer before playing).

这篇关于Python Youtube ffmpeg会话已失效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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