如何在不将单独的帧图像写入磁盘的情况下从 C++ 程序中生成的多个图像编码视频?

Posted

技术标签:

【中文标题】如何在不将单独的帧图像写入磁盘的情况下从 C++ 程序中生成的多个图像编码视频?【英文标题】:How to encode a video from several images generated in a C++ program without writing the separate frame images to disk? 【发布时间】:2016-04-03 08:28:19 【问题描述】:

我正在编写一个 C++ 代码,其中在执行其中实现的一些操作后会生成 N 个不同帧的序列。每一帧完成后,我将其作为IMG_%d.png写入磁盘,最后通过ffmpeg使用x264编解码器将它们编码为视频。

程序主要部分的伪代码总结如下:

std::vector<int> B(width*height*3);
for (i=0; i<N; i++)

  // void generateframe(std::vector<int> &, int)
  generateframe(B, i); // Returns different images for different i values.
  sprintf(s, "IMG_%d.png", i+1);
  WriteToDisk(B, s); // void WriteToDisk(std::vector<int>, char[])

这个实现的问题是,所需的帧数 N 通常很高(N~100000)以及图片的分辨率(1920x1080),导致磁盘过载,产生的写入周期为每次执行后数十 GB。

为了避免这种情况,我一直在尝试查找有关将向量 B 中存储的每个图像直接解析到 x264 等编码器的文档(无需将中间图像文件写入磁盘)。尽管发现了一些有趣的话题,但没有一个能具体解决我真正想要的问题,因为其中许多涉及使用磁盘上现有图像文件执行编码器,而其他一些则为其他编程语言提供解决方案,例如 Python (@987654321 @你可以为那个平台找到一个完全令人满意的解决方案)。

我想得到的伪代码是这样的:

std::vector<int> B(width*height*3);
video_file=open_video("Generated_Video.mp4", ...[encoder options]...);
for (i=0; i<N; i++)

  generateframe(B, i+1);
  add_frame(video_file, B);

video_file.close();

根据我在相关主题上阅读的内容,x264 C++ API 可能能够做到这一点,但是,如上所述,我没有为我的具体问题找到令人满意的答案。我尝试直接学习和使用 ffmpeg 源代码,但它的低易用性和编译问题迫使我放弃这种可能性,因为我只是一个非专业程序员(我把它当作一种爱好,不幸的是我不能浪费这么多时间学习要求很高的东西)。

我想到的另一个可能的解决方案是找到一种在 C++ 代码中调用 ffmpeg 二进制文件的方法,并设法将每次迭代的图像数据(存储在 B 中)传输到编码器,让加法每一帧(即不“关闭”要写入的视频文件)直到最后一帧,这样可以添加更多帧,直到到达第 N 个,视频文件将被“关闭”。也就是说,通过C++程序调用ffmpeg.exe将第一帧写入视频,但让编码器“等待”更多帧。然后再次调用 ffmpeg 以添加第二帧并使编码器再次“等待”更多帧,依此类推,直到到达最后一帧,视频将在此处完成。但是,我不知道如何进行或是否真的可行。

编辑 1:

正如回复中所建议的,我一直在记录有关命名管道并尝试在我的代码中使用它们。首先,应该指出我正在使用 Cygwin,所以我的命名管道是像在 Linux 下创建的一样创建。我使用的修改后的伪代码(包括相应的系统库)如下:

FILE *fd;
mkfifo("myfifo", 0666);

for (i=0; i<N; i++)

  fd=fopen("myfifo", "wb");
  generateframe(B, i+1);
  WriteToPipe(B, fd); // void WriteToPipe(std::vector<int>, FILE *&fd)
  fflush(fd);
  fd=fclose("myfifo");

unlink("myfifo");

WriteToPipe 是对之前 WriteToFile 函数的轻微修改,我确保发送图像数据的写入缓冲区足够小以适应管道缓冲限制。

然后我在Cygwin终端编译并编写如下命令:

./myprogram | ffmpeg -i pipe:myfifo -c:v libx264 -preset slow -crf 20 Video.mp4

但是,当 i=0 在“fopen”行(即第一个 fopen 调用)时,它仍然停留在循环中。如果我没有调用 ffmpeg,那很自然,因为服务器(我的程序)会等待客户端程序连接到管道的“另一端”,但事实并非如此。看起来它们无法以某种方式通过管道连接,但我无法找到进一步的文档来解决这个问题。有什么建议吗?

【问题讨论】:

你用过命名管道吗?至于 FFMPEG,它可以接受命名管道作为输入 -i pipe:pipe_name Example on msdn.microsoft 感谢您的建议。我了解了命名管道并尝试通过这种方式。尝试此方法后出现的新问题在我的新编辑中公开。 【参考方案1】:

经过一番激烈的斗争,在学习了一点如何将 FFmpeg 和 libx264 C API 用于我的特定目的后,我终于设法使它工作,这要感谢一些用户在本网站和其他一些用户提供的有用信息,如以及一些 FFmpeg 的文档示例。为了便于说明,下面将详细介绍。

首先,编译 libx264 C 库,然后编译带有配置选项 --enable-gpl --enable-libx264 的 FFmpeg。现在让我们开始编码。实现请求目的的相关代码部分如下:

包括:

#include <stdint.h>
extern "C"
#include <x264.h>
#include <libswscale/swscale.h>
#include <libavcodec/avcodec.h>
#include <libavutil/mathematics.h>
#include <libavformat/avformat.h>
#include <libavutil/opt.h>

Makefile 上的 LDFLAGS:

-lx264 -lswscale -lavutil -lavformat -lavcodec

内部代码(为简单起见,将省略错误检查,变量声明将在需要时完成,而不是从头开始,以便更好地理解):

av_register_all(); // Loads the whole database of available codecs and formats.

struct SwsContext* convertCtx = sws_getContext(width, height, AV_PIX_FMT_RGB24, width, height, AV_PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL); // Preparing to convert my generated RGB images to YUV frames.

// Preparing the data concerning the format and codec in order to write properly the header, frame data and end of file.
char *fmtext="mp4";
char *filename;
sprintf(filename, "GeneratedVideo.%s", fmtext);
AVOutputFormat * fmt = av_guess_format(fmtext, NULL, NULL);
AVFormatContext *oc = NULL;
avformat_alloc_output_context2(&oc, NULL, NULL, filename);
AVStream * stream = avformat_new_stream(oc, 0);
AVCodec *codec=NULL;
AVCodecContext *c= NULL;
int ret;

codec = avcodec_find_encoder_by_name("libx264");

// Setting up the codec:
av_dict_set( &opt, "preset", "slow", 0 );
av_dict_set( &opt, "crf", "20", 0 );
avcodec_get_context_defaults3(stream->codec, codec);
c=avcodec_alloc_context3(codec);
c->width = width;
c->height = height;
c->pix_fmt = AV_PIX_FMT_YUV420P;

// Setting up the format, its stream(s), linking with the codec(s) and write the header:
if (oc->oformat->flags & AVFMT_GLOBALHEADER) // Some formats require a global header.
    c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
avcodec_open2( c, codec, &opt );
av_dict_free(&opt);
stream->time_base=(AVRational)1, 25;
stream->codec=c; // Once the codec is set up, we need to let the container know which codec are the streams using, in this case the only (video) stream.
av_dump_format(oc, 0, filename, 1);
avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);
ret=avformat_write_header(oc, &opt);
av_dict_free(&opt); 

// Preparing the containers of the frame data:
AVFrame *rgbpic, *yuvpic;

// Allocating memory for each RGB frame, which will be lately converted to YUV:
rgbpic=av_frame_alloc();
rgbpic->format=AV_PIX_FMT_RGB24;
rgbpic->width=width;
rgbpic->height=height;
ret=av_frame_get_buffer(rgbpic, 1);

// Allocating memory for each conversion output YUV frame:
yuvpic=av_frame_alloc();
yuvpic->format=AV_PIX_FMT_YUV420P;
yuvpic->width=width;
yuvpic->height=height;
ret=av_frame_get_buffer(yuvpic, 1);

// After the format, code and general frame data is set, we write the video in the frame generation loop:
// std::vector<uint8_t> B(width*height*3);

上面注释的向量与我在问题中公开的向量具有相同的结构;但是,RGB 数据以特定方式存储在 AVFrames 上。因此,为了说明起见,让我们假设我们有一个指向 uint8_t[3] Matrix(int, int) 形式的结构的指针,它访问给定坐标 (x, y) 是 Matrix(x, y)->Red, Matrix(x, y)->Green 和 Matrix(x, y)->Blue,为了分别得到红色、绿色和蓝色的值坐标(x,y)。第一个参数代表水平位置,从左到右随着 x 的增加而第二个参数代表垂直位置,从上到下随着 y 的增加。

话虽如此,用于传输数据、编码和写入每一帧的 for 循环如下:

Matrix B(width, height);
int got_output;
AVPacket pkt;
for (i=0; i<N; i++)

    generateframe(B, i); // This one is the function that generates a different frame for each i.
    // The AVFrame data will be stored as RGBRGBRGB... row-wise, from left to right and from top to bottom, hence we have to proceed as follows:
    for (y=0; y<height; y++)
    
        for (x=0; x<width; x++)
        
            // rgbpic->linesize[0] is equal to width.
            rgbpic->data[0][y*rgbpic->linesize[0]+3*x]=B(x, y)->Red;
            rgbpic->data[0][y*rgbpic->linesize[0]+3*x+1]=B(x, y)->Green;
            rgbpic->data[0][y*rgbpic->linesize[0]+3*x+2]=B(x, y)->Blue;
        
    
    sws_scale(convertCtx, rgbpic->data, rgbpic->linesize, 0, height, yuvpic->data, yuvpic->linesize); // Not actually scaling anything, but just converting the RGB data to YUV and store it in yuvpic.
    av_init_packet(&pkt);
    pkt.data = NULL;
    pkt.size = 0;
    yuvpic->pts = i; // The PTS of the frame are just in a reference unit, unrelated to the format we are using. We set them, for instance, as the corresponding frame number.
    ret=avcodec_encode_video2(c, &pkt, yuvpic, &got_output);
    if (got_output)
    
        fflush(stdout);
        av_packet_rescale_ts(&pkt, (AVRational)1, 25, stream->time_base); // We set the packet PTS and DTS taking in the account our FPS (second argument) and the time base that our selected format uses (third argument).
        pkt.stream_index = stream->index;
        printf("Write frame %6d (size=%6d)\n", i, pkt.size);
        av_interleaved_write_frame(oc, &pkt); // Write the encoded frame to the mp4 file.
        av_packet_unref(&pkt);
    

// Writing the delayed frames:
for (got_output = 1; got_output; i++) 
    ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
    if (got_output) 
        fflush(stdout);
        av_packet_rescale_ts(&pkt, (AVRational)1, 25, stream->time_base);
        pkt.stream_index = stream->index;
        printf("Write frame %6d (size=%6d)\n", i, pkt.size);
        av_interleaved_write_frame(oc, &pkt);
        av_packet_unref(&pkt);
    

av_write_trailer(oc); // Writing the end of the file.
if (!(fmt->flags & AVFMT_NOFILE))
    avio_closep(oc->pb); // Closing the file.
avcodec_close(stream->codec);
// Freeing all the allocated memory:
sws_freeContext(convertCtx);
av_frame_free(&rgbpic);
av_frame_free(&yuvpic);
avformat_free_context(oc);

旁注:

为了将来参考,由于网络上有关时间戳 (PTS/DTS) 的可用信息看起来很混乱,接下来我将解释我是如何通过设置适当的值来解决问题的。错误设置这些值会导致输出大小比通过 ffmpeg 构建的二进制命令行工具获得的大得多,因为帧数据是通过比 FPS 实际设置的更小的时间间隔冗余写入的。

首先,需要注意的是,编码时有两种时间戳:一种与帧相关联(PTS)(预编码阶段),两种与数据包相关联(PTS和DTS)(后-编码阶段)。在第一种情况下,看起来可以使用自定义的参考单位来分配帧 PTS 值(唯一的限制是,如果想要恒定 FPS,它们必须等间距),因此可以将帧号作为我们的例子在上面的代码中做了。在第二个中,我们必须考虑以下参数:

输出格式容器的时基,在我们的例子中是 mp4 (=12800 Hz),其信息保存在 stream->time_base 中。 视频所需的 FPS。 编码器是否生成 B 帧(在第二种情况下,帧的 PTS 和 DTS 值必须设置相同,但如果我们在第一种情况下,则更复杂,如本例所示)。有关更多参考资料,请参阅此 answer 到另一个相关问题。

这里的关键是幸运的是,不必为这些数量的计算而苦恼,因为 libav 提供了一个函数,可以通过了解上述数据来计算与数据包相关的正确时间戳:

av_packet_rescale_ts(AVPacket *pkt, AVRational FPS, AVRational time_base)

多亏了这些考虑,我终于能够生成一个健全的输出容器,并且与使用命令行工具获得的压缩率基本相同,这是在更深入地研究格式头和尾如何以及如何正确设置时间戳。

【讨论】:

我还发现了可能有用的单头 MPEG 编码器:jonolick.com/home/mpeg-video-writer 你能把源文件附加到这个答案吗?【参考方案2】:

感谢您的出色工作,@ksb496!

一个小改进:

c=avcodec_alloc_context3(codec);

最好写成:

c = stream->codec;

避免内存泄漏。

如果您不介意,我已将完整的可部署库上传到 GitHub:https://github.com/apc-llc/moviemaker-cpp.git

【讨论】:

【参考方案3】:

感谢 ksb496,我设法完成了这项任务,但就我而言,我需要更改一些代码才能按预期工作。我想也许它可以帮助其他人,所以我决定分享(延迟两年:D)。

我有一个 RGB 缓冲区,由 directshow 样本采集器填充,我需要从中获取视频。从给定答案的RGBYUV 的转换对我来说不起作用。我是这样做的:

int stride = m_width * 3;
int index = 0;
for (int y = 0; y < m_height; y++) 
    for (int x = 0; x < stride; x++) 
        int j = (size - ((y + 1)*stride)) + x;
        m_rgbpic->data[0][j] = data[index];
        ++index;
    

data 变量是我的RGB 缓冲区(简单的BYTE*),sizedata 缓冲区大小(以字节为单位)。它开始从左下角到右上角填充RGB AVFrame

另一件事是我的FFMPEG版本没有av_packet_rescale_ts功能。这是最新版本,但 FFMPEG 文档并没有说此功能在任何地方都已弃用,我想这可能仅适用于 Windows。无论如何,我使用 av_rescale_q 代替它做同样的工作。像这样:

AVPacket pkt;
pkt.pts = av_rescale_q(pkt.pts,  1, 25 , m_stream->time_base);

最后一件事,使用这种格式转换,我需要将我的swsContext 更改为BGR24 而不是RGB24,如下所示:

m_convert_ctx = sws_getContext(width, height, AV_PIX_FMT_BGR24, width, height,
        AV_PIX_FMT_YUV420P, SWS_FAST_BILINEAR, nullptr, nullptr, nullptr);

【讨论】:

【参考方案4】:

avcodec_encode_video2 & avcodec_encode_audio2 似乎已被弃用。当前版本 (4.2) 的 FFmpeg 具有新的 API:avcodec_send_frame & avcodec_receive_packet

【讨论】:

以上是关于如何在不将单独的帧图像写入磁盘的情况下从 C++ 程序中生成的多个图像编码视频?的主要内容,如果未能解决你的问题,请参考以下文章

如何在不将应用程序放在前面的情况下从 BLE 设备控制 iOS 应用程序进行后台处理

如何在不将控制器添加到视图层次结构并使其可见的情况下从 UIViewController 获取 UIView?

如何在不使用临时文件的情况下从 Java 中的嵌套 zip 文件中读取数据?

是否有可能在不快速调用 API 的情况下从互联网获取图像?

如何播放音频样本而不将其写入文件?

如何在不写入输出流的情况下从 ZipInputStream 获取每个 ZipFile 条目的字节/内容?