Convert raw RGB32 file to JPEG or PNG using FFmpeg
Asked Answered
S

2

11

Context

I used a C++ program to write raw bytes to a file (image.raw) in RGB32 format:

R G B A R G B A R G B A ...

and I want to be able to view it in some way. I have the dimensions of the image.

My tools are limited to command line commands (e.g. ffmpeg). I have visited the ffmpeg website for instructions, but it deals more with converting videos to images.


Questions

Is it possible to turn this file into a viewable file type (e.g. .jpeg, .png) using ffmpeg. If so, how would I do it?

If it's not possible, is there a way I can use another command?

It that's still not viable, is there any way I can manipulate the RGB32 bytes inside a C++ program to make it more suitable without the use of external libraries? I also don't want to encode .jpeg myself like this.

Silda answered 19/9, 2017 at 22:6 Comment(0)
D
8

Use the rawvideo demuxer:

ffmpeg -f rawvideo -pixel_format rgba -video_size 320x240 -i input.raw output.png

Since there is no header specifying the assumed video parameters you must specify them, as shown above, in order to be able to decode the data correctly.

See ffmpeg -pix_fmts for a list of supported input pixel formats which may help you choose the appropriate -pixel_format.

Dimidiate answered 19/9, 2017 at 22:24 Comment(5)
Answer worked for me, although I have to add it only worked after I added -i before input.raw.Silda
@PhotometricStereo Yes, that was a typo. Thanks for the edit.Dimidiate
For jpg, try ffmpeg -f rawvideo -pix_fmt rgba -s 320x240 -i input.raw -vcodec mjpeg -pix_fmt yuvj420p output.jpgCerebritis
In case it helps someone, you can replace -i input.raw with -i - so you can simply pipe the output of a program into ffmpeg without having to store the intermediate result (the raw data) in a file.Aleppo
Going in the other direction: ffmpeg -i inputfilename -s wxh outputfilename.rgb produces file with 3 bytes per pixel r followed by g and b (at least on x86 machines).Fairweather
W
0

get a single frame from raw RGBA data

ffmpeg -y -f rawvideo -pix_fmt rgba -ss 00:01 -r 1 -s 320x240 -i input.raw -frames:v 1 output.png

-y overwrite output
-r input framerate (placed before -i)
-ss skip to this time
-frames:v number of frames ot output

Wattage answered 16/12, 2019 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.