How to encode grayscale video streams to MPEG-1 with FFmpeg?
Asked Answered
B

4

9

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?

Beebeebe answered 1/12, 2011 at 22:42 Comment(0)
H
11

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().

Hidalgo answered 2/12, 2011 at 8:34 Comment(6)
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.Beebeebe
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 extensionHidalgo
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.Beebeebe
OK, now I get it. You say MPEG1VIDEO doesn't accept byte pixel formats. That's true - it only accepts YUV. 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.Hidalgo
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.Beebeebe
Most codecs accept yuv only. I had never heard about yuv before I started using ffmpeg.Hidalgo
A
8

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).

Ashtoreth answered 16/12, 2011 at 3:50 Comment(1)
Thanks for the info, fortunately the sws_getContext and sws_scale functions perform the conversion quite efficiently.Beebeebe
G
0

It works if you simply use "hue" filter; like this:

ffmpeg -i inputfile.ogv -vf hue=s=0 outputfile-unsat.ogv
Gringo answered 26/10, 2014 at 17:17 Comment(1)
I think you misunderstood the questionBlepharitis
P
-2

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
Pacificism answered 28/6, 2021 at 18:47 Comment(1)
Please 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.Sandoval

© 2022 - 2024 — McMap. All rights reserved.