File: filesrc.py

package info (click to toggle)
gst-python1.0 1.26.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 1,000 kB
  • sloc: python: 8,030; ansic: 1,869; makefile: 33
file content (116 lines) | stat: -rwxr-xr-x 3,339 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4

# GStreamer python bindings
# Copyright (C) 2002 David I. Lehn <dlehn@users.sourceforge.net>
#               2004 Johan Dahlin  <johan@gnome.org>
#
# filesrc.py: implements a file source element completely in python
#
# 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 gi
import sys

gi.require_version('Gst', '1.0')
gi.require_version('GstBase', '1.0')
from gi.repository import GLib, Gst, GObject, GstBase


if __name__ == '__main__':
    Gst.init(None)
Gst.init_python()


class FileSource(GstBase.BaseSrc):
    __gstmetadata__ = ('PyFileSource', 'Source',
                       'Custom Python filesrc example', 'Jan Schmidt')

    __gsttemplates__ = (
        Gst.PadTemplate.new("src",
                            Gst.PadDirection.SRC,
                            Gst.PadPresence.ALWAYS,
                            Gst.Caps.new_any()),
    )

    blocksize = 4096
    fd = None

    def __init__(self, name):
        super().__init__()
        self.curoffset = 0
        self.set_name(name)

    def set_property(self, name, value):
        if name == 'location':
            self.fd = open(value, 'rb')

    def do_create(self, offset, size, wot):
        if offset != self.curoffset:
            self.fd.seek(offset, 0)
        data = self.fd.read(self.blocksize)
        if data:
            self.curoffset += len(data)
            return Gst.FlowReturn.OK, Gst.Buffer.new_wrapped(data)
        else:
            return Gst.FlowReturn.EOS, None


GObject.type_register(FileSource)
__gstelementfactory__ = ("filesrc_py", Gst.Rank.NONE, FileSource)


def main(args):
    Gst.init()

    if len(args) != 3:
        print(f'Usage: {args[0]} input output')
        return -1

    bin = Gst.Pipeline('pipeline')

    filesrc = FileSource('filesource')
    assert filesrc
    filesrc.set_property('location', args[1])

    filesink = Gst.ElementFactory.make('filesink', 'sink')
    filesink.set_property('location', args[2])

    bin.add(filesrc, filesink)
    Gst.Element.link_many(filesrc, filesink)

    bin.set_state(Gst.State.PLAYING)
    mainloop = GLib.MainLoop()

    def bus_event(bus, message):
        t = message.type
        if t == Gst.MessageType.EOS:
            mainloop.quit()
        elif t == Gst.MessageType.ERROR:
            err, debug = message.parse_error()
            print(f"Error: {err}, {debug}")
            mainloop.quit()
        return True
    bus = bin.get_bus()
    bus.add_signal_watch()
    bus.connect('message', bus_event)

    mainloop.run()
    bin.set_state(Gst.State.NULL)


if __name__ == '__main__':
    sys.exit(main(sys.argv))