Skip to content

Commit

Permalink
mpv.py: Add play_bytes convenience function
Browse files Browse the repository at this point in the history
  • Loading branch information
jaseg committed Jan 22, 2024
1 parent 141ec7d commit 4e76f01
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions mpv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,17 @@ def write(chunk):
yield write
q.put(EOF)

def play_bytes(self, data):
frame = sys._getframe()
stream_name = f'__python_mpv_play_generator_{hash(frame)}'

@self.python_stream(stream_name)
def reader():
yield data
reader.unregister() # unregister itself

self.play(f'python://{stream_name}')

def python_stream_catchall(self, cb):
""" Register a catch-all python stream to be called when no name matches can be found. Use this decorator on a
function that takes a name argument and returns a (generator, size) tuple (with size being None if unknown).
Expand Down
18 changes: 18 additions & 0 deletions tests/test_mpv.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,24 @@ def cb(evt):
m.terminate()
disp.stop()

def test_play_bytes(self):
handler = mock.Mock()

disp = Display()
disp.start()
m = mpv.MPV(vo=testvo)
def cb(evt):
handler(evt.as_dict(decoder=mpv.lazy_decoder))
m.register_event_callback(cb)

with open(TESTVID, 'rb') as f:
m.play_bytes(f.read())

m.wait_for_playback()
handler.assert_any_call({'event': 'end-file', 'reason': 'eof', 'playlist_entry_id': 1})
m.terminate()
disp.stop()


class TestLifecycle(unittest.TestCase):
def test_create_destroy(self):
Expand Down

0 comments on commit 4e76f01

Please sign in to comment.