【GStreamer 】3-6-2 gstreamer x264enc 、avdec_h264插件说明

【GStreamer 】3-6-2 gstreamer x264enc 、avdec_h264插件说明_第1张图片

1、x264enc编码

        该插件将原始视频编码为H264压缩数据,也称为MPEG-4 AVC(高级视频编解码器)。也就是MPEG4,其实就是H264.

        查询插件信息:只罗列的输入和输出,其它参数属性遇到了再看

nvidia@nvidia-desktop:~$ gst-inspect-1.0  x264enc
Pad Templates:
  SRC template: 'src'
    Availability: Always
    Capabilities:
      video/x-h264
              framerate: [ 0/1, 2147483647/1 ]
                  width: [ 1, 2147483647 ]
                 height: [ 1, 2147483647 ]
          stream-format: { (string)avc, (string)byte-stream }
              alignment: au
                profile: { (string)high-4:4:4, (string)high-4:2:2, (string)high-10, (string)high, (string)main, (string)baseline, (string)constrained-baseline, (string)high-4:4:4-intra, (string)high-4:2:2-intra, (string)high-10-intra }
  
  SINK template: 'sink'
    Availability: Always
    Capabilities:
      video/x-raw
              framerate: [ 0/1, 2147483647/1 ]
                  width: [ 16, 2147483647 ]
                 height: [ 16, 2147483647 ]
                 format: { (string)Y444, (string)Y42B, (string)I420, (string)YV12, (string)NV12, (string)Y444_10LE, (string)I422_10LE, (string)I420_10LE }

        sink 输入必须是video/x-raw,注意适配。src 输出是video/x-h264,注意保存文件的话,注意下游插件的输入。

1.1 示例

1、压缩保存成MP4

#x264enc 其输入为 video/x-raw ,输出为video/x-h264
# mp4mux MP4 文件格式封装插件
# filesink location 老朋友了,保存文件
gst-launch-1.0 videotestsrc num-buffers=100 \
! 'video/x-raw,format=YUY2,width=1280, height=720, framerate=25/1' \
! videoconvert  \
! x264enc ! mp4mux ! filesink location=/home/nvidia/Pictures/x264-video-1280-720-25.mp4

【GStreamer 】3-6-2 gstreamer x264enc 、avdec_h264插件说明_第2张图片

2、压缩保存成AVI

        下面这是官方给的一个示例管道,将测试源视频压缩成264格式后,保存成avi格式文件,同时确保合理的最小量化因子以避免一些(过度)浪费。理想情况下,不应该将H264放入AVI容器(或其他任何内容),而是使用Matroska或MP4/QuickTime或MPEG-TS,比如上一个示例,存放到了MP4中

# avimux 之前见过不少次了,AVI文件生成必用的
# qp-min x264enc插件的一个属性参数
gst-launch-1.0 -v videotestsrc num-buffers=1000 \
! 'video/x-raw,format=YUY2,width=640, height=480, framerate=30/1' \
! videoconvert  \
! x264enc qp-min=18 \
! avimux ! filesink location=/home/nvidia/Pictures/x264-video-640-480-30.avi

【GStreamer 】3-6-2 gstreamer x264enc 、avdec_h264插件说明_第3张图片

3、压缩保存成mkv

下面这个示例,会示意将视频压缩成264格式后,保存成 (Matroska) mkv格式文件

# matroskamux mkv文件生成使用
gst-launch-1.0 -v videotestsrc num-buffers=1000 \
! 'video/x-raw,format=YUY2,width=640, height=480, framerate=30/1' \
! videoconvert  \
! x264enc pass=quant \
! matroskamux ! filesink location=/home/nvidia/Pictures/x264-video-640-480-30.mkv

【GStreamer 】3-6-2 gstreamer x264enc 、avdec_h264插件说明_第4张图片

4、压缩保存成mov

        下面这个示例,会示意将视频压缩成264格式后,保存成 (Quicktime mov format) mov格式文件

gst-launch-1.0 -v videotestsrc num-buffers=1000 \
! x264enc pass=5 quantizer=25 speed-preset=6 \
! video/x-h264, profile=baseline \
! qtmux ! filesink location=/home/nvidia/Pictures/x264.mov

【GStreamer 】3-6-2 gstreamer x264enc 、avdec_h264插件说明_第5张图片

5、一边压缩,一边显示

下面这个示例,将演示在显示视频的同时,压缩H.264

gst-launch-1.0 -v videotestsrc num-buffers=1000 \
! tee name=t \
! queue ! videoconvert ! xvimagesink t. \
! queue ! x264enc rc-lookahead=5 ! fakesink

        为了让他更明显一点,我们压缩后保存成指定格式的文件,上面我们已经举例了好几种H.264常用的格式。

gst-launch-1.0 -v videotestsrc num-buffers=1000 \
! 'video/x-raw,format=YUY2,width=640, height=480, framerate=30/1' \
! videoconvert   \
! tee name=srctee  \
! queue !  xvimagesink srctee. \
! queue ! x264enc threads=4 sliced-threads=TRUE tune=zerolatency ! matroskamux ! filesink location = /home/nvidia/Pictures/x264-tee.mkv
gst-launch-1.0 -v videotestsrc num-buffers=1000 \
! 'video/x-raw,format=YUY2,width=640, height=480, framerate=30/1' \
! videoconvert   \
! tee name=srctee  \
! queue !  xvimagesink srctee. \
! queue !  x264enc pass=5 quantizer=25 speed-preset=6 ! video/x-h264, profile=baseline ! qtmux ! filesink location=/home/nvidia/Pictures/x264-tee.mov

6、USB相机输入一边压缩,一边显示

        之前我们也示例过一个例子,这个可以进一步罗列一下:将测试源换成了USB相机

# USB相机使用image/jpeg格式数据
# jpegdec 解码
# queue2 与queue类似,但是也有差异
gst-launch-1.0 v4l2src device=/dev/video0  num-buffers=500 \
! image/jpeg,width=1280,height=720,framerate=30/1  \
! jpegdec ! videoconvert \
! tee name=srctee srctee. \
! queue2 ! xvimagesink srctee. \
! queue2 ! x264enc threads=4 sliced-threads=TRUE tune=zerolatency ! matroskamux ! filesink location = /home/nvidia/Pictures/x264-tee-video0.mkv

【GStreamer 】3-6-2 gstreamer x264enc 、avdec_h264插件说明_第6张图片

 

2、 avdec_h264 解码

        上一节,我们分享了x264enc 这个编码插件,按照惯例,GStreamer 对于对应的解码插件是不是应该叫x264dec好一点呢,但是偏不是,对应的解码库名字叫avdec_h264 ,这个对于生手来说,确认不容易通过联想得到。

查询插件信息:只罗列的输入和输出,其它参数属性遇到了再看

nvidia@nvidia-desktop:~/Pictures$ gst-inspect-1.0  avdec_h264 
​
GObject
 +----GInitiallyUnowned
       +----GstObject
             +----GstElement
                   +----GstVideoDecoder
                         +----avdec_h264
​
Pad Templates:
  SINK template: 'sink'
    Availability: Always
    Capabilities:
      video/x-h264
              alignment: au
          stream-format: { (string)avc, (string)byte-stream }
  
  SRC template: 'src'
    Availability: Always
    Capabilities:
      video/x-raw
                 format: { (string)I420, (string)YUY2, (string)RGB, (string)BGR, (string)Y42B, (string)Y444, (string)YUV9, (string)Y41B, (string)GRAY8, (string)RGB8P, (string)I420, (string)Y42B, (string)Y444, (string)UYVY, (string)NV12, (string)NV21, (string)ARGB, (string)RGBA, (string)ABGR, (string)BGRA, (string)GRAY16_BE, (string)GRAY16_LE, (string)A420, (string)RGB16, (string)RGB15, (string)I420_10BE, (string)I420_10LE, (string)I422_10BE, (string)I422_10LE, (string)Y444_10BE, (string)Y444_10LE, (string)GBR, (string)GBR_10BE, (string)GBR_10LE, (string)A420_10BE, (string)A420_10LE, (string)A422_10BE, (string)A422_10LE, (string)A444_10BE, (string)A444_10LE, (string)GBRA, (string)xRGB, (string)RGBx, (string)xBGR, (string)BGRx, (string)I420_12BE, (string)I420_12LE, (string)I422_12BE, (string)I422_12LE, (string)Y444_12BE, (string)Y444_12LE, (string)GBR_12BE, (string)GBR_12LE, (string)GBRA_12BE, (string)GBRA_12LE }
​
Element has no clocking capabilities.
Element has no URI handling capabilities.
​
Pads:
  SINK: 'sink'
    Pad Template: 'sink'
  SRC: 'src'
    Pad Template: 'src'

2.1 示例-读取显示MP4文件

# filesrc 读文件插件
# qtdemux 解复用插件
# h264parse 用来链接 流数据到 解码插件 avdec_h264 配对使用
# avdec_h264 解码插件
gst-launch-1.0 -v filesrc location=/home/nvidia/Pictures/x264-video-1280-720-25.mp4 \
! qtdemux ! h264parse ! avdec_h264 \
! videoconvert ! xvimagesink

2.2 示例-读取显示AVI 文件

gst-launch-1.0 -v filesrc location=/home/nvidia/Pictures/x264-video-640-480-30.avi \
! avidemux ! h264parse ! avdec_h264 \
! videoconvert ! xvimagesink
 
  

2.3 示例-读取显示mkv 文件

gst-launch-1.0 -v filesrc location=/home/nvidia/Pictures/x264-video-640-480-30.mkv \
! matroskademux ! h264parse ! avdec_h264 \
! videoconvert ! xvimagesink

2.4 示例-读取显示mov 文件

gst-launch-1.0 -v filesrc location=/home/nvidia/Pictures/x264.mov \
! qtdemux ! h264parse ! avdec_h264 \
! videoconvert ! xvimagesink

觉得以上有点用处,点个赞呗,谢谢。

 

你可能感兴趣的:(nvidia,Jetson,GStreamer,Ubuntu,ffmpeg,音视频,边缘计算,开发语言,linux)