C++でAVIを出力する実験 60fps 簡易アニメーション

 C++でAVIを出力する実験をしています。無圧縮なので高解像度の本編では使えないですが、動画内のアクセントとなるアニメーションや特殊効果程度なら応用例があると考えます。WindowsのGDIでグラフなどを描画するスキルが有ればそれをそのまま動画制作に応用可能です。以下のソースはG++でコンパイルできます。(Windows APIを使ってるので残念ながらWindows専用です。)



  1. #define _UNICODE    //ワイド文字列 (Unicode) 対応
  2. #include <windows.h>
  3. #include <vfw.h>
  4. #include <sstream>
  5. #include <string>
  6. /********************************************************************
  7. 実行すると100までカウントアップするAVI動画を作成します。以下のコマンドラインでビルドしてください。
  8. g++ .\avisample001.cpp -mwindows -lvfw32 -municode -o avisample001
  9. *********************************************************************/
  10. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hinstPrev, LPWSTR lpszCmdLine, int nCmdShow)
  11. {
  12.     const int width = 640; //画面サイズフレームレートなどを設定
  13.     const int height = 560;
  14.     const int fps = 60;
  15.     const int frames = 1200;
  16.     
  17.     PAVIFILE pfile;
  18.     PAVISTREAM pavi;
  19.     AVISTREAMINFO si;
  20.     LPAVISTREAMINFO lpsi = &si;
  21.     LPVOID lpBits;
  22.     BITMAPINFOHEADER bmiHeader;    
  23.     
  24.     AVIFileInit();
  25.     if (AVIFileOpen(&pfile, L"video.avi", OF_CREATE | OF_WRITE, NULL) != 0) {
  26.         MessageBox(NULL, L"Fail file open.", L"OK", MB_OK);
  27.         AVIFileExit();
  28.         return 0;}
  29.     ZeroMemory(&si, sizeof(AVISTREAMINFO));
  30.     si.fccType = streamtypeVIDEO;
  31.     si.fccHandler = comptypeDIB;
  32.     si.dwScale = 1;
  33.     si.dwRate = fps;
  34.     si.dwLength = 0;
  35.     si.dwQuality = (DWORD)-1;
  36.     SetRect(&si.rcFrame, 0, 0, width, height);
  37.     if (AVIFileCreateStream(pfile, &pavi, &si) != 0) {
  38.         MessageBox(NULL, TEXT("Fail open stream."), TEXT("OK"), MB_OK);
  39.         AVIFileRelease(pfile);
  40.         AVIFileExit();
  41.         return 0;}
  42.     ZeroMemory(&bmiHeader, sizeof(BITMAPINFOHEADER));
  43.     bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  44.     bmiHeader.biWidth = lpsi->rcFrame.right;
  45.     bmiHeader.biHeight = lpsi->rcFrame.bottom;
  46.     bmiHeader.biPlanes = 1;
  47.     bmiHeader.biBitCount = 24;
  48.     bmiHeader.biCompression = BI_RGB;
  49.     bmiHeader.biSizeImage = bmiHeader.biHeight * ((3 * bmiHeader.biWidth + 3) / 4) * 4;
  50.     AVIStreamSetFormat(pavi, 0, &bmiHeader, sizeof(BITMAPINFOHEADER));
  51.     //ここにHDCへ描画し、AVIStreamWriteを呼び出し一コマ書き出す。これを繰り返す。
  52.     HDC hdc = CreateCompatibleDC(NULL);
  53.     HBITMAP hbmpMem = CreateDIBSection(NULL, (LPBITMAPINFO)&bmiHeader, DIB_RGB_COLORS, &lpBits, NULL, 0);
  54.     HBITMAP hbmpMemPrev = (HBITMAP)SelectObject(hdc, hbmpMem);
  55.     
  56.     SetTextColor(hdc, RGB(255, 255, 255));
  57.     HGDIOBJ hFont = CreateFontW(100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,
  58.         CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"UI Gothic");
  59.     HGDIOBJ hFontOld = SelectObject(hdc, hFont);
  60.     
  61.     HBRUSH hBrush = CreateSolidBrush(RGB(255, 255, 255));
  62.     HBRUSH hPrevBrush =(HBRUSH)SelectObject(hdc,hBrush);
  63.     
  64.     SetBkMode(hdc, TRANSPARENT);
  65.     
  66.     bool isTop = true; //裏表フラグ
  67.     for (int fCnt = 0; fCnt < frames; fCnt++) {
  68.         FillRect(hdc, &lpsi->rcFrame, (HBRUSH)GetStockObject(BLACK_BRUSH)); //背景を黒で塗る
  69.         
  70.         std::wstringstream wss;
  71.         wss << fCnt;
  72.         std::wstring buf = wss.str();
  73.         const int size = width < height?width:height; //高さと幅の短い方を選ぶ
  74.         const int c_width = size/10; //円の幅を調整
  75.         const int div = 59; //円を分割する数
  76.         const float step = (float)360 / div; //分割された度数
  77.         const float seg = step / 2; //空白部と実部の割合(数字が大きいほど実部が減る)
  78.         const int c_x = width / 2; //円の中心座標
  79.         const int c_y = height /2;
  80.         const int radius = size / 2 - size / 20; //余白
  81.         const int l_begin = isTop?0:fCnt%(div+1);
  82.         const int l_end = isTop?fCnt%(div+1):div;
  83.         for (int j = l_begin;j<l_end;j++){
  84.             BeginPath(hdc);
  85.             AngleArc(hdc,c_x,c_y,radius,step*j,0);
  86.             EndPath(hdc);    
  87.             BeginPath(hdc);
  88.             AngleArc(hdc,c_x,c_y,radius,step*j,seg);
  89.             AngleArc(hdc,c_x,c_y,radius-c_width,step*j+seg,-seg);
  90.             CloseFigure(hdc);
  91.             EndPath(hdc);    
  92.             FillPath(hdc);
  93.         }
  94.         
  95.         DrawText(hdc, buf.c_str(), -1, &lpsi->rcFrame, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  96.         AVIStreamWrite(pavi, fCnt, 1, lpBits, bmiHeader.biSizeImage, AVIIF_KEYFRAME, NULL, NULL); //一コマ書き出し
  97.         
  98.         if(fCnt%(div+1) == div){
  99.             isTop = !isTop; //裏表逆転
  100.         }
  101.     }
  102.     
  103.     
  104.     SelectObject(hdc,hPrevBrush);
  105.     DeleteObject(hBrush);
  106.     SelectObject(hdc, hFontOld);
  107.     DeleteObject(hFont);
  108.     SelectObject(hdc, hbmpMemPrev);
  109.     DeleteObject(hbmpMem);
  110.     DeleteDC(hdc);
  111.     AVIStreamRelease(pavi);
  112.     AVIFileRelease(pfile);
  113.     AVIFileExit();
  114.     MessageBox(NULL, L"Done.", L"OK", MB_OK);
  115.     return 0;
  116. }

劇遅 メモリ交換だけで古いセレロンパソコンを使えるレベルにアップグレード esprimo FH52/W 2950M SP016GLSTU160N22

Amazonでメモリを購入します ウチにある古いパソコンの富士通ESPRIMO FH52/W が酷く遅くなりました。OSの起動も遅く、アプリの起動も遅いです。さらにブラウザや事務ソフト程度の軽いソフトも突然固まります。30秒くらい待っていればまた使えるようになりますがストレスはあ...