Skip to content

Commit 9d71a81

Browse files
add support for fetching loudness levels of audio streams (#1529)
* add support for fetching loudness levels of audio streams * Add test for levels * Apply suggestions from code review Co-authored-by: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> * move Level object to bottom of media.py * clean up whitespace issues --------- Co-authored-by: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com>
1 parent 93932d8 commit 9d71a81

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

plexapi/media.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,17 @@ def setSelected(self):
433433
"""
434434
return self._parent().setSelectedAudioStream(self)
435435

436+
def levels(self, subSample=128):
437+
""" Returns a list of :class:`~plexapi.media.Level` objects for this AudioStream.
438+
Only available for Tracks which have been analyzed for loudness.
439+
440+
Attributes:
441+
subSample (int): The number of loudness samples to return. Default 128.
442+
"""
443+
key = f'/library/streams/{self.id}/levels'
444+
params = {'subsample': subSample}
445+
return self.fetchItems(key, params=params)
446+
436447
@deprecated('Use "setSelected" instead.')
437448
def setDefault(self):
438449
return self.setSelected()
@@ -1343,3 +1354,15 @@ def _loadData(self, data):
13431354
self.quality = data.attrib.get('quality')
13441355
self.title = data.attrib.get('title')
13451356
self.url = data.attrib.get('url')
1357+
1358+
1359+
@utils.registerPlexObject
1360+
class Level(PlexObject):
1361+
""" Represents a single loudness Level sample for an AudioStream.
1362+
1363+
Attributes:
1364+
loudness (float): Loudness level value
1365+
"""
1366+
def _loadData(self, data):
1367+
""" Load attribute values from Plex XML response. """
1368+
self.loudness = utils.cast(float, data.attrib.get('v'))

tests/test_audio.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ def test_audio_Track_attrs(album):
397397
assert stream.lra is None
398398
assert stream.peak is None
399399
assert stream.startRamp is None
400+
if stream.loudness is not None:
401+
assert len(stream.levels(subSample=32)) == 32
400402

401403

402404
def test_audio_Track_album(album):

0 commit comments

Comments
 (0)