From 4e76f01ecc4c55a8dfd923a44bebd671907f944b Mon Sep 17 00:00:00 2001 From: jaseg Date: Mon, 22 Jan 2024 11:52:42 +0100 Subject: mpv.py: Add play_bytes convenience function --- mpv.py | 11 +++++++++++ tests/test_mpv.py | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/mpv.py b/mpv.py index 19f3581..1efdeb1 100644 --- a/mpv.py +++ b/mpv.py @@ -1981,6 +1981,17 @@ class MPV(object): 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). diff --git a/tests/test_mpv.py b/tests/test_mpv.py index 9cc688d..0935a32 100755 --- a/tests/test_mpv.py +++ b/tests/test_mpv.py @@ -664,6 +664,24 @@ class TestStreams(unittest.TestCase): 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): -- cgit