Looping a video with gstreamer and gst-launch?

26,621

Solution 1

Assuming bash...

Wrap it in a while-loop?

while true; do [your command]; done

where true does nothing sucessfully, i.e.

true: true
    Return a successful result.

    Exit Status:
    Always succeeds.

It allows you to create infinite loops, e.g.

$ while true; do echo "run..."; sleep 1; done
run...
run...
run...
run...
run...
...

Solution 2

If using gst-launch then you may have to use while true; do [your command]; done as Fredrik has stated. However if interested in C code, I have written a code which may help you. Looping of video every 2 seconds from the beginning of the file at the end of the stream of first run.

  //(c) 2011 enthusiasticgeek
  // This code 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.

#include <gst/gst.h>

gboolean bus_callback(GstBus *bus, GstMessage *msg, gpointer data)
{
    GstElement *play = GST_ELEMENT(data);
    switch (GST_MESSAGE_TYPE(msg))
    {
    case GST_MESSAGE_EOS:
        /* restart playback if at end */
        if (!gst_element_seek(play, 
                    1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
                    GST_SEEK_TYPE_SET,  2000000000, //2 seconds (in nanoseconds)
                    GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE)) {
            g_print("Seek failed!\n");
        }
        break;
    default:
        break;
    }
    return TRUE;
}

gint
main (gint   argc,
      gchar *argv[])
{
  GMainLoop *loop;
  GstElement *play;
  GstBus *bus;

  /* init GStreamer */
  gst_init (&argc, &argv);
  loop = g_main_loop_new (NULL, FALSE);

  /* make sure we have a URI */
  if (argc != 2) {
    g_print ("Usage: %s <URI>\n", argv[0]);
    return -1;
  }

  /* set up */
  play = gst_element_factory_make ("playbin", "play");
  g_object_set (G_OBJECT (play), "uri", argv[1], NULL);

  bus = gst_pipeline_get_bus (GST_PIPELINE (play));
  gst_bus_add_watch (bus, bus_callback, play);
  gst_object_unref (bus);

  gst_element_set_state (play, GST_STATE_PLAYING);

  /* now run */
  g_main_loop_run (loop);

  /* also clean up */
  gst_element_set_state (play, GST_STATE_NULL);
  gst_object_unref (GST_OBJECT (play));

  return 0;
}

Update: See the following link http://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/chapter-dataaccess.html

[Section 19.1.2. Play a region of a media file]. This could be used in conjugation with my code.

Solution 3

This seems to be possible with multifilesrc plugin,

gst-launch-1.0 multifilesrc location=alien-age.mpg loop=true ! decodebin ! autovideosink

Seems to be added back in June 2011.

Solution 4

According to folks on the #gstreamer IRC channel, you can't do this with gstreamer itself, you'd need something outside the gstreamer pipeline to loop it.

Solution 5

multifilesrc is the easiest way, but it won't work on media files that have "Media length" known. you can loop on any video files only if file does not have any information about the time or length.

Open your file with any media player, if it shows media length or if you can seek the file forward or backward, that means it knows the media length and multifilesrc won't loop it.

How to convert video file into file without time track (stream file) with GStreamer:

you need to run two pipelines on command line, first run the recorder:

gst-launch-1.0 udpsrc port=10600 ! application/x-rtp-stream ! rtpstreamdepay name=pay1 ! rtph264depay ! h264parse ! video/x-h264,alignment=nal ! filesink location=my_timeless_file.mp4

it starts and waits for incoming stream.

on another terminal run the play pipeline:

gst-launch-1.0 filesrc location=my_file_with_time_track ! queue ! decodebin ! videoconvert ! x264enc ! h264parse config-interval=-1 ! rtph264pay pt=96 ! rtpstreampay name=pay0 ! udpsink host=127.0.0.1 port=10600

play pipeline starts and eventually terminates when it streamed whole file, now go back to the first command line and terminate recording pipeline with Ctrl+C.

(instead of udpsrc/udpsink you can use any other mechanisms to make the stream, like appsrc/appsink)

Now you have a new file which can be used in multifilesrc with loop:

gst-launch-1.0 multifilesrc location=my_timeless_file.mp4 loop=true ! queue ! decodebin ! videoconvert ! ximagesink

Why multifilesrc does not loop files with known length?

Because when length of media is known it sends EOS message downstream and causes whole pipeline going to state NULL, by removing that information when it reaches end of file (byte stream) it tries to find next file to play (remember it is "multi" file source, and by default can accept wildcard location like "image_%d.png"). When there is no wildcard to point to the next file, it loops back to only known file.

Share:
26,621
Amandasaurus
Author by

Amandasaurus

I'm a Linux user

Updated on July 09, 2022

Comments

  • Amandasaurus
    Amandasaurus almost 2 years

    I am able to play a video on the command line with gstreamer's gst-launch like this:

    gst-launch gnlfilesource location=file:///tmp/myfile.mov start=0 duration=2000000000 ! autovideosink
    

    This plays the first 2 seconds of the file in /tmp/myfile.mov, afterwards the video playback stops. Is there anyway to get this to loop repeatidly? i.e. turn the 2 second long gnlfilesource into an infinite length video that plays those 2 seconds again and again and again?

  • Amandasaurus
    Amandasaurus almost 13 years
    Pedically that would work, but I want to do it within gstreamer so I can process this infinite loop later...
  • Fredrik Pihl
    Fredrik Pihl almost 13 years
    @Rory - sorry, can't help you there, googling "gst-launch looping video" returns some interesting matches...
  • Dejan
    Dejan over 12 years
    This fails when looping videos of variable lenghts inside a videomixer.
  • xryl669
    xryl669 over 8 years
    Interesting, however, the timestamp is not monotonically increasing with this solution.
  • cweiske
    cweiske about 4 years
    Does not loop on Ubuntu 18.04 when streaming the video as webcam in a browser with v4l2loopback.
  • Alexis Wilke
    Alexis Wilke almost 4 years
    How do you use that to actually play the gstreamer video?! When I start that binary, nothing happens...
  • Prakash Vanapalli
    Prakash Vanapalli over 3 years
    getting Connection to tcp://localhost:8554?timeout=0 failed (Connection refused), trying next address error when trying to work with this,
  • R.Bar
    R.Bar almost 3 years
    Does not work on both Ubuntu18.04 and Ubuntu20.04 for me as well
  • R.Bar
    R.Bar almost 3 years
    This looks more like a quick fix rather then a solution