pythontimedurationyoutube-dl

Converting seconds to minutes and hours in 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

So it is all about the second line. Right now the output is shown the duration in seconds. I would like to change the format so that the actual time is displayed. For example the video is 3 min long:


Solution

  • Going from the docs (https://github.com/ytdl-org/youtube-dl/blob/master/README.md#readme), duration is the Length of the video in seconds, so you can just manually go from 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))
    

    that time string may be a little messy (the int() is so when you print it it doesn't have decimal points) but that has all the relevant info.