I've got a grayscale video stream coming off a Firewire astronomy camera, I'd like to use FFmpeg to compress the video stream but it will not accept single byte pixel formats for the MPEG1VIDEO codecs. How can I use the FFmpeg API to convert grayscale video frames into a frame format accepted by FFmpeg?
4 Answers
MPEG-1 only accepts YUV so you need to convert your frame to the YUV format. Use the SwsContext
structure which you create by calling sws_getContext()
, and then use sws_scale()
.

- 75,001
- 122
- 434
- 781
-
Yes, my pixels are grayscale, I've already tried the 'rawvideo' codec but it doesn't give me any compression, that's what I'm really looking for. – Gearoid Murphy Dec 02 '11 at 10:34
-
You should use rawvideo as the **input** codec. You can specify anything you want for the output codec, and in fact, you don't need to - ffmpeg will guess the output format and codecs to use by the output file's extension – sashoalm Dec 02 '11 at 11:07
-
I'm using the C API, not the command line tool, my code fills the AVFrame buffer with the grayscale image and then calls the encoder function. – Gearoid Murphy Dec 02 '11 at 12:40
-
1OK, now I get it. You say MPEG1VIDEO doesn't accept byte pixel formats. That's true - it only accepts [YUV](http://en.wikipedia.org/wiki/MPEG-1#Color_space). So convert your frame to yuv. Use the SwsContext structure, create it by calling sws_getContext, and then use sws_scale. I think there was also img_convert but it was deprecated I think. I have code that does this somewhere so I can help if you have problems. – sashoalm Dec 02 '11 at 13:00
-
I was thinking as much, I was hoping to find a codec which could handle the grayscale natively, thanks for your help all the same, could you edit your answer to reflect this and I'll accept it. – Gearoid Murphy Dec 02 '11 at 13:53
-
Most codecs accept yuv only. I had never heard about yuv before I started using ffmpeg. – sashoalm Dec 02 '11 at 13:56
The Relationship of Gray scale and YUV is very simple - The "Y" of YUV is exactly same as Grayscale.
The simpler way to convert the Grayscale in to YUV is
See this reference for Conversion between the scales :
Accordingly :
Y = (0.257 * R) + (0.504 * G) + (0.098 * B) + 16 Cr = V = (0.439 * R) - (0.368 * G) - (0.071 * B) + 128 Cb = U = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128 Now: For a Grayscale image (W): R = W; G = W; B = W; Keeping this: Y[i] = 0.895 * W[i] + 16. U[i] = 128 (fixed value) V[i] = 128 (fxied value)
You can actually use Y[i] = W[i] that will be almost same. The 128 value represents '0' in a scaled/shifted base of 0-256 signed to unsigned conversion.
So all you need to keep is create this other memory area Y and U as the fixed value and provide this frames to ffmpeg.
I am not sure, but by appropriately telling FFMPEG, it does this inside only. The RGB values you supply are equally covered there as well; which is also not native to MPEG.
So look for FFMPEG's API which is able to let you do this.
BONUS
Remember, that in good old days there were Black and white (Gray scale) TV sets. The new color TV sets, needed to be compliant to the old ones, so color information is added in the form of U and V (sometimes it is also called YCbCr - where Cb and Cr are called chroma and are respectively linear variations of UV in this context).

- 2,110
- 1
- 17
- 31
-
1Thanks for the info, fortunately the sws_getContext and sws_scale functions perform the conversion quite efficiently. – Gearoid Murphy Dec 16 '11 at 13:24
It works if you simply use "hue" filter; like this:
ffmpeg -i inputfile.ogv -vf hue=s=0 outputfile-unsat.ogv

- 25,759
- 11
- 71
- 103

- 17
- 1
-
1
-
Old answer, but the question is about how to take greyscale video from a scientific device and process it via FFmpeg. – Giacomo1968 Oct 22 '20 at 00:43
Any problem with ffmpeg then you need to download latest version first, it will be fix some thing bug:
https://ffmpeg.org/download.html#build-windows

- 83
- 5
-
2Please don't paste the same answer multiple places. Further, this question already has an accepted answer—and it has nothing to do with upgrading to the latest version. This is really generic advice that's making no attempt to answer the specific question being asked. – Jeremy Caney Jun 28 '21 at 19:00