金子邦彦研究室インストールUbuntu, WSL2Ubuntu で libav を使ってみる

Ubuntu で libav を使ってみる

libav のインストール手順別のページで説明している

libav チュートリアルのプログラム tutorial01.c,tutorial02.c を動かしてみる

tutorial01.c はビデオのデコード、tutorial02.c はビデオの画面表示の例です.

  1. libav チュートリアルの tutorial01 の Web ページを開く

    http://dranger.com/ffmpeg/tutorial01.html

  2. tutorial01.c, tutorial02.c をダウンロード
  3. tutorial01.c, tutorial02.c を次のように変更する
    1. #include の部分

      理由: Ubuntu でコンパイルできるようにするため,次のように変更する

      <変更前>

      #include<ffmpeg/avcodec.h>
      #include<ffmpeg/avformat.h>
      

      <変更後>

      #include<libavcodec/avcodec.h>
      #include<libavformat/avformat.h>
      #include<libswscale/swscale.h>
      
    2. 「av_open_input_file」は「avformat_open_input」を使うように変更

      <変更前>

        if(av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL)!=0)
          return -1; // Couldn't open file
      

      <変更後>

        AVDictionary *opts = NULL;
        if(avformat_open_input(&pFormatCtx, argv[1], NULL, &opts)!=0)
          return -1; // Couldn't open file
      

      ◆ エディタの画面

      [image]
    3. codec_type==...」の部分の変更

      <変更前>

      if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) {
      

      <変更後>

      if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
      
    4. 「avcodec_open」は「avcodec_open2」を使うように変更

      <変更前>

        // Open codec
        if(avcodec_open(pCodecCtx, pCodec)<0)
          return -1; // Could not open codec
      

      <変更後>

        // Open codec
        av_dict_set(&opts, /* key */ "b", /* value */ NULL, 0);
        if(avcodec_open2(pCodecCtx, pCodec, &opts)<0)
          return -1; // Could not open codec
      
    5. 「avcodec_decode_video」は「avcodec_decode_video2」を使うように変更

      <変更前>

           avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, 
           packet.data, packet.size);
      

      <変更後>

      avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
      
    6. img_convert に関する修正

      <変更前>

      tutorial01.c

        img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24, 
                          (AVPicture*)pFrame, pCodecCtx->pix_fmt, pCodecCtx->width, 
                          pCodecCtx->height);
      

        img_convert(&pict, PIX_FMT_YUV420P,
                          (AVPicture *)pFrame, pCodecCtx->pix_fmt, 
            pCodecCtx->width, pCodecCtx->height);
      

      http://ffmpeg.arrozcru.com/forum/viewtopic.php?f=1&t=319 (現存しない) に記載の指示に従う

      ◆ エディタの画面 (tutorial01.c)

      [image]

      ◆ エディタの画面 (tutorial2.c)

      [image]
    7. 「SwsContext」は「struct SwsContext」に変更

      <変更前>

      //Declation
      SwsContext *pSWSCtx;
      

      <変更後>

      //Declation
      struct SwsContext *pSWSCtx;
      
  4. コンパイル

    tutorial01.c の場合

    gcc -o tutorial01 tutorial01.c  -lavcodec -lavformat -lswscale -lavutil
    

    [image]
    tutorial02.c の場合
    gcc -I/usr/include/SDL -o tutorial02 tutorial02.c  -lavcodec -lavformat -lswscale -lavutil -lSDL
    

    [image]
  5. 動画ファイルを自前で準備した後,プログラムを実行してみる

    [image]

    tutorial01.c の場合、 プログラムを実行すると, frame1.ppm, frame2.ppm, frame3.ppm, frame4.ppm, frame5.ppm ができる

    tutorial02.c は画面表示である