如何使用 Gstreamer 从 PNG 图像创建 mp4 视频文件

Posted

技术标签:

【中文标题】如何使用 Gstreamer 从 PNG 图像创建 mp4 视频文件【英文标题】:How to create a mp4 video file from PNG images using Gstreamer 【发布时间】:2019-09-17 23:08:11 【问题描述】:

我正在使用 gstreamer 命令行界面,在从 PNG 图像创建 mp4 视频文件时卡住了。请各位大神帮忙解决一下。

使用以下命令,我从摄像机创建了 PNG 图像:

gst-launch-1.0.exe -v ksvideosrc !队列 !解码器!视频转换! ! multifilesink location="frame%05d.png"

我可以使用以下命令播放 pPNG 图像:

gst-launch-1.0 -v multifilesrc location="frame%05d.png" index=0 !解码器! glimagesink

但在从 PNG 图像创建 mp4 视频时,我从管道收到了 EOS:

将管道设置为 PAUSED ... 管道正在预滚... /GstPipeline:pipeline0/GstDecodeBin:decodebin0/GstTypeFindElement:typefind.GstPad:src: caps = image/png /GstPipeline:pipeline0/GstDecodeBin:decodebin0/GstPngParse:pngparse0.GstPad:sink: caps = image/png /GstPipeline:pipeline0/GstDecodeBin:decodebin0/GstPngDec:pngdec0.GstPad:sink: caps = image/png, width=(int)640, height=(int)480 /GstPipeline:pipeline0/GstDecodeBin:decodebin0/GstPngParse:pngparse0.GstPad:src: caps = image/png, width=(int)640, height=(int)480 /GstPipeline:pipeline0/GstDecodeBin:decodebin0/GstPngDec:pngdec0.GstPad:src: caps = video/x-raw, format=(string)RGB, width=(int)640, height=(int)480, interlace-mode= (string)progressive, multiview-mode=(string)mono, multiview-flags=(GstVideoMultiviewFlagsSet)0:ffffffff:/right-view-first/left-flipped/left-flopped/right-flipped/right-flopped/half-纵横比/混合单声道,像素纵横比=(分数)1/1,色度=(字符串)sRGB,帧率=(分数)0/1 /GstPipeline:pipeline0/GstVideoConvert:videoconvert0.GstPad:src: caps = video/x-raw, width=(int)640, height=(int)480, framerate=(fraction)0/1, format=(string)Y444 , interlace-mode=(string)progressive, multiview-mode=(string)mono, multiview-flags=(GstVideoMultiviewFlagsSet)0:ffffffff:/right-view-first/left-flipped/left-flopped/right-flipped/right -flopped/half-aspect/mixed-mono,像素纵横比=(分数)1/1 /GstPipeline:pipeline0/GstVideoRate:videorate0.GstPad:src: caps = video/x-raw, width=(int)640, height=(int)480, framerate=(fraction)0/1, format=(string)Y444 , interlace-mode=(string)progressive, multiview-mode=(string)mono, multiview-flags=(GstVideoMultiviewFlagsSet)0:ffffffff:/right-view-first/left-flipped/left-flopped/right-flipped/right -flopped/half-aspect/mixed-mono,像素纵横比=(分数)1/1 重新分配延迟... /GstPipeline:pipeline0/GstX264Enc:x264enc0.GstPad:sink: caps = video/x-raw, width=(int)640, height=(int)480, framerate=(fraction)0/1, format=(string)Y444 , interlace-mode=(string)progressive, multiview-mode=(string)mono, multiview-flags=(GstVideoMultiviewFlagsSet)0:ffffffff:/right-view-first/left-flipped/left-flopped/right-flipped/right -flopped/half-aspect/mixed-mono,像素纵横比=(分数)1/1 /GstPipeline:pipeline0/GstVideoRate:videorate0.GstPad:sink: caps = video/x-raw, width=(int)640, height=(int)480, framerate=(fraction)0/1, format=(string)Y444 , interlace-mode=(string)progressive, multiview-mode=(string)mono, multiview-flags=(GstVideoMultiviewFlagsSet)0:ffffffff:/right-view-first/left-flipped/left-flopped/right-flipped/right -flopped/half-aspect/mixed-mono,像素纵横比=(分数)1/1 /GstPipeline:pipeline0/GstVideoConvert:videoconvert0.GstPad:sink: caps = video/x-raw, format=(string)RGB, width=(int)640, height=(int)480, interlace-mode=(string)progressive , multiview-mode=(string)mono, multiview-flags=(GstVideoMultiviewFlagsSet)0:ffffffff:/right-view-first/left-flipped/left-flopped/right-flipped/right-flopped/half-aspect/mixed-单声道,像素纵横比=(分数)1/1,色度=(字符串)sRGB,帧率=(分数)0/1 /GstPipeline:pipeline0/GstDecodeBin:decodebin0.GstDecodePad:src_0.GstProxyPad:proxypad1: caps = video/x-raw, format=(string)RGB, width=(int)640, height=(int)480, interlace-mode= (string)progressive, multiview-mode=(string)mono, multiview-flags=(GstVideoMultiviewFlagsSet)0:ffffffff:/right-view-first/left-flipped/left-flopped/right-flipped/right-flopped/half-纵横比/混合单声道,像素纵横比=(分数)1/1,色度=(字符串)sRGB,帧率=(分数)0/1 /GstPipeline:pipeline0/GstX264Enc:x264enc0.GstPad:src:帽=视频/ X-H264,codec_data =(缓冲液)01f4001effe1001c67f4001e919b281407b602d41804150000030001773594000f162d9601000668ebec448440,流格式=(字符串)AVC中,对准=(字符串)AU,电平=(字符串)3 , profile=(string)high-4:4:4, width=(int)640, height=(int)480, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction)0/1, interlace-mode=(string)progressive, colorimetry=(string)bt601, chroma-site=(string)jpeg, multiview-mode=(string)mono, multiview-flags=(GstVideoMultiviewFlagsSet)0:ffffffff:/right-view-第一/左翻转/左翻转/右翻转/右翻转/半纵横比/混合单声道 /GstPipeline:pipeline0/GstMP4Mux:mp4mux0.GstQTMuxPad:video_0:帽=视频/ X-H264,codec_data =(缓冲液)01f4001effe1001c67f4001e919b281407b602d41804150000030001773594000f162d9601000668ebec448440,流格式=(字符串)AVC中,对准=(字符串)AU,电平=(字符串)3 , profile=(string)high-4:4:4, width=(int)640, height=(int)480, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction)0/1, interlace-mode=(string)progressive, colorimetry=(string)bt601, chroma-site=(string)jpeg, multiview-mode=(string)mono, multiview-flags=(GstVideoMultiviewFlagsSet)0:ffffffff:/right-view-第一/左翻转/左翻转/右翻转/右翻转/半纵横比/混合单声道 /GstPipeline:pipeline0/GstMP4Mux:mp4mux0.GstPad:src: caps = video/quicktime, variant=(string)iso /GstPipeline:pipeline0/GstFileSink:filesink0.GstPad:sink: caps = video/quicktime, variant=(string)iso 管道预装... 将管道设置为 PLAYING ... 新时钟:GstSystemClock /GstPipeline:pipeline0/GstMP4Mux:mp4mux0.GstPad:src:帽=视频/ QuickTime的,变体=(字符串)异,streamheader =(缓冲) /GstPipeline:pipeline0/GstFileSink:filesink0.GstPad:sink:帽=视频/ QuickTime的,变体=(字符串)异,streamheader =(缓冲) 从元素“pipeline0”获得 EOS。 执行在 0:00:00.814042926 之后结束 将管道设置为 PAUSED ... 将管道设置为 READY ... 将管道设置为 NULL ... 释放管道……

【问题讨论】:

【参考方案1】:

要从您的 png 文件中创建一个 mp4,您可以使用以下管道:

gst-launch-1.0 -e multifilesrc location="frame%05d.png" 大写=“图像/png,帧率=30/1”!解码器!视频转换!队列 ! x264enc !队列 ! mp4 多路复用器!文件接收位置=image.mp4

注意:

    如果您只需要 60fps 的视频,以上将创建一个 30fps 的 mp4 将 multifilesrc 中的大写更改为 caps="image/png,framerate=60/1" 对于 60fps 的视频。 如果您有任何特定硬件,您也可以更改编码器 编码器。

【讨论】:

感谢您的解决方案,它有效。你能简单解释一下上限的工作原理和效果吗 当 pngdec 解码 png 时,它将帧速率设置为 0/1,因为它一次解码单个图像,所以我强制管道将 caps 设置为 30fps。您可以参考链接了解更多信息:[1] gstreamer.freedesktop.org/documentation/design/caps.html [2] gstreamer.freedesktop.org/documentation/plugin-development/…

以上是关于如何使用 Gstreamer 从 PNG 图像创建 mp4 视频文件的主要内容,如果未能解决你的问题,请参考以下文章

JAVA:如何从 byte[] 创建 .PNG 图像?

通过 gstreamer udpsink 流式传输 h.264 时如何解决图像问题

如何使用 OpenCV Python 和 GStreamer 后端创建 x264 RTSP 服务器

从图像创建实时 RTP 流

使用 OpenCV VideoWriter 保存灰度图像时出现 Gstreamer 错误

如何在处理中加载的 png 图像上创建颜色剪贴蒙版?