将 RAW 图像转换为 BMP 格式

Convert RAW images to BMP format

我收到了一个 .raw 帧格式 (here is a sample),其中包含帧的原始像素值。我被告知可以使用 FFMpeg 使用以下命令将其转换为图像(.bmp 或 .png)。但是,尝试时,它不起作用。

dd bs=16 dumpB4EncodeHLSAfterEncodeBuf1200IP_TS/dumpedYuv/image-1140.raw |  ffmpeg -f rawvideo -video_size 2160 2160 -pixel_format nv12 -i - yuv.bmp

有什么问题?有什么方法可以避免使用 dd 并纯粹从 FFmpeg 开始吗?

这是错误:

dd: unrecognized operand ‘frame.raw’
Try 'dd --help' for more information.
ffmpeg version 3.4.8-0ubuntu0.2 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04)
  configuration: --prefix=/usr --extra-version=0ubuntu0.2 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
pipe:: Invalid data found when processing input

如果我直接使用 ffmpeg:

ffmpeg -video_size 2160x2160 -pixel_format nv12 -i frame.raw yuv.bmp

我会得到这个错误:

ffmpeg version 3.4.8-0ubuntu0.2 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04)
  configuration: --prefix=/usr --extra-version=0ubuntu0.2 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
[image2 @ 0x5605e52bb4c0] Format image2 detected only with low score of 5, misdetection possible!
Input #0, image2, from 'frame.raw':
  Duration: 00:00:00.04, start: 0.000000, bitrate: 1244160 kb/s
    Stream #0:0: Video: rawvideo (NV12 / 0x3231564E), nv12, 2160x2160, 25 tbr, 25 tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo (native) -> bmp (native))
Press [q] to stop, [?] for help
[rawvideo @ 0x5605e52bea00] Invalid buffer size, packet size 6220800 < expected frame_size 6998400
Error while decoding stream #0:0: Invalid argument
Finishing stream 0:0 without any data written to it.
Output #0, image2, to 'yuv.bmp':
  Metadata:
    encoder         : Lavf57.83.100
    Stream #0:0: Video: bmp, bgr24, 2160x2160, q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc57.107.100 bmp
frame=    0 fps=0.0 q=0.0 Lsize=N/A time=00:00:00.00 bitrate=N/A speed=   0x
video:0kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)
Conversion failed!

更新答案

现在我们知道了正确的大小和格式,我们可以使用 ImageMagick 轻松地进行转换:

magick -depth 8 -size 1920x1080 rgb:frame.raw result.png

或者,如您所愿,使用 ffmpeg:

ffmpeg -y -f rawvideo -s 1920x1080 -pix_fmt rgb24 -i frame.raw  image.bmp

原答案

您的原始文件不是 NV12 或不是 2160x2160。 NV12 每个像素使用 1.5 个字节,这意味着你应该

2160 * 2160 * 1.5 = 6,998,400 bytes

但您实际上有 6,220,800,所以您的信息有问题 - 文件不完整,或者不是那种格式,或者不是那种大小。

如果是 NV12,前 2160x2160 字节将是灰度(或 Luminance/Y)通道,但即使我们将前 2160x2160 字节作为灰度并忽略任何尾部颜色信息,它仍然是错误的:

head -c $((2160*2160)) frame.raw | magick -depth 8 -size 2160x2160 gray:- image.bmp

请检查并确定原始数据的来源和特征。