简体   繁体   English

如何使用 FFmpeg 将灰度视频流编码为 MPEG-1?

[英]How to encode grayscale video streams to MPEG-1 with FFmpeg?

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.我有一个来自 Firewire 天文相机的灰度视频流,我想使用 FFmpeg 来压缩视频流,但它不接受 MPEG1VIDEO 编解码器的单字节像素格式。 How can I use the FFmpeg API to convert grayscale video frames into a frame format accepted by FFmpeg?如何使用 FFmpeg API 将灰度视频帧转换为 FFmpeg 接受的帧格式?

MPEG-1 only accepts YUV so you need to convert your frame to the YUV format. MPEG-1 仅接受 YUV,因此您需要将帧转换为 YUV 格式。 Use the SwsContext structure which you create by calling sws_getContext() , and then use sws_scale() .使用您通过调用sws_getContext()创建的SwsContext结构,然后使用sws_scale()

The Relationship of Gray scale and YUV is very simple - The "Y" of YUV is exactly same as Grayscale.灰度和YUV的关系很简单——YUV的“Y”和灰度完全一样。

The simpler way to convert the Grayscale in to YUV is将灰度转换为 YUV 的更简单方法是

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.您实际上可以使用几乎相同的 Y[i] = W[i]。 The 128 value represents '0' in a scaled/shifted base of 0-256 signed to unsigned conversion. 128 值表示 0-256 有符号到无符号转换的缩放/移位基数中的“0”。

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.所以你需要保留的就是创建另一个内存区域 Y 和 U 作为固定值,并将这些帧提供给 ffmpeg。

I am not sure, but by appropriately telling FFMPEG, it does this inside only.我不确定,但通过适当地告诉 FFMPEG,它只在内部执行此操作。 The RGB values you supply are equally covered there as well;您提供的 RGB 值也同样包含在内; which is also not native to MPEG.这也不是 MPEG 原生的。

So look for FFMPEG's API which is able to let you do this.因此,请寻找能够让您执行此操作的 FFMPEG 的 API。

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).新的彩电,需要兼容旧的,所以颜色信息以U和V的形式加入(有时也称为YCbCr——其中Cb和Cr称为色度,分别是UV在这个上下文)。

It works if you simply use "hue" filter;如果您只是使用“色调”过滤器,它会起作用; like this:像这样:

ffmpeg -i inputfile.ogv -vf hue=s=0 outputfile-unsat.ogv

ffmpeg 有任何问题,那么你需要先下载最新版本,它会修复一些错误:

https://ffmpeg.org/download.html#build-windows

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM