使用 brew 和 libgio-2.0.0.dylib 解决环境冲突

Solving environment conflict with brew and libgio-2.0.0.dylib

提问人:Nick Skywalker 提问时间:9/19/2023 更新时间:9/19/2023 访问量:43

问:

我在 mac 上运行 python gstreamer 应用程序,但我遇到了库冲突。这是应用程序:

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gst', '1.0')
from gi.repository import Gtk, Gst, Gdk

Gst.init(None)
Gtk.init(None)

class VideoPlayer:
    def __init__(self):
        self.window = Gtk.Window()
        self.window.set_default_size(640, 480)
        self.window.connect("delete-event", Gtk.main_quit)

        self.player = Gst.ElementFactory.make("playbin")
        self.playing = False

        self.setup_ui()
        self.setup_pipeline()

    def setup_ui(self):
        self.video_widget = Gtk.DrawingArea()
        self.video_widget.set_size_request(640, 360)

        self.play_button = Gtk.Button(label="Play")
        self.play_button.connect("clicked", self.toggle_play)

        self.repeat_button = Gtk.Button(label="Repeat")
        self.repeat_button.connect("clicked", self.toggle_repeat)

        self.slider = Gtk.Scale(orientation=Gtk.Orientation.HORIZONTAL)
        self.slider.set_range(0, 100)
        self.slider.connect("value-changed", self.seek)

        self.file_chooser = Gtk.FileChooserButton(title="Open Video")
        self.file_chooser.connect("file-set", self.load_file)

        self.grid = Gtk.Grid()
        self.grid.attach(self.video_widget, 0, 0, 4, 1)
        self.grid.attach(self.play_button, 0, 1, 1, 1)
        self.grid.attach(self.repeat_button, 1, 1, 1, 1)
        self.grid.attach(self.slider, 2, 1, 1, 1)
        self.grid.attach(self.file_chooser, 0, 2, 4, 1)

        self.window.add(self.grid)
        self.window.show_all()

    def setup_pipeline(self):
        self.bus = self.player.get_bus()
        self.bus.add_signal_watch()
        self.bus.connect("message", self.on_message)

    def toggle_play(self, button):
        if self.playing:
            self.player.set_state(Gst.State.PAUSED)
            self.play_button.set_label("Play")
        else:
            self.player.set_state(Gst.State.PLAYING)
            self.play_button.set_label("Pause")
        self.playing = not self.playing

    def toggle_repeat(self, button):
        pass  # Implement repeat logic if needed

    def load_file(self, button):
        dialog = Gtk.FileChooserDialog("Open Video File", None, Gtk.FileChooserAction.OPEN,
                                       (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                                        Gtk.STOCK_OPEN, Gtk.ResponseType.OK))

        filter_mp4 = Gtk.FileFilter()
        filter_mp4.set_name("MP4 files")
        filter_mp4.add_mime_type("video/mp4")
        dialog.add_filter(filter_mp4)

        response = dialog.run()
        if response == Gtk.ResponseType.OK:
            uri = dialog.get_uri()
            self.player.set_property("uri", uri)
            self.play_button.set_label("Play")
            self.playing = False
            self.slider.set_value(0)
        dialog.destroy()

    def seek(self, slider):
        if not self.playing:
            return

        value = slider.get_value()
        seek_time = int(value * self.player.query_duration(Gst.Format.TIME)[1] / 100)
        self.player.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH, seek_time)

    def on_message(self, bus, message):
        if message.type == Gst.MessageType.EOS:
            self.play_button.set_label("Play")
            self.playing = False
        elif message.type == Gst.MessageType.ERROR:
            err, debug = message.parse_error()
            print(f"Error: {err}, Debug: {debug}")

if __name__ == '__main__':
    player = VideoPlayer()
    Gtk.main()

和错误:

Traceback (most recent call last):
  File "/Users/username/app_example/gplayer2.py", line 1, in <module>
    import gi
  File "/opt/homebrew/Caskroom/miniforge/base/envs/switch/lib/python3.11/site-packages/gi/__init__.py", line 40, in <module>
    from . import _gi
ImportError: dlopen(/opt/homebrew/Caskroom/miniforge/base/envs/switch/lib/python3.11/site-packages/gi/_gi.cpython-311-darwin.so, 0x0002): Library not loaded: /opt/homebrew/opt/glib/lib/libgio-2.0.0.dylib
  Referenced from: <15BE39D1-C102-3CA5-9F72-4B518DC6E3B7> /opt/homebrew/Cellar/gobject-introspection/1.78.1/lib/libgirepository-1.0.1.dylib
  Reason: tried: '/opt/homebrew/opt/glib/lib/libgio-2.0.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/opt/glib/lib/libgio-2.0.0.dylib' (no such file), '/opt/homebrew/opt/glib/lib/libgio-2.0.0.dylib' (no such file), '/usr/local/lib/libgio-2.0.0.dylib' (no such file), '/usr/lib/libgio-2.0.0.dylib' (no such file, not in dyld cache), '/opt/homebrew/Cellar/glib/2.78.0/lib/libgio-2.0.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/Cellar/glib/2.78.0/lib/libgio-2.0.0.dylib' (no such file), '/opt/homebrew/Cellar/glib/2.78.0/lib/libgio-2.0.0.dylib' (no such file), '/usr/local/lib/libgio-2.0.0.dylib' (no such file), '/usr/lib/libgio-2.0.0.dylib' (no such file, not in dyld cache)

我试过了:

$ mv /opt/homebrew/Caskroom/miniforge/base/envs/switch/lib/libgio-2.0.0.dylib /opt/homebrew/Caskroom/miniforge/base/envs/switch/lib/libgio-2.0.0.dylib.backup
$ python gplayer2.py
Traceback (most recent call last):
  File "/Users/nicholasscottodiperto/work/SPOC-SW/demos/streaming/gplayer2.py", line 1, in <module>
    import gi
  File "/opt/homebrew/Caskroom/miniforge/base/envs/switch/lib/python3.11/site-packages/gi/__init__.py", line 40, in <module>
    from . import _gi
ImportError: dlopen(/opt/homebrew/Caskroom/miniforge/base/envs/switch/lib/python3.11/site-packages/gi/_gi.cpython-311-darwin.so, 0x0002): Library not loaded: @rpath/libgio-2.0.0.dylib
  Referenced from: <35AF13C3-C9FA-322E-81D8-92F96EFB3CC0> /opt/homebrew/Caskroom/miniforge/base/envs/switch/lib/python3.11/site-packages/gi/_gi.cpython-311-darwin.so
  Reason: tried: '/opt/homebrew/Caskroom/miniforge/base/envs/switch/lib/libgio-2.0.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/Caskroom/miniforge/base/envs/switch/lib/libgio-2.0.0.dylib' (no such file), '/opt/homebrew/Caskroom/miniforge/base/envs/switch/lib/libgio-2.0.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/Caskroom/miniforge/base/envs/switch/lib/libgio-2.0.0.dylib' (no such file), '/opt/homebrew/Caskroom/miniforge/base/envs/switch/lib/libgio-2.0.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/Caskroom/miniforge/base/envs/switch/lib/libgio-2.0.0.dylib' (no such file), '/opt/homebrew/Caskroom/miniforge/base/envs/switch/lib/libgio-2.0.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/Caskroom/miniforge/base/envs/switch/lib/libgio-2.0.0.dylib' (no such file), '/opt/homebrew/Caskroom/miniforge/base/envs/switch/bin/../lib/libgio-2.0.0.dylib' (no such file), '/opt/homebrew/Caskroom/miniforge/base/envs/switch/bin/../lib/libgio-2.0.0.dylib' (no such file), '/usr/local/lib/libgio-2.0.0.dylib' (no such file), '/usr/lib/libgio-2.0.0.dylib' (no such file, not in dyld cache)

$ mv /opt/homebrew/Cellar/glib/2.78.0/lib/libgio-2.0.0.dylib /opt/homebrew/Cellar/glib/2.78.0/lib/libgio-2.0.0.dylib.backup
$ python gplayer2.py
Traceback (most recent call last):
  File "/Users/nicholasscottodiperto/work/SPOC-SW/demos/streaming/gplayer2.py", line 1, in <module>
    import gi
  File "/opt/homebrew/Caskroom/miniforge/base/envs/switch/lib/python3.11/site-packages/gi/__init__.py", line 40, in <module>
    from . import _gi
ImportError: dlopen(/opt/homebrew/Caskroom/miniforge/base/envs/switch/lib/python3.11/site-packages/gi/_gi.cpython-311-darwin.so, 0x0002): Library not loaded: /opt/homebrew/opt/glib/lib/libgio-2.0.0.dylib
  Referenced from: <15BE39D1-C102-3CA5-9F72-4B518DC6E3B7> /opt/homebrew/Cellar/gobject-introspection/1.78.1/lib/libgirepository-1.0.1.dylib
  Reason: tried: '/opt/homebrew/opt/glib/lib/libgio-2.0.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/opt/glib/lib/libgio-2.0.0.dylib' (no such file), '/opt/homebrew/opt/glib/lib/libgio-2.0.0.dylib' (no such file), '/usr/local/lib/libgio-2.0.0.dylib' (no such file), '/usr/lib/libgio-2.0.0.dylib' (no such file, not in dyld cache), '/opt/homebrew/Cellar/glib/2.78.0/lib/libgio-2.0.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/Cellar/glib/2.78.0/lib/libgio-2.0.0.dylib' (no such file), '/opt/homebrew/Cellar/glib/2.78.0/lib/libgio-2.0.0.dylib' (no such file), '/usr/local/lib/libgio-2.0.0.dylib' (no such file), '/usr/lib/libgio-2.0.0.dylib' (no such file, not in dyld cache)

如何修复我的环境?

python gtk 自制 gstreamer glib

评论


答: 暂无答案