cleanup stream init
Browse files- pytube/streams.py +18 -39
pytube/streams.py
CHANGED
@@ -12,7 +12,7 @@ import io
|
|
12 |
import logging
|
13 |
import os
|
14 |
import pprint
|
15 |
-
from typing import Dict, Tuple, Optional
|
16 |
|
17 |
from pytube import extract
|
18 |
from pytube import request
|
@@ -44,39 +44,11 @@ class Stream:
|
|
44 |
|
45 |
self.url = stream["url"] # signed download url
|
46 |
self.itag = int(stream["itag"]) # stream format id (youtube nomenclature)
|
47 |
-
self.type = stream[
|
48 |
-
"type"
|
49 |
-
] # the part of the mime before the slash, overwritten later
|
50 |
|
51 |
-
|
52 |
-
self.fps = None # frames per second (video streams only)
|
53 |
-
self.res = None # resolution (e.g.: 480p, 720p, 1080p)
|
54 |
-
|
55 |
-
self._filesize: Optional[int] = None # filesize in bytes
|
56 |
-
self.mime_type = None # content identifier (e.g.: video/mp4)
|
57 |
-
self.subtype = None # the part of the mime after the slash
|
58 |
-
|
59 |
-
self.codecs: List[str] = [] # audio/video encoders (e.g.: vp8, mp4a)
|
60 |
-
self.audio_codec = None # audio codec of the stream (e.g.: vorbis)
|
61 |
-
self.video_codec = None # video codec of the stream (e.g.: vp8)
|
62 |
-
self.is_dash: Optional[bool] = None
|
63 |
-
|
64 |
-
# Iterates over the key/values of stream and sets them as class
|
65 |
-
# attributes. This is an anti-pattern and should be removed.
|
66 |
-
self.set_attributes_from_dict(stream)
|
67 |
-
|
68 |
-
# Additional information about the stream format, such as resolution,
|
69 |
-
# frame rate, and whether the stream is live (HLS) or 3D.
|
70 |
-
self.fmt_profile: Dict = get_format_profile(self.itag)
|
71 |
-
|
72 |
-
# Same as above, except for the format profile attributes.
|
73 |
-
self.set_attributes_from_dict(self.fmt_profile)
|
74 |
-
|
75 |
-
# The player configuration, contains info like the video title.
|
76 |
-
self.player_config_args = player_config_args
|
77 |
|
78 |
# 'video/webm; codecs="vp8, vorbis"' -> 'video/webm', ['vp8', 'vorbis']
|
79 |
-
self.mime_type, self.codecs = extract.mime_type_codec(
|
80 |
|
81 |
# 'video/webm' -> 'video', 'webm'
|
82 |
self.type, self.subtype = self.mime_type.split("/")
|
@@ -85,13 +57,21 @@ class Stream:
|
|
85 |
# streams return NoneType for audio/video depending.
|
86 |
self.video_codec, self.audio_codec = self.parse_codecs()
|
87 |
|
88 |
-
|
89 |
-
"""Set class attributes from dictionary items.
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
@property
|
97 |
def is_adaptive(self) -> bool:
|
@@ -127,7 +107,7 @@ class Stream:
|
|
127 |
"""
|
128 |
return self.is_progressive or self.type == "video"
|
129 |
|
130 |
-
def parse_codecs(self) -> Tuple:
|
131 |
"""Get the video/audio codecs from list of codecs.
|
132 |
|
133 |
Parse a variable length sized list of codecs and returns a
|
@@ -334,7 +314,6 @@ class Stream:
|
|
334 |
:returns:
|
335 |
A string representation of a :class:`Stream <Stream>` object.
|
336 |
"""
|
337 |
-
# TODO(nficano): this can probably be written better.
|
338 |
parts = ['itag="{s.itag}"', 'mime_type="{s.mime_type}"']
|
339 |
if self.includes_video_track:
|
340 |
parts.extend(['res="{s.resolution}"', 'fps="{s.fps}fps"'])
|
|
|
12 |
import logging
|
13 |
import os
|
14 |
import pprint
|
15 |
+
from typing import Dict, Tuple, Optional
|
16 |
|
17 |
from pytube import extract
|
18 |
from pytube import request
|
|
|
44 |
|
45 |
self.url = stream["url"] # signed download url
|
46 |
self.itag = int(stream["itag"]) # stream format id (youtube nomenclature)
|
|
|
|
|
|
|
47 |
|
48 |
+
# set type and codec info
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
# 'video/webm; codecs="vp8, vorbis"' -> 'video/webm', ['vp8', 'vorbis']
|
51 |
+
self.mime_type, self.codecs = extract.mime_type_codec(stream["type"])
|
52 |
|
53 |
# 'video/webm' -> 'video', 'webm'
|
54 |
self.type, self.subtype = self.mime_type.split("/")
|
|
|
57 |
# streams return NoneType for audio/video depending.
|
58 |
self.video_codec, self.audio_codec = self.parse_codecs()
|
59 |
|
60 |
+
self._filesize: Optional[int] = None # filesize in bytes
|
|
|
61 |
|
62 |
+
# Additional information about the stream format, such as resolution,
|
63 |
+
# frame rate, and whether the stream is live (HLS) or 3D.
|
64 |
+
itag_profile = get_format_profile(self.itag)
|
65 |
+
self.is_dash = itag_profile["is_dash"]
|
66 |
+
self.abr = itag_profile["abr"] # average bitrate (audio streams only)
|
67 |
+
self.fps = itag_profile["fps"] # frames per second (video streams only)
|
68 |
+
self.resolution = itag_profile["resolution"] # resolution (e.g.: "480p")
|
69 |
+
self.is_3d = itag_profile["is_3d"]
|
70 |
+
self.is_hdr = itag_profile["is_hdr"]
|
71 |
+
self.is_live = itag_profile["is_live"]
|
72 |
+
|
73 |
+
# The player configuration, contains info like the video title.
|
74 |
+
self.player_config_args = player_config_args
|
75 |
|
76 |
@property
|
77 |
def is_adaptive(self) -> bool:
|
|
|
107 |
"""
|
108 |
return self.is_progressive or self.type == "video"
|
109 |
|
110 |
+
def parse_codecs(self) -> Tuple[Optional[str], Optional[str]]:
|
111 |
"""Get the video/audio codecs from list of codecs.
|
112 |
|
113 |
Parse a variable length sized list of codecs and returns a
|
|
|
314 |
:returns:
|
315 |
A string representation of a :class:`Stream <Stream>` object.
|
316 |
"""
|
|
|
317 |
parts = ['itag="{s.itag}"', 'mime_type="{s.mime_type}"']
|
318 |
if self.includes_video_track:
|
319 |
parts.extend(['res="{s.resolution}"', 'fps="{s.fps}fps"'])
|