【GStreamer开发】GStreamer基础教程10——GStreamer工具

目标

      GStreamer提供了一系列方便使用的工具。这篇教程里不牵涉任何代码,但还是会讲一些有用的内容:

      如何在命令行下建立一个pipeline——完全不使用C

      如何找出一个element的Capabilities

      如何发现一个媒体文件的内部结构


介绍

      这些工具就位于SDK的bin目录下。你需要把这个目录加入PATH变量,或者把当前目前切换到GStreamer SDK的bin目录。

      打开一个终端界面,把当前目录切换到GStreamer SDK的bin目录,然后准备跟着我们操作吧。

      为了防止多个版本的GStreamer都安装导致的冲突,所有的工具都是有版本的,他们的名字后面跟着GStreamer的版本号。因为这个版本的SDK是0.10,所以工具就是gst-launch-0.10、gst-inspect-0.10和gst-discoverer-0.10。


gst-launch

      这个工具可以创建一个pipeline,初始化然后运行。它可以让你在正式写代码实现pipeline之前先快速测试一下,看看是否能工作。

      请记住这个工具只能建立简单地pipeline。尤其是,它只能在一个特定层级之上模拟pipeline和应用的交互。在任何情况下,它都可以很简单的快速测试pipeline,全世界的GStreamer的开发者每天都在使用它。

      请注意,gst-launch对于开发者来说主要是一个调试工具。你不应该基于它开发应用,而应该使用gst_parse_launch()这个API来创建pipeline。

      虽然构造pipeline的描述是非常简单地事情,但多个element的连接也很快能让事情变得超越你的想象。不过别怕,最后每个人都能学会gst-launch的语法。

      gst-launch的命令行包括一个在PIPELINE-DESCRIPTION之后的一系列选项。有些简单地指令在下面会讲到,在gst-launch的描述文档上可以看到所有的相关内容。

elements

      简单来说,一个PIPELINE-DESCRPTION是一系列用!分隔开的元素,试一下下面的命令:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. gst-launch-0.10 videotestsrc ! ffmpegcolorspace ! autovideosink  
      你可以看见一个带动画视频窗口。使用CTRL+C可以终止运行。

      这个例子用了videotestsrc,ffmpegcolorspace和autovideosink三个element。GStreamer会把他们的输出pad和输入pad连接起来,如果存在超过1个可用的输入/输出pad,那么就用pad的Caps来确定兼容的pad。

属性

      element可能是有属性的,在命令行里格式就是“属性=值”,多个属性用空格来分开。可以用gst-inspect工具来查一下element的属性(这个工具下面会讲到)。

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. gst-launch-0.10 videotestsrc pattern=11 ! ffmpegcolorspace ! autovideosink  
      你可以看见一个静态的一系列同心圆的图像。

带名字的element

      element可以用name这个属性来设置名称,这样一些复杂的包含分支的pipeline可以创建了。有了名字,就可以使用前面创建的element,这在使用有多个pad的element(比如demuxer或者tee等)时是必不可少的。

      带名字的element在使用名字时需要在后面加一个点。

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. gst-launch-0.10 videotestsrc ! ffmpegcolorspace ! tee name=t ! queue ! autovideosink t. ! queue ! autovideosink  
      你可以看见两个视频窗口,显示同样的内容。如果你只看到了一个,试着挪动一些窗口位置,可能另一个被压住了。

      这个例子中把videotestsrc先连接了ffmpegcolorspace,然后连接了tee element(在《GStreamer基础教程07——多线程和pad的有效性》里提到过),这个tee就被命名成‘t’,然后一路输出到queue以及autovideosink,另一路输出到另一个queue和autovideosink。

      queue在这里是必须的,原因参见《GStreamer基础教程07——多线程和pad的有效性》。

Pads

      在连接两个element时与其让GStreamer来选择哪个Pad,我们宁可直接指定Pad。我们可以在命名element后使用.+pad名字的方法来做到这点(element必须先命名)。同样可以用gst-inspect来查看element里面pad的名字。

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. gst-launch-0.10.exe souphttpsrc location=http://docs.gstreamer.com/media/sintel_trailer-480p.webm ! matroskademux name=d d.video_00 ! matroskamux ! filesink location=sintel_video.mkv  
      这个命令使用souphttpsrc在internet上锁定了一个媒体文件,这个文件是webm格式的。我们可以用matroskademux来打开这个文件,因为媒体包含音频和视频,所以我们创建了两个输出Pad,名字分别是video_00和audio_00。我们把video_00和matroskamux element连接起来,把视频流重新打包,最后连接到filesink,这样我们就把流存到了一个名叫sintel_video.mkv的文件。

      总之,我们找了一个webm文件,去掉了声音,仅把视频拿出来存成了一个新文件。如果我们像保持声音,那么就应该这样做:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. gst-launch-0.10.exe souphttpsrc location=http://docs.gstreamer.com/media/sintel_trailer-480p.webm ! matroskademux name=d d.audio_00 ! vorbisparse ! matroskamux ! filesink location=sintel_audio.mka  
      这里的vorbisparse element会从流里面取出一些信息,然后放到Pad的Caps里面,这样下一个element,也就是matroskamux就可以知道如何处理这个流了。这个处理在抓取视频的时候是不用做的,因为matroskademux已经做了这件事情。

      请注意,在上面的两个例子中,媒体没有被解码和播放。我们仅仅只是把数据搬动了一下而已。

Caps过滤

      当一个element有不止一个pad时,连接下一个element可能是模糊不清的,因为下游的element可能有不止一个的兼容的输入pad,或者它的输入pad可以和所有的输出pad兼容。在这样的情况下,GStreamer会使用第一个可以连接的Pad,这样相当于说GStreamer是随机找一个pad来连接的。

      看一下下面的pipeline:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. gst-launch-0.10 souphttpsrc location=http://docs.gstreamer.com/media/sintel_trailer-480p.webm ! matroskademux ! filesink location=test  

      这里和上一个例子用了同样的媒体文件和demuxer。finksink输入pad是任意格式,这意味着它可以接受所有的媒体格式。那么matroskademux的哪个pad可以接到filesink呢?video_00还是audio_00?我们无法知道。

      为了消除这种不确定性,前面例子中我们用了pad的名字的方法,这里介绍使用Caps过滤的方法:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. gst-launch-0.10 souphttpsrc location=http://docs.gstreamer.com/media/sintel_trailer-480p.webm ! matroskademux ! video/x-vp8 ! matroskamux ! filesink location=sintel_video.mkv  
      一个Caps过滤动作类似于让element不做任何动作,仅仅接受给出的Caps。在这个例子中,在matroskademux和matroskamux中间我们加入了一个ievideo/x-vp8的Caps过滤,这样就表明在matroskademux中我们仅仅需要能生成这种类型视频的输出Pad。

      我们需要用gst-inspect工具来查看一个element能接受和生成的Caps,用gst-discoverer来查看文件里面包含的Caps。如果我们需要查看在pipeline里面一个element生成的Caps,在gst-launch里面使用-v参数即可。

例子

      用playbin2播放一个媒体文件(和《GStreamer基础教程01——Hello World》一样)

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. gst-launch-0.10 playbin2 uri=http://docs.gstreamer.com/media/sintel_trailer-480p.webm  
      一个正常的播放pipeline。

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. gst-launch-0.10 souphttpsrc location=http://docs.gstreamer.com/media/sintel_trailer-480p.webm ! matroskademux name=d ! queue ! vp8dec ! ffmpegcolorspace ! autovideosink d. ! queue ! vorbisdec ! audioconvert ! audioresample ! autoaudiosink  

      一个转码的pipeline,解析webm之后把所有的流解码,重新把音视频编码成其他格式,然后压成Ogg文件。

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. gst-launch-0.10 uridecodebin uri=http://docs.gstreamer.com/media/sintel_trailer-480p.webm name=d ! queue ! theoraenc ! oggmux name=m ! filesink location=sintel.ogg d. ! queue ! audioconvert ! audioresample ! flacenc ! m.  

      一个调整视频比例的pipeline。videoscale element可以调整输入尺寸然后再输出。例子里面用Caps过滤设置了视频大小为320x200:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. gst-launch-0.10 uridecodebin uri=http://docs.gstreamer.com/media/sintel_trailer-480p.webm ! queue ! videoscale ! video/x-raw-yuv,width=320,height=200 ! ffmpegcolorspace ! autovideosink  

      get-launch简单介绍大致就这些内容了,这可以作为一个很好的起点。如果你需要更多的信息,请猛戳这里


gst-inspect

      这个工具有三种操作:

      不带参数,它会列出所有可用的element,也就是你所有可以使用的元素

      带一个文件名,它会把这个文件作为GStreamer的一个插件,试着打开,然后列出内部所有的element

      带一个GStreamer的element,会列出该element的所有信息

      让我们看一个例子:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. gst-inspect-0.10 vp8dec  
  2.    
  3. Factory Details:  
  4.   Long name:    On2 VP8 Decoder  
  5.   Class:        Codec/Decoder/Video  
  6.   Description:  Decode VP8 video streams  
  7.   Author(s):    David Schleef <ds@entropywave.com>  
  8.   Rank:         primary (256)  
  9. Plugin Details:  
  10.   Name:                 vp8  
  11.   Description:          VP8 plugin  
  12.   Filename:             I:gstreamer-sdk2012.5x86libgstreamer-0.10libgstvp8.dll  
  13.   Version:              0.10.23  
  14.   License:              LGPL  
  15.   Source module:        gst-plugins-bad  
  16.   Source release date:  2012-02-20  
  17.   Binary package:       GStreamer Bad Plug-ins (GStreamer SDK)  
  18.   Origin URL:           http://www.gstreamer.com  
  19. GObject  
  20.  +----GstObject  
  21.        +----GstElement  
  22.              +----GstBaseVideoCodec  
  23.                    +----GstBaseVideoDecoder  
  24.                          +----GstVP8Dec  
  25. Pad Templates:  
  26.   SRC template'src'  
  27.     Availability: Always  
  28.     Capabilities:  
  29.       video/x-raw-yuv  
  30.                  format: I420  
  31.                   width: [ 12147483647 ]  
  32.                  height: [ 12147483647 ]  
  33.               framerate: [ 0/12147483647/1 ]  
  34.   SINK template'sink'  
  35.     Availability: Always  
  36.     Capabilities:  
  37.       video/x-vp8  
  38.    
  39. Element Flags:  
  40.   no flags set  
  41. Element Implementation:  
  42.   Has change_state() function: gst_base_video_decoder_change_state  
  43.   Has custom save_thyself() function: gst_element_save_thyself  
  44.   Has custom restore_thyself() function: gst_element_restore_thyself  
  45. Element has no clocking capabilities.  
  46. Element has no indexing capabilities.  
  47. Element has no URI handling capabilities.  
  48. Pads:  
  49.   SRC'src'  
  50.     Implementation:  
  51.       Has custom eventfunc(): gst_base_video_decoder_src_event  
  52.       Has custom queryfunc(): gst_base_video_decoder_src_query  
  53.         Provides query types:  
  54.                 (1):    position (Current position)  
  55.                 (2):    duration (Total duration)  
  56.                 (8):    convert (Converting between formats)  
  57.       Has custom iterintlinkfunc(): gst_pad_iterate_internal_links_default  
  58.       Has getcapsfunc(): gst_pad_get_fixed_caps_func  
  59.       Has acceptcapsfunc(): gst_pad_acceptcaps_default  
  60.     Pad Template'src'  
  61.   SINK'sink'  
  62.     Implementation:  
  63.       Has chainfunc(): gst_base_video_decoder_chain  
  64.       Has custom eventfunc(): gst_base_video_decoder_sink_event  
  65.       Has custom queryfunc(): gst_base_video_decoder_sink_query  
  66.       Has custom iterintlinkfunc(): gst_pad_iterate_internal_links_default  
  67.       Has setcapsfunc(): gst_base_video_decoder_sink_setcaps  
  68.       Has acceptcapsfunc(): gst_pad_acceptcaps_default  
  69.     Pad Template'sink'  
  70. Element Properties:  
  71.   name                : The name of the object  
  72.                         flags: readable, writable  
  73.                         String. Default"vp8dec0"  
  74.   post-processing     : Enable post processing  
  75.                         flags: readable, writable  
  76.                         Boolean. Defaultfalse  
  77.   post-processing-flags: Flags to control post processing  
  78.                         flags: readable, writable  
  79.                         Flags "GstVP8DecPostProcessingFlags" Default0x00000003"demacroblock+deblock"  
  80.                            (0x00000001): deblock          - Deblock  
  81.                            (0x00000002): demacroblock     - Demacroblock  
  82.                            (0x00000004): addnoise         - Add noise  
  83.   deblocking-level    : Deblocking level  
  84.                         flags: readable, writable  
  85.                         Unsigned Integer. Range0 - 16 Default4   
  86.   noise-level         : Noise level  
  87.                         flags: readable, writable  
  88.                         Unsigned Integer. Range0 - 16 Default0    
      这里最重要的是 :

      Pad Templates:这部分会列出所有的Pad的种类以及它们的Caps。通过这些你可以确认是否可以和某一个element连接。这个例子中,只有一个sink的Pad Template,只能接受video/x-vp8(用VP8格式来编码视频数据)格式;只有一个source的Pad Template,生成video/x-raw-yuv。

      element的属性:这里列出了element的所有的属性以及有效值。

      更多信息请查看gst-inspect的文档。


gst-discoverer

      这个工具是对GstDiscoverer对象的一个包装。它可以接受从命令行输入的一个URI,然后打印出所有的信息。这个在查看媒体是如何编码如何复用时是很有用的,这样我们可以确定把什么element放到pipeline里面。

      使用gst-discoverer --help来获得帮助。

      让我们看个例子:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. gst-discoverer-0.10 http://docs.gstreamer.com/media/sintel_trailer-480p.webm -v  
  2.    
  3. Analyzing http://docs.gstreamer.com/media/sintel_trailer-480p.webm  
  4. Done discovering http://docs.gstreamer.com/media/sintel_trailer-480p.webm  
  5. Topology:  
  6.   container: video/webm  
  7.     audio: audio/x-vorbis, channels=(int)2, rate=(int)48000  
  8.       Codec:  
  9.         audio/x-vorbis, channels=(int)2, rate=(int)48000  
  10.       Additional info:  
  11.         None  
  12.       Language: en  
  13.       Channels2  
  14.       Sample rate48000  
  15.       Depth0  
  16.       Bitrate80000  
  17.       Max bitrate0  
  18.       Tags:  
  19.         taglist, language-code=(string)en, container-format=(string)Matroska, audio-codec=(string)Vorbis, application-name=(string)ffmpeg2theora-0.24, encoder=(string)"Xiph.Org libVorbis I 20090709", encoder-version=(uint)0, nominal-bitrate=(uint)80000, bitrate=(uint)80000;  
  20.     video: video/x-vp8, width=(int)854, height=(int)480, framerate=(fraction)25/1  
  21.       Codec:  
  22.         video/x-vp8, width=(int)854, height=(int)480, framerate=(fraction)25/1  
  23.       Additional info:  
  24.         None  
  25.       Width854  
  26.       Height480  
  27.       Depth0  
  28.       Frame rate25/1  
  29.       Pixel aspect ratio1/1  
  30.       Interlacedfalse  
  31.       Bitrate0  
  32.       Max bitrate0  
  33.       Tags:  
  34.         taglist, video-codec=(string)"VP8 video", container-format=(string)Matroska;  
  35.    
  36. Properties:  
  37.   Duration: 0:00:52.250000000  
  38.   Seekable: yes  
  39.   Tags:  
  40.       video codec: On2 VP8  
  41.       language code: en  
  42.       container format: Matroska  
  43.       application name: ffmpeg2theora-0.24  
  44.       encoder: Xiph.Org libVorbis I 20090709  
  45.       encoder version0  
  46.       audio codec: Vorbis  
  47.       nominal bitrate80000  
  48.       bitrate80000  
原文地址:https://www.cnblogs.com/huty/p/8517312.html