From f46831a51d20ddd0c7aeb19dfd3a6d958f86938a Mon Sep 17 00:00:00 2001
From: Jan Schmidt <jan@centricular.com>
Date: Tue, 15 Aug 2023 17:41:49 +1000
Subject: [PATCH] python: Add a GstDiscoverer example

Port the old gst-discover example to 1.0

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/5187>
---
 subprojects/gst-python/examples/gst-discover  | 107 ++++++++++++++++++
 .../gst-python/old_examples/gst-discover      |  89 ---------------
 2 files changed, 107 insertions(+), 89 deletions(-)
 create mode 100755 subprojects/gst-python/examples/gst-discover
 delete mode 100755 subprojects/gst-python/old_examples/gst-discover

diff --git a/subprojects/gst-python/examples/gst-discover b/subprojects/gst-python/examples/gst-discover
new file mode 100755
index 0000000000..558d4e9740
--- /dev/null
+++ b/subprojects/gst-python/examples/gst-discover
@@ -0,0 +1,107 @@
+#!/usr/bin/env python
+# gst-python
+# Copyright (C) 2006 Andy Wingo <wingo at pobox.com>
+# Copyright (C) 2023 Jan Schmidt <jan at centricular.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Library General Public
+# License as published by the Free Software Foundation; either
+# version 2 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Library General Public License for more details.
+#
+# You should have received a copy of the GNU Library General Public
+# License along with this library; if not, write to the
+# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+# Boston, MA 02110-1301, USA.
+import os
+import sys
+import gi
+
+gi.require_version("GLib", "2.0")
+gi.require_version("Gst", "1.0")
+gi.require_version("GstPbutils", "1.0")
+from gi.repository import GLib, Gst, GstPbutils
+
+def fail(path, info):
+    error = info.get_result()
+    print(f"Discovery of {path} failed with result {error}")
+    sys.exit(1)
+
+def print_stream_info(stream_info, depth):
+    indent = " "*depth
+
+    stream_type = stream_info.get_stream_type_nick()
+    stream_num = stream_info.get_stream_number()
+    stream_id = stream_info.get_stream_id()
+    caps = stream_info.get_caps()
+    print(f"{indent}{stream_type} #{stream_num}: {caps}")
+    print(f"{indent}  Stream ID: {stream_id}")
+
+    tags = stream_info.get_tags()
+    if tags is not None:
+        print(f"{indent}  Tags: ")
+        for (key, tag) in tags.enumerate():
+            print(f"{indent}    {key}: {tag}")
+
+    if isinstance(stream_info, GstPbutils.DiscovererVideoInfo) or isinstance(stream_info, GstPbutils.DiscovererAudioInfo):
+        print(f"{indent}Bitrate: {stream_info.get_bitrate()}")
+        print(f"{indent}Max Bitrate: {stream_info.get_max_bitrate()}")
+    print()
+
+def print_topology(stream_info, depth):
+    print_stream_info(stream_info, depth)
+    
+    next = stream_info.get_next()
+    if next is not None:
+        print_topology (next, depth + 2);
+    elif isinstance(stream_info, GstPbutils.DiscovererContainerInfo):
+      streams = stream_info.get_streams()
+      for tmpinf in streams:
+        print_topology (tmpinf, depth + 2);
+
+def succeed(info):
+    print(f"URI: {info.get_uri()}")
+    # Print properties
+    duration = info.get_duration()
+    seekable = info.get_seekable()
+    is_live = info.get_live()
+
+    print(f"Duration: {Gst.TIME_ARGS(duration)}\nSeekable: {seekable}\nLive: {is_live}")
+
+    print_topology(info.get_stream_info(), 0)
+    sys.exit(0)
+
+def discover(path):
+    def discovered(d, info, err):
+        if info.get_result() == GstPbutils.DiscovererResult.OK:
+            succeed(info)
+        else:
+            fail(path, info)
+
+    d = GstPbutils.Discoverer.new(5 * Gst.SECOND)
+    d.connect('discovered', discovered)
+    d.start()
+    d.discover_uri_async(path)
+    GLib.MainLoop().run()
+
+def usage():
+    print("usage: gst-discover PATH-TO-MEDIA-FILE", file=sys.stderr)
+    sys.exit(1)
+
+def main(argv):
+    if len(argv) != 2:
+        usage()
+    path = argv.pop()
+    if not os.path.isfile(path):
+        print(f"error: file {path} does not exist", file=sys.stderr)
+        usage()
+
+    Gst.init(sys.argv)
+    return discover('file://' + os.path.abspath(path))
+
+if __name__ == '__main__':
+    sys.exit(main(sys.argv))
diff --git a/subprojects/gst-python/old_examples/gst-discover b/subprojects/gst-python/old_examples/gst-discover
deleted file mode 100755
index 8b03966b49..0000000000
--- a/subprojects/gst-python/old_examples/gst-discover
+++ /dev/null
@@ -1,89 +0,0 @@
-#!/usr/bin/env python
-# gst-python
-# Copyright (C) 2006 Andy Wingo <wingo at pobox.com>
-#
-# This library is free software; you can redistribute it and/or
-# modify it under the terms of the GNU Library General Public
-# License as published by the Free Software Foundation; either
-# version 2 of the License, or (at your option) any later version.
-#
-# This library is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# Library General Public License for more details.
-#
-# You should have received a copy of the GNU Library General Public
-# License along with this library; if not, write to the
-# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-# Boston, MA 02110-1301, USA.
-
-
-import os
-import sys
-
-import pygtk
-pygtk.require('2.0')
-import gobject
-gobject.threads_init()
-import pygst
-pygst.require('0.10')
-import gst
-from gst.extend import discoverer
-
-def fail(path):
-    print "error: %r does not appear to be a media file" % path
-    sys.exit(1)
-
-def succeed(d):
-    def pp(prop, val):
-        print '%s: %s' % (prop, val)
-    pp('media type', d.mimetype)
-
-    pp('has video', d.is_video)
-    if d.is_video:
-        pp('video caps', d.videocaps)
-        pp('video width (pixels)', d.videowidth)
-        pp('video height (pixels)', d.videoheight)
-        pp('video length (hh:mm:ss)', gst.TIME_ARGS(d.videolength))
-        pp('framerate (fps)', '%s/%s' % (d.videorate.num, d.videorate.denom))
-
-    pp('has audio', d.is_audio)
-    if d.is_audio:
-        pp('audio caps', d.audiocaps)
-        pp('audio format', d.audiofloat and 'floating-point' or 'integer')
-        pp('sample rate (Hz)', d.audiorate)
-        pp('sample width (bits)', d.audiowidth)
-        pp('sample depth (bits)', d.audiodepth)
-        pp('audio length (hh:mm:ss)', gst.TIME_ARGS(d.audiolength))
-        pp('audio channels', d.audiochannels)
-
-    sys.exit(0)
-
-def discover(path):
-    def discovered(d, is_media):
-        if is_media:
-            succeed(d)
-        else:
-            fail(path)
-
-    d = discoverer.Discoverer(path)
-    d.connect('discovered', discovered)
-    d.discover()
-    gobject.MainLoop().run()
-
-def usage():
-    print >>sys.stderr, "usage: gst-discover PATH-TO-MEDIA-FILE"
-    sys.exit(1)
-
-def main(argv):
-    if len(argv) != 2:
-        usage()
-    path = argv.pop()
-    if not os.path.isfile(path):
-        print >>sys.stderr, "error: file %r does not exist" % path
-        usage()
-
-    return discover(path)
-
-if __name__ == '__main__':
-    sys.exit(main(sys.argv))