在youtubedl中将秒转换为分钟和小时

问题描述

        embed = discord.Embed(
            title=self.title,description=f"**Channel:** {self.uploader}\n **Duration:** {self.duration}",url=self.video_url)
        embed.set_footer(
            text=f"Requested by: {self.requested_by.name}",icon_url=self.requested_by.avatar_url)
        if self.thumbnail:
            embed.set_thumbnail(url=self.thumbnail)
        return embed

这一切都与第二行有关。现在,输出以秒为单位显示持续时间。我想更改格式,以便显示实际时间。例如,视频长3分钟:

  • 在YouTube上显示03:00。我的机器人将其转换为300秒。 有没有办法改变这一点,以便给出实际时间?

解决方法

从文档(https://github.com/ytdl-org/youtube-dl/blob/master/README.md#readme)来看,durationLength of the video in seconds,因此您可以手动从hours:minutes:seconds秒开始。

total_seconds = self.duration
hours = (total_seconds - ( total_seconds % 3600))/3600
seconds_minus_hours = (total_seconds - hours*3600)
minutes = (seconds_minus_hours - (seconds_minus_hours % 60) )/60
seconds = seconds_minus_hours - minutes*60

time = '{}:{}:{}'.format(int(hours),int(minutes),int(seconds))

该时间字符串可能有点混乱(int()是这样,因此在打印时它没有小数点),但是具有所有相关信息。