【问题标题】:How to download file with pytube in maximum available resolution?How to download file with pytube in maximum available resolution?
【发布时间】:2022-12-19 07:05:43
【问题描述】:
url = message.text
file = YouTube(url)
stream = file.streams.get_by_resolution(360)
stream.download(filename='qwe.mp4')

I know only two possible ways to download stream. I can get it by itag or by resolution(here its 360). But there is a problem, because sometimes the video I want to download do not have 360, maximum 240 or 120.

My question is: how can I download a file in its maximum available resolution, but not higher than 1080. Is it possible?

If its not, can i download the video by its first existing itag?

【问题讨论】:

    标签: python pytube


    【解决方案1】:

    Yes it's possible you can just use the filter functionality to loop through all the possible resolution and pick the highest excluding those above 1080.

    You can find the documentation on filters here: https://pytube.io/en/latest/user/streams.html#filtering-for-mp4-streams

    【讨论】:

      【解决方案2】:

      Try this:

      from pytube import YouTube
      
      file = YouTube('<url>')
      
      # little utility to parse out the resolution from a stream description like 1440p
      def get_resolution(s):
          return int(s.resolution[:-1])
      
      
      stream = max(
          filter(lambda s: get_resolution(s) <= 1080,  # filter out sub-1080p streams
                 filter(lambda s: s.type == 'video', file.fmt_streams)), # filter out the video streams
          key=get_resolution  # maximum resolution among those streams
      )
      print(stream)
      

      Which gives us the stream that we want. Then we can download with the download method:

      stream.download('file.mp4')
      

      【讨论】:

        【解决方案3】:

        When using pytube, your variable file is a youtube object, an object type specific to the pytube package. You can use this to get the resolution of the video before downloading it via:

        print(file.streams.get_highest_resolution().resolution)
        

        Then just assign this to a variable and use that to set the resolution of the download:

        res = file.streams.get_highest_resolution().resolution
        if int(res.replace("p", "")) > 1080:
            res = 1080
        

        the replace part ist just so that the p is removed from the resolution string e.g. 720p and can be compared as an int for the condition. This way you download at a max of 1080p

        【讨论】:

        • print(file.streams.get_highest_resolution().resolution what does the last resolution do? The one after the dot. and how does this work if int(res.replace("p", "")) > 1080:?
        • I got that part from another stackoverflow post about how to get resolution using pytube: stackoverflow.com/questions/65827515/…
        【解决方案4】:

        Although I find @rdas 's solution to likely be more elegant, a simple solution is as follows:

        yt = YouTube(url)
        
        # selects for highest resolution stream not > set resolution ceiling from an ordered resolution list
        def select_best_res(yt):
            stream_list = yt.streams.filter(type="video", mime_type="video/mp4").order_by(
            "resolution"
            )
            res_ceiling = 1080  # maximum acceptable resolution (e.g. 2160, 1440, 1080, 720, 480, 360, 240)
        
            # selector for last stream (and therefore should be highest resolution) in ordered resolution list
            downward_moving_resolution_selector = -1
            while int(
                stream_list[downward_moving_resolution_selector].resolution.rstrip("p")
            ) > int(res_ceiling):
                    downward_moving_resolution_selector -= 1
            best_res_stream = stream_list[downward_moving_resolution_selector]
            # print(
            #     f"The highest resolution stream that is not greater than {res_ceiling}p for {best_res_stream.title} is: 
        ",
            #     best_res_stream,
            # )
            return best_res_stream
        
        select_best_res(yt)
        

        【讨论】:

          猜你喜欢
          • 2022-08-27
          • 2022-12-28
          • 2022-12-26
          • 2023-02-25
          • 2022-12-02
          • 2023-02-24
          • 2022-12-02
          • 2022-12-26
          • 2022-12-02
          相关资源
          最近更新 更多