gstreamer-pipeline用法汇总

生成mp4

生成2s包含video和audio的mp4

gst-launch-1.0 -e --gst-debug=2 \
\
videotestsrc num-buffers=60 ! video/x-raw,framerate=30/1 ! \
openh264enc ! h264parse ! muxer.video_0 \
\
audiotestsrc num-buffers=90 ! audio/x-raw,channels=1 ! \
fdkaacenc ! aacparse ! muxer.audio_0 \
\
qtmux name=muxer ! queue ! filesink location=./test.mp4

qtmux生成只有video的mp4

gst-launch-1.0 videotestsrc num-buffers=60 \
! video/x-raw,framerate=30/1 ! openh264enc ! h264parse \
! video/x-h264,stream-format=avc,alignment=au ! qtmux \
! filesink location=./test.mp4

openh264enc输出是byte-stream,所以后面不能直接跟qtmux,需要加h264parse转成avc

不要caps video/x-h264,stream-format=avc,alignment=au也是可以的:

gst-launch-1.0 videotestsrc num-buffers=60 \
! video/x-raw,framerate=30/1 ! openh264enc \
! h264parse ! qtmux ! filesink location=./test.mp4

比较全的一个写法:

gst-launch-1.0 -e --gst-debug=**:2 videotestsrc is-live=true \
! queue ! videoconvert \
! videorate silent=false \
! videoscale \
! "video/x-raw, width=1280, height=720, framerate=30/1" \
! queue ! openh264enc ! h264parse \
! queue ! muxer.video_0 \
audiotestsrc is-live=true \
! audioconvert ! audioresample ! audiorate ! "audio/x-raw, rate=48000, channels=1" \
! queue ! fdkaacenc \
! queue ! muxer.audio_0 \
mp4mux name=muxer streamable=true \
! queue ! filesink location="./mystream.mp4" sync=false

从playbin里面dump audio数据

通过修改audiosink,将audio数据dump到文件,并且以mmp4文件存储:

gst-play-1.0 ./test.mp4 --audiosink='aacparse ! qtmux ! filesink location=./test.aac.mp4'

这个适合在代码中指定playbin的audiosink为上面的组合,dump出来的数据方便问题分析。

然后用gst-launch播放

gst-launch-1.0 filesrc location=./test.aac.mp4 \
! qtdemux name=d d.audio_0 ! aacparse ! fdkaacdec ! autoaudiosink

播放-如何添加caps filter

autovideosink和waylandsink可以播放成功,但是颜色不对:

gst-launch-1.0 playbin uri=file:/data/panda.mp4
gst-launch-1.0 filesrc location=/data/panda.mp4 ! decodebin ! autovideosink

h264parse的pad属性

h264parse的src pad的sink caps是video/x-h264,经过处理,输出的caps是经过parse的,stream-format可以是avc,avc3, byte-stream。

$ gst-inspect-1.0 h264parse

Pad Templates:
  SINK template: 'sink'
    Availability: Always
    Capabilities:
      video/x-h264

  SRC template: 'src'
    Availability: Always
    Capabilities:
      video/x-h264
                 parsed: true
          stream-format: { (string)avc, (string)avc3, (string)byte-stream }
              alignment: { (string)au, (string)nal }

v4l2h264dec的sink pad属性

sink pad的caps是video/x-h264,可接收的收据stream-format只能是byte-stream,alignment只能是au

$ gst-inspect-1.0 v4l2h264dec

  SINK template: 'sink'
    Availability: Always
    Capabilities:
      video/x-h264(memory:DMABuf)
          stream-format: byte-stream
              alignment: au
      video/x-h264
          stream-format: byte-stream
              alignment: au

所以h264parse和v4l2h264dec放在一起,实际上不指定caps filter是可以的,可以完成自动协商。

但下面这几种写法都可以播放,但是颜色不对:

gst-launch-1.0 filesrc location=/data/panda.mp4 ! qtdemux name=demux demux.video_0 ! h264parse ! v4l2h264dec ! autovideosink
gst-launch-1.0 filesrc location=/data/panda.mp4 ! qtdemux ! h264parse ! v4l2h264dec ! autovideosink

caps filter解决颜色问题

通过分析,判断color-format不对,增加caps filter,加上format参数,指定位NV21格式,颜色正常:

gst-launch-1.0 filesrc location=/data/panda.mp4 \
! qtdemux ! h264parse ! video/x-h264,stream-format=byte-stream,alignment=au \
! v4l2h264dec ! videoconvert ! video/x-raw,format=NV21 ! autovideosink

简化下,去掉h264parse和v4l2h264dec之间的caps filter,videoconvert也是不需要的:

gst-launch-1.0 filesrc location=/data/panda.mp4 \
! qtdemux ! h264parse ! v4l2h264dec ! video/x-raw,format=NV21 ! autovideosink

使用waylandsink也是一样的:

gst-launch-1.0 filesrc location=/data/panda.mp4 \
! qtdemux name=demux demux.video_0 ! h264parse ! v4l2h264dec \
! videoconvert ! video/x-raw,format=NV21 ! waylandsink

播放http源

gst-launch-1.0 playbin uri=http://192.168.31.122/mov/mp4/panda.mp4

gst-launch-1.0 souphttpsrc location=http://192.168.31.122/mov/mp4/panda.mp4 \
! qtdemux name=demux demux.video_0 ! h264parse ! v4l2h264dec \
! video/x-raw,format=NV21 ! autovideosink

打开Gstreamer日志,GST_DEBUG=4,4是INFO级别信息

GST_DEBUG=4 gst-launch-1.0 filesrc location=/data/panda.mp4 ! qtdemux ! decodebin ! autovideosink

播放https

souphttpsrc需要加上ssl-strict=false属性

gst-launch-1.0 \
souphttpsrc ssl-strict=false location=https://192.168.31.122/mov/mp4/panda.mp4 \
! qtdemux name=demux demux.video_0 ! h264parse ! v4l2h264dec \
! video/x-raw,format=NV21 ! autovideosink

gst-launch-1.0 \
souphttpsrc ssl-strict=false location=https://media.w3.org/2010/05/sintel/trailer.mp4 \
! qtdemux ! h264parse ! openh264dec !  autovideosink

playbin播放hls

gst-launch-1.0 playbin uri=http://playertest.longtailvideo.com/adaptive/bipbop/gear4/prog_index.m3u8

gst-launch-1.0 playbin uri=http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8

gst-launch-1.0 playbin \
uri=https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_fmp4/master.m3u8

playbin通过videosink设置参数

gst-play-1.0 ./test.mp4 --videosink="glimagesink render-rectangle=<300,200,320,240>'"

gst-launch-1.0生成hls播放源

使用gst-launch-1.0生成HLS segment

gst-launch-1.0 videotestsrc is-live=true ! x264enc ! mpegtsmux ! hlssink max-files=20

它将生成播放列表和片段文件,时间越长segment000xx.ts的文件个数会更多,不用max-files选项的话,默认是10个:

.
├── playlist.m3u8
├── segment00000.ts
├── segment00001.ts
├── segment00002.ts
├── segment00003.ts
└── segment00004.ts

然后这些文件就可以通过HTTP访问,如nginx或Apache做服务器,用ffplay和gst-launch做播放测试:

ffplay http://192.168.31.122/hls/playlist.m3u8

gst-launch-1.0 playbin uri=http://192.168.31.122/hls/playlist.m3u8

gst-launch-1.0 playbin uri=http://192.168.31.122/hls/playlist.m3u8

生成Pipeline运行时的Element关系图

命令行程序,可以设置环境变量GST_DEBUG_DUMP_DOT_DIR,在pipeline运行时会生成pipeline的dot图。

GST_DEBUG_DUMP_DOT_DIR=. gst-launch-1.0 playbin uri=file:///data/panda.mp4

或者:

export GST_DEBUG_DUMP_DOT_DIR=.
gst-launch-1.0 playbin uri=file:///data/panda.mp4

分离2channel音频位1ch音频

gst-launch-1.0 filesrc location=~/audio.mp4 \
! qtdemux ! faad ! deinterleave name=d d.src_0 \
! fdkaacenc ! audio/mpeg,stream-format=raw ! aacparse \
! qtmux ! filesink location=~/audio-1ch.mp4

分离后播放:

gst-launch-1.0 filesrc location=~/audio-1ch.mp4 ! qtdemux ! faad ! playsink
gst-launch-1.0 filesrc location=~/audio-1ch.mp4 ! qtdemux ! avdec_aac ! playsink

你可能感兴趣的:(Gstreamer)