<bdo id='Z7374'></bdo><ul id='Z7374'></ul>
    <tfoot id='Z7374'></tfoot>
  • <legend id='Z7374'><style id='Z7374'><dir id='Z7374'><q id='Z7374'></q></dir></style></legend>

        <small id='Z7374'></small><noframes id='Z7374'>

      1. <i id='Z7374'><tr id='Z7374'><dt id='Z7374'><q id='Z7374'><span id='Z7374'><b id='Z7374'><form id='Z7374'><ins id='Z7374'></ins><ul id='Z7374'></ul><sub id='Z7374'></sub></form><legend id='Z7374'></legend><bdo id='Z7374'><pre id='Z7374'><center id='Z7374'></center></pre></bdo></b><th id='Z7374'></th></span></q></dt></tr></i><div id='Z7374'><tfoot id='Z7374'></tfoot><dl id='Z7374'><fieldset id='Z7374'></fieldset></dl></div>
      2. 从 C++ 中的像素值数组创建视频

        时间:2023-10-07
      3. <legend id='nxuOE'><style id='nxuOE'><dir id='nxuOE'><q id='nxuOE'></q></dir></style></legend>

            <tfoot id='nxuOE'></tfoot>

            1. <small id='nxuOE'></small><noframes id='nxuOE'>

                  <tbody id='nxuOE'></tbody>
                <i id='nxuOE'><tr id='nxuOE'><dt id='nxuOE'><q id='nxuOE'><span id='nxuOE'><b id='nxuOE'><form id='nxuOE'><ins id='nxuOE'></ins><ul id='nxuOE'></ul><sub id='nxuOE'></sub></form><legend id='nxuOE'></legend><bdo id='nxuOE'><pre id='nxuOE'><center id='nxuOE'></center></pre></bdo></b><th id='nxuOE'></th></span></q></dt></tr></i><div id='nxuOE'><tfoot id='nxuOE'></tfoot><dl id='nxuOE'><fieldset id='nxuOE'></fieldset></dl></div>
                  <bdo id='nxuOE'></bdo><ul id='nxuOE'></ul>
                  本文介绍了从 C++ 中的像素值数组创建视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  有谁知道一种将存储在数组中的像素值序列保存到视频的方法?目前我正在使用 Cimg 来可视化一个简单的 n 体模拟,同时我可以将每次迭代保存到一个图像文件中,这非常慢.任何有关处理视频的类似库的建议将不胜感激.本质上,我只想将我创建的 Cimg 窗口中显示的内容记录到视频文件中.程序是用C++编写的,在linux上,用g++编译.

                  Does anyone know of a method to save a sequence of pixel values, stored in an array to a video? Currently I'm using Cimg to visualise a simple n-body simulation, whilst I can save each iteration to an image file, this is very slow. Any suggestions on a similar library for handling video would be appreciated. Essentially, I just want to record what's displayed in the Cimg window I create to a video file. The program is written in C++, on linux, compiling with g++.

                  事实上,我可以运行模拟并使用屏幕捕获软件记录它的运行情况,这似乎意味着这是可能的,但我想要一个更简洁的解决方案.

                  The fact that I can run the simulation and record it running with screen capturing software would seem to imply it's possible, but I'd like a tidier solution.

                  推荐答案

                  我今天正在玩这个,我想我会分享我的结果.您可以从 CImg 输出原始 RGB 视频,然后使用 ffmpeg 将其编码为视频,如下所示:

                  I was playing around doing this today, and thought I would share my results. You can output raw RGB video from CImg and then use ffmpeg to encode it up into video like this:

                  #include <iostream>
                  #include "CImg.h"
                  
                  using namespace std;
                  using namespace cimg_library;
                  
                  int main()
                  {
                     const unsigned int width=1024;
                     const unsigned int height=768;
                  
                     // Basic frame we will draw in
                     CImg<unsigned char> image(width,height,1,3);
                  
                     unsigned char magenta[] = {255,0,255};
                  
                     // We are going to output 300 frames of 1024x768 RGB raw video
                     // ... making a 10s long video at 30fps
                     int radius=100;
                     int cx=100;
                     int cy=100;
                     for(int frame=0;frame<300;frame++){
                        // Start with black - it shows fewer stains ;-)
                        image.fill(0);
                        image.draw_circle(cx,cy,radius,magenta);
                  
                        // Move and re-colour circle
                        cx+=2; cy++; if(magenta[1]!=255){magenta[1]++;}
                  
                        // Output to ffmpeg to make video, in planar GBR format
                        // i.e. run program like this
                        // ./main | ffmpeg -y -f rawvideo -pixel_format gbrp -video_size 1024x768 -i - -c:v h264 -pix_fmt yuv420p video.mov
                        char* s=reinterpret_cast<char*>(image.data()+(width*height));   // Get start of G plane
                        std::cout.write(s,width*height);                                // Output it
                        s=reinterpret_cast<char*>(image.data()+2*(width*height));       // Get start of B plane
                        std::cout.write(s,width*height);                                // Output it
                        s=reinterpret_cast<char*>(image.data());                        // Get start of R plane
                        std::cout.write(s,width*height);                                // Output it
                     }
                  }
                  

                  我想我不会去好莱坞,因为视频不是很令人兴奋!

                  I guess I won't make it to Hollywood as the video is not very exciting!

                  像这样运行上面的代码来制作视频:

                  Run the above code like this to make a video:

                  ./main | ffmpeg -y -f rawvideo -pixel_format gbrp -video_size 1024x768 -i - -c:v h264 -pix_fmt yuv420p video.mov
                  


                  注意 1

                  要意识到的是,CImg 以平面配置存储数据,这意味着首先是所有红色像素,然后是所有绿色像素,然后是所有蓝色像素 - 所有没有任何填充或空格.

                  The thing to realise is that CImg stores data in a planar configuration, which means all the red pixels first, then all the green ones directly afterwards and then all the blue ones straight after that - all without any padding or spaces.

                  想象一下 CImg 中的 4x4 图像(16 像素):

                  Imagine a 4x4 image (with 16 pixels) in CImg:

                  RRRRRRRRRRRRRRRR GGGGGGGGGGGGGGGG BBBBBBBBBBBBBBBB
                  

                  与常规 RGB 数据不同,它会存储与以下内容相同的图像:

                  unlike regular RGB data, which would store the same image as:

                  RGB RGB RGB RGB RGB RGB RGB RGB RGB RGB RGB RGB RGB RGB RGB RGB 
                  

                  因此,您可以将所有数据打乱并重新格式化并作为 -pixel_fmt rgb24 传递给 ffmpeg,或者像我一样在 中输出CImg 的平面格式并选择匹配的 -pixel_fmt gbrp(其中 p 表示 平面").您只需要以正确的 B、G、R 顺序输出平面.另请参阅注释 4.

                  So, you can either shuffle all the data around and reformat it and pass to ffmpeg as -pixel_fmt rgb24, or do as I did and output in CImg's planar format and choose a matching -pixel_fmt gbrp (where the p means "planar"). You just have to output the planes in the correct B,G,R order. See also Note 4.

                  注意事项 2

                  我选择做 3 个 write() ,每个颜色平面一个,为了演示的清晰起见,使用 聚集写入"会更有效.writev(),所以:

                  I chose to do 3 write()s, one for each colour plane, for the sake of clarity of demonstration, it would be more efficient to use a "gathered write" with writev(), so this:

                  char* s=reinterpret_cast<char*>(image.data()+(width*height));   // Get start of G plane
                  std::cout.write(s,width*height);                                // Output it
                  s=reinterpret_cast<char*>(image.data()+2*(width*height));       // Get start of B plane
                  std::cout.write(s,width*height);                                // Output it
                  s=reinterpret_cast<char*>(image.data());                        // Get start of R plane
                  std::cout.write(s,width*height);  
                  

                  会变成这样:

                  struct iovec iov[3];
                  ssize_t nwritten;
                  
                  iov[0].iov_base = reinterpret_cast<char*>(image.data()+(width*height))
                  iov[0].iov_len  = width*height;
                  iov[1].iov_base = reinterpret_cast<char*>(image.data()+2*(width*height));
                  iov[1].iov_len  = width*height;
                  iov[2].iov_base = reinterpret_cast<char*>(image.data());  
                  iov[2].iov_len  = width*height;
                  
                  nwritten = writev(STDOUT_FILENO,iov,3);
                  


                  注意 3

                  我使用 -c:v h264 -pix_fmt yuv420p 使视频与 Mac 上的 Apple QuickTime 兼容,但无论如何您都可以轻松更改输出 -更难的部分是让 CImgfmpeg 之间的接口正确.

                  I used the -c:v h264 -pix_fmt yuv420p to make the video compatible with Apple's QuickTime on my Mac, but you can change the output easily anyway - the harder part was getting the interface between CImg and fmpeg right.

                  注意事项 4

                  如果你想打乱数据并将其写入ffmpeg非平面(-pixel_fmt rgb),我最初是这样做的,代码是这样的:

                  If you want to shuffle the data around and write it to ffmpeg non-planar (-pixel_fmt rgb), I did that originally and the code was like this:

                  // Outside main loop
                  unsigned char* BIP = new unsigned char[width*height*3];
                  unsigned char *d,*r,*g,*b;
                  
                  ...
                  ...
                  
                  // Now output it...
                  // ... remember CImg is band-interleaved by plane  RRRRRR GGGGGG BBBBBB
                  // ... not band-interleaved by pixel RGB RGB RGB RGB
                  r=image.data();       // Start of R plane in CImg image
                  g=r+(width*height);   // Start of G plane in CImg image
                  b=g+(width*height);   // Start of B plane in CImg image
                  d=BIP;                // Destination buffer in RGB order
                  for(int i=0;i<width*height;i++){
                     *d++=*r++;
                     *d++=*g++;
                     *d++=*b++;
                  }
                  // Output to ffmpeg to make video, i.e. run program like this
                  // ./main | ffmpeg -y -f rawvideo -pixel_format rgb24 -video_size 1024x768 -i - -c:v h264 -pix_fmt yuv420p video.mov
                  std::cout.write(reinterpret_cast<char*>(BIP),width*height*3);
                  

                  理论上,您可以使用 CImgpermute_axes() 方法来做到这一点,但我没有成功.

                  In theory, you can do this with CImg's permute_axes() method, but I had no success.

                  这篇关于从 C++ 中的像素值数组创建视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 Qt 播放实时视频流 下一篇:使用gstreamer暂停后如何继续播放?

                  相关文章

                  最新文章

                • <legend id='ZVAkK'><style id='ZVAkK'><dir id='ZVAkK'><q id='ZVAkK'></q></dir></style></legend>

                    <small id='ZVAkK'></small><noframes id='ZVAkK'>

                  1. <i id='ZVAkK'><tr id='ZVAkK'><dt id='ZVAkK'><q id='ZVAkK'><span id='ZVAkK'><b id='ZVAkK'><form id='ZVAkK'><ins id='ZVAkK'></ins><ul id='ZVAkK'></ul><sub id='ZVAkK'></sub></form><legend id='ZVAkK'></legend><bdo id='ZVAkK'><pre id='ZVAkK'><center id='ZVAkK'></center></pre></bdo></b><th id='ZVAkK'></th></span></q></dt></tr></i><div id='ZVAkK'><tfoot id='ZVAkK'></tfoot><dl id='ZVAkK'><fieldset id='ZVAkK'></fieldset></dl></div>

                      <tfoot id='ZVAkK'></tfoot>
                        <bdo id='ZVAkK'></bdo><ul id='ZVAkK'></ul>