用gstreamer和gst-launch循环播放视频?

时间:2011-07-26 16:10:37

标签: linux video video-processing gstreamer gnonlin

我可以在gstreamer的gst-launch这样的命令行上播放视频:

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

这将在/tmp/myfile.mov中播放文件的前2秒,之后视频播放停止。无论如何要让它重复循环?即将2秒长的gnlfilesource变成一个无限长的视频,一次又一次地播放那些2秒?

5 个答案:

答案 0 :(得分:10)

如果使用gst-launch,那么您可能必须使用while true; do [your command]; done,正如Fredrik所述。但是,如果对C代码感兴趣,我编写了一个可能对您有所帮助的代码。在第一次运行流结束时从文件开头每2秒循环一次视频。

  //(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;
}

<强>更新 请参阅以下链接 http://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/chapter-dataaccess.html

[第19.1.2节。播放媒体文件的某个区域]。这可以与我的代码结合使用。

答案 1 :(得分:7)

这似乎可以使用multifilesrc插件,

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

似乎将在2011年6月加入。

答案 2 :(得分:3)

假设bash ......

将其包裹在while循环中?

while true; do [your command]; done

其中true没有任何成功,即

true: true
    Return a successful result.

    Exit Status:
    Always succeeds.

它允许您创建无限循环,例如

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

答案 3 :(得分:2)

#gstreamer IRC频道上的人说,你不能用gstreamer本身做这个,你需要在gstreamer管道之外的东西来循环它。

答案 4 :(得分:0)

它不是在gstreamer上的流中循环文件,但是我能够使用ffmpeg -stream_loop选项来实现。 https://ffmpeg.org/ffmpeg.html#Main-options

$ ffmpeg -re -stream_loop -1 -i /tmp/sample.mp4 -f rtsp rtsp://localhost:8554/stream