gstreamer将RTSP转jpg图片保存

简介: gstreamer将RTSP转jpg图片保存

命令行:

gst-launch-1.0 rtspsrc location="rtsp://admin:admin@127.0.0.1:554/h264/ch1/sub/av_stream" ! rtph264depay ! h264parse ! openh264dec ! videorate ! jpegenc ! multifilesink location="img_%06.jpg"

代码方式一:

void Test()
{
  GstElement *pipeline;
  GstBus *bus;
  GstMessage *msg;
  gst_init(NULL, NULL);
  pipeline =
    gst_parse_launch
    ("gst-launch-1.0 rtspsrc location = rtsp://admin:admin@127.0.0.1:554/h264/ch1/sub/av_stream !rtph264depay !h264parse !openh264dec !videorate !jpegenc !multifilesink location = img_%06.jpg", NULL);
  /* Start playing */
  GstStateChangeReturn res = gst_element_set_state(pipeline, GST_STATE_PLAYING);
  if (res == GST_STATE_CHANGE_FAILURE)
  {
    g_printerr("Unable to set the pipeline to the playing state.\n");
    gst_object_unref(pipeline);
    Sleep(1000);
    return;
  }
  bus = gst_element_get_bus(pipeline);
  msg =
    gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE,
      (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));
  if (msg != NULL)
    gst_message_unref(msg);
  gst_object_unref(bus);
  gst_element_set_state(pipeline, GST_STATE_NULL);
  gst_object_unref(pipeline);
  return;
}

代码方式二:

#include <gst/gst.h>
#include <Windows.h>
#include <iostream>
#include <thread>
GstMessage *msg;
GMainLoop  *_loop;
GstBus*     _bus;
GstElement* _pipeline;
GstElement* _source;
GstElement* _rtpbin;
GstElement* _depay;
GstElement* _parse;
GstElement* _dec;
GstElement* _rate;
GstElement* _enc;
GstElement* _capsfilter;
GstElement* _sink;
struct Exception : std::exception {};
template<typename T>
T* chk(T* pointer) {
  if (pointer == nullptr) {
    throw Exception();
  }
  return pointer;
}
void _onPadAdded(GstElement *src, GstPad *src_pad, gpointer user_data)
{
  GstPad* sink_pad = (GstPad*)user_data;
  gst_pad_link(src_pad, sink_pad);
}
void Init()
{
  gst_init(NULL, NULL);
  _pipeline = chk(gst_pipeline_new("pipline"));
  _source = chk(gst_element_factory_make("rtspsrc", "src"));
  _depay = chk(gst_element_factory_make("rtph264depay", "depay"));
  _parse = chk(gst_element_factory_make("h264parse", "parse"));
  _dec = chk(gst_element_factory_make("openh264dec", "dec"));
  _rate = chk(gst_element_factory_make("videorate", "rate"));
  _enc = chk(gst_element_factory_make("jpegenc", "enc"));
  _capsfilter = chk(gst_element_factory_make("capsfilter", "filter"));
  _sink = chk(gst_element_factory_make("multifilesink", "sink"));
  GstPad* depay_sink = gst_element_get_static_pad(_depay, "sink");
  GstCaps* depay_sink_caps = gst_caps_new_simple("application/x-rtp",
    //"format", G_TYPE_STRING, "rgb",
    //"width", G_TYPE_INT, 1920,
    //"height", G_TYPE_INT, 1080,
    "framerate", GST_TYPE_FRACTION, 1, 1,
     NULL);
  gst_pad_use_fixed_caps(depay_sink);
  gst_pad_set_caps(depay_sink, depay_sink_caps);
  gst_object_unref(depay_sink);
  g_object_set(_source, "latency", 0, NULL);
  g_object_set(_capsfilter, "caps-change-mode", 1, NULL);
  g_object_set(_sink, "location", "E:/pic/img_%d.jpg", NULL);
  gst_bin_add_many(GST_BIN(_pipeline), _source, _depay, _parse, _dec, _rate, _enc, _capsfilter, _sink, NULL);
  g_signal_connect(_source, "pad-added", G_CALLBACK(&_onPadAdded), gst_element_get_static_pad(_depay, "sink"));
  gboolean bsuccess = gst_element_link_many(_depay, _parse, _dec, _rate, _enc, _capsfilter, _sink, NULL);
  if (!bsuccess) {
    g_print("Failed to link one or more elements!\n");
    gst_element_unlink_many(_depay, _parse, _dec, _rate, _enc, _capsfilter, _sink, NULL);
    //Sleep(1000);
    //continue;
  }
  g_object_set(_source, "location", "rtsp://admin:admin@127.0.0.1:554/h264/ch1/sub/av_stream", NULL);
  while (1)
  {
    _bus = gst_element_get_bus(_pipeline);
    msg =
      gst_bus_timed_pop_filtered(_bus, GST_CLOCK_TIME_NONE,
      (GstMessageType)(GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS));
    if (msg != NULL)
    {
      GError *err;
      gchar *debug_info;
      switch (GST_MESSAGE_TYPE(msg))
      {
      case GST_MESSAGE_ERROR:
      {
        gst_message_parse_error(msg, &err, &debug_info);
        g_printerr("Error received from element %s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
        g_printerr("Debugging information: %s\n", debug_info ? debug_info : "none");
        g_clear_error(&err);
        g_free(debug_info);
        break;
      }
      break;
      case GST_MESSAGE_EOS:
      {
        g_print("End-Of-Streamreached.\n");
        break;
      }
      break;
      case GST_MESSAGE_STATE_CHANGED:
      {
        /* We are only interested in state-changed messages from the pipeline */
        if (GST_MESSAGE_SRC(msg) == GST_OBJECT(_pipeline))
        {
          GstState old_state, new_state, pending_state;
          gst_message_parse_state_changed(msg,
            &old_state,
            &new_state,
            &pending_state);
          g_print("Pipeline state changed from %s to %s:\n",
            gst_element_state_get_name(old_state),
            gst_element_state_get_name(new_state));
          char buf[216] = { 0 };
          sprintf(buf, "Pipeline state changed from %s to %s:\n",
            gst_element_state_get_name(old_state),
            gst_element_state_get_name(new_state));
          if (pending_state == GST_STATE_NULL)
          {
            break;
          }
        }
      }
      break;
      default:
      {
        /* We shouldnot reach here */
        g_printerr("Unexpected message received.\n");
        break;
      }
      }
    }
  }
  if (msg != NULL)
    gst_message_unref(msg);
  gst_object_unref(_bus);
  gst_element_set_state(_pipeline, GST_STATE_NULL);
  gst_object_unref(_pipeline);
  return;
}
void PausePictureCapture()
{
  GstStateChangeReturn res = gst_element_set_state(_pipeline, GST_STATE_PAUSED);
  if (res == GST_STATE_CHANGE_FAILURE)
  {
    g_printerr("Unable to set the pipeline to the paused state.\n");
    gst_object_unref(_pipeline);
  }
}
void StartPictureCapture()
{
  GstStateChangeReturn res = gst_element_set_state(_pipeline, GST_STATE_PLAYING);
  if (res == GST_STATE_CHANGE_FAILURE)
  {
    g_printerr("Unable to set the pipeline to the playing state.\n");
    gst_object_unref(_pipeline);
  }
}
int main()
{
  std::thread([]() {
    Init();
  }).detach();
  Sleep(10000);
  for (int i = 0; i < 10; i++)
  {
    StartPictureCapture();
    Sleep(10000);
    PausePictureCapture();
    Sleep(10000);
  }
  getchar();
  return 0;
}
相关文章
|
存储 网络协议 网络安全
RTSP协议抓包及讲解(三)
RTSP协议抓包及讲解
657 1
|
开发工具
编译SDK出现aclocal-1.15: command not found错误
编译SDK出现aclocal-1.15: command not found错误
1446 0
|
API 数据处理 数据安全/隐私保护
curl基础用法
curl基础用法
|
监控 程序员 Linux
supervisor的正确离线安装和使用
supervisor的正确离线安装和使用
1017 0
|
监控 Java Linux
Jetson 学习笔记(十二):CSI摄像头实现rtsp流的传输并对动态获取多路流进行探索
本文是关于如何在Jetson设备上使用CSI摄像头实现RTSP流传输的详细教程,包括安装依赖、编译gst-rtsp-server、测试、源代码介绍以及如何动态获取多路流的RTSP服务器。
1918 2
Jetson 学习笔记(十二):CSI摄像头实现rtsp流的传输并对动态获取多路流进行探索
|
Linux 开发者 iOS开发
QT:基于QMediaPlayer制作的视频播放器(最下方有整合包,可直接运行)
QMediaPlayer是Qt多媒体模块中的一个核心类,它提供了播放音频和视频内容的功能。这个类的设计旨在简化跨平台的媒体播放,使得开发者能够在多种操作系统(如Linux、Windows、macOS及移动平台)上轻松集成多媒体播放能力到他们的应用中,而无需关心底层实现细节。以下是关于QMediaPlayer的一些关键点:
2347 1
|
12月前
|
存储 安全 测试技术
2025年云真机与购买真机成本对比及高性价比平台推荐
文章对比了2025年云真机与购买实体机的成本,指出云真机在设备灵活性、批量部署和长期使用成本上更具优势,适合中高频测试场景。还介绍了主流云真机平台特点,给出筛选高性价比平台的逻辑及常见问题解答,助力企业按需选择。
|
安全 Unix Linux
LD_PRELOAD劫持(超详细篇)
LD_PRELOAD劫持(超详细篇)
2693 1
|
存储 Ubuntu 计算机视觉
使用ros标定相机的内参和外参
使用ros标定相机的内参和外参
1714 2
|
自然语言处理 Linux C++
make和Cmake都有什么区别?(内附使用详解)
make: 是一个构建工具,它的任务是读取 Makefile 文件,并基于这些文件中的指令执行具体的构建操作。Makefile 文件包含了如何构建项目的规则,make 负责解析这些规则并执行必要的命令来编译和链接源代码,生成可执行文件或库。 CMake: 是一个构建系统生成器。它并不直接进行编译或链接,而是根据项目中 CMakeLists.txt 文件的内容生成一个或多个构建系统的描述文件(如 Makefile 或 Visual Studio 解决方案)。CMake 提供了一种更高级、更抽象的方式来描述构建过程,这使得它能够跨平台地生成各种构建系统。
2364 1