brvh
brvh

Reputation: 296

GStreamer Python decodebin, jpegenc elements not linking

I'm starting out using GStreamer using the gst-python bindings. An example I'm working on is reading in an .mp4 file, encoding it in a MJPEG stream and saving it in an .avi container. The pipeline I've built for this is:

gst-launch-1.0 filesrc location=./my_movie.mp4 ! decodebin ! jpegenc ! avimux ! filesink location=./encoded_movie.avi

Which works fine. I can play the encoded_movie.avi using VLC Media Player.

I've written a Python script trying to build that pipeline with the following code:

import sys
# Gstreamer
import gi 
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject, GLib

# initialize GStreamer
Gst.init(None)
# This does some things:
# 1. It initializes all internal structures
# 2. It checks what plugins are available.
# 3. It executes any command-line option intended for GStreamer

# Build pipeline
source = Gst.ElementFactory.make('filesrc', 'source')
decoder = Gst.ElementFactory.make('decodebin', 'decoder')
encoder = Gst.ElementFactory.make('jpegenc', 'encoder')
avi = Gst.ElementFactory.make('avimux', 'avimux')
sink = Gst.ElementFactory.make('filesink', 'sink')


# Create empty pipeline
pipeline = Gst.Pipeline.new('test-pipeline')

if (not pipeline or not source or not decoder or not sink or not encoder or 
  not avi):
  print('ERROR: could not init pipeline')
  sys.exit(1)

# build the pipeline
pipeline.add(source)
pipeline.add(decoder)
pipeline.add(encoder)
pipeline.add(avi)
pipeline.add(sink)

print('Added all sources')

if not source.link(decoder):
  print('ERROR: Could not link source to decoder')
  sys.exit(1)

if not decoder.link(encoder):
  print('ERROR: Could not link decoder to ' + encoder.get_property('name'))
  sys.exit(1)

if not encoder.link(avi):
  print('ERROR: Could not link ' + encoder.get_property('name') + ' with ' + 
    avi.get_property('name'))
  sys.exit(1)

if not avi.link(sink):
  print('ERROR: Could not link ' + avi.get_property('name') + ' with ' + 
    sink.get_property('name'))

print('linked all sources')
# modify source and sink properties
source.set_property('location', './my_movie.mp4')
print(source.get_property('location'))
sink.set_property('location', './encoded_movie.avi')
print(sink.get_property('location'))

# Start playing
try:
  # start playing
  ret = pipeline.set_state(Gst.State.PLAYING)
  if ret == Gst.StateChangeReturn.FAILURE:
    print("ERROR: Unable to set the pipeline to the playing state")
  else:
    print('Pipeline started')

  # wait for EOS or error
  bus = pipeline.get_bus()
  msg = bus.timed_pop_filtered(
    Gst.CLOCK_TIME_NONE,
    Gst.MessageType.ERROR | Gst.MessageType.EOS
  )
  # Error handling
  if msg:
    t = msg.type 
    if t == Gst.MessageType.ERROR:
      err, dbg = msg.parse_error()
      print('ERROR:', msg.src.get_name(), '\n', err.message)
      if dbg:
          print('Debugging info:', dbg)
    elif t == Gst.MessageType.EOS:
      print('End-Of-Stream reached')
    print('Clean up pipeline')
    pipeline.set_state(Gst.State.NULL)
except KeyboardInterrupt:
  # Free resources and exit
  pipeline.set_state(Gst.State.NULL)
  sys.exit()       
finally:
  pipeline.set_state(Gst.State.NULL)

I am getting one of my own errors as the output is:

Added all sources
ERROR: Could not link decoder to encoder

I'm wondering why the elements can't be linked using the bindings? Since the pipeline does work on my own host.

The pipeline with gst-launch-1.0 runs on macOS High Sierra version 10.13.4 using GStreamer 1.0.

The Python script is running on in a Docker container running Ubuntu v. 17.10, GStreamer 1.0, Python3.6

Upvotes: 3

Views: 2309

Answers (1)

Algis
Algis

Reputation: 332

The decodebin element uses dynamic pads. You have to select pads using 'pad-added' signal.

Example

def decodebin_pad_added(self, element, pad):
    string = pad.query_caps(None).to_string()
    print('Found stream: %s' % string)
    if string.startswith('video/x-raw'):
        pad.link(encoder.get_static_pad('sink'))


decoder.connect("pad-added", decodebin_pad_added)

Upvotes: 5

Related Questions