| 1 |
#!/usr/bin/env python |
| 2 |
# gst-python |
| 3 |
# Copyright (C) 2006 Andy Wingo <wingo at pobox.com> |
| 4 |
# Copyright (C) 2023 Jan Schmidt <jan at centricular.com> |
| 5 |
# |
| 6 |
# This library is free software; you can redistribute it and/or |
| 7 |
# modify it under the terms of the GNU Library General Public |
| 8 |
# License as published by the Free Software Foundation; either |
| 9 |
# version 2 of the License, or (at your option) any later version. |
| 10 |
# |
| 11 |
# This library is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
# Library General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU Library General Public |
| 17 |
# License along with this library; if not, write to the |
| 18 |
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 19 |
# Boston, MA 02110-1301, USA. |
| 20 |
import os |
| 21 |
import sys |
| 22 |
import gi |
| 23 |
|
| 24 |
gi.require_version("GLib", "2.0") |
| 25 |
gi.require_version("Gst", "1.0") |
| 26 |
gi.require_version("GstPbutils", "1.0") |
| 27 |
from gi.repository import GLib, Gst, GstPbutils |
| 28 |
|
| 29 |
def fail(path, info): |
| 30 |
error = info.get_result() |
| 31 |
print(f"Discovery of {path} failed with result {error}") |
| 32 |
sys.exit(1) |
| 33 |
|
| 34 |
def print_stream_info(stream_info, depth): |
| 35 |
indent = " "*depth |
| 36 |
|
| 37 |
stream_type = stream_info.get_stream_type_nick() |
| 38 |
stream_num = stream_info.get_stream_number() |
| 39 |
stream_id = stream_info.get_stream_id() |
| 40 |
caps = stream_info.get_caps() |
| 41 |
print(f"{indent}{stream_type} #{stream_num}: {caps}") |
| 42 |
print(f"{indent} Stream ID: {stream_id}") |
| 43 |
|
| 44 |
tags = stream_info.get_tags() |
| 45 |
if tags is not None: |
| 46 |
print(f"{indent} Tags: ") |
| 47 |
for (key, tag) in tags.enumerate(): |
| 48 |
print(f"{indent} {key}: {tag}") |
| 49 |
|
| 50 |
if isinstance(stream_info, GstPbutils.DiscovererVideoInfo) or isinstance(stream_info, GstPbutils.DiscovererAudioInfo): |
| 51 |
print(f"{indent}Bitrate: {stream_info.get_bitrate()}") |
| 52 |
print(f"{indent}Max Bitrate: {stream_info.get_max_bitrate()}") |
| 53 |
print() |
| 54 |
|
| 55 |
def print_topology(stream_info, depth): |
| 56 |
print_stream_info(stream_info, depth) |
| 57 |
|
| 58 |
next = stream_info.get_next() |
| 59 |
if next is not None: |
| 60 |
print_topology (next, depth + 2); |
| 61 |
elif isinstance(stream_info, GstPbutils.DiscovererContainerInfo): |
| 62 |
streams = stream_info.get_streams() |
| 63 |
for tmpinf in streams: |
| 64 |
print_topology (tmpinf, depth + 2); |
| 65 |
|
| 66 |
def succeed(info): |
| 67 |
print(f"URI: {info.get_uri()}") |
| 68 |
# Print properties |
| 69 |
duration = info.get_duration() |
| 70 |
seekable = info.get_seekable() |
| 71 |
is_live = info.get_live() |
| 72 |
|
| 73 |
print(f"Duration: {Gst.TIME_ARGS(duration)}\nSeekable: {seekable}\nLive: {is_live}") |
| 74 |
|
| 75 |
print_topology(info.get_stream_info(), 0) |
| 76 |
sys.exit(0) |
| 77 |
|
| 78 |
def discover(path): |
| 79 |
def discovered(d, info, err): |
| 80 |
if info.get_result() == GstPbutils.DiscovererResult.OK: |
| 81 |
succeed(info) |
| 82 |
else: |
| 83 |
fail(path, info) |
| 84 |
|
| 85 |
d = GstPbutils.Discoverer.new(5 * Gst.SECOND) |
| 86 |
d.connect('discovered', discovered) |
| 87 |
d.start() |
| 88 |
d.discover_uri_async(path) |
| 89 |
GLib.MainLoop().run() |
| 90 |
|
| 91 |
def usage(): |
| 92 |
print("usage: gst-discover PATH-TO-MEDIA-FILE", file=sys.stderr) |
| 93 |
sys.exit(1) |
| 94 |
|
| 95 |
def main(argv): |
| 96 |
if len(argv) != 2: |
| 97 |
usage() |
| 98 |
path = argv.pop() |
| 99 |
if not os.path.isfile(path): |
| 100 |
print(f"error: file {path} does not exist", file=sys.stderr) |
| 101 |
usage() |
| 102 |
|
| 103 |
Gst.init(sys.argv) |
| 104 |
return discover('file://' + os.path.abspath(path)) |
| 105 |
|
| 106 |
if __name__ == '__main__': |
| 107 |
sys.exit(main(sys.argv)) |