1. 程式人生 > 其它 >mac下搭建CLion-FFmpeg開發環境

mac下搭建CLion-FFmpeg開發環境

brew安裝ffmpeg

brew install ffmpeg
這裡如果安裝失敗,可以嘗試切換brew源

確定ffmpeg安裝位置

brew info ffmpeg

CLion新建c工程

修改CMakeLists.txt

cmake_minimum_required(VERSION 3.20)
project(ffmpeg_base C)

set(CMAKE_C_STANDARD 11)

# FFmpeg的安裝目錄,可以通過命令"brew info ffmpeg"獲取
set(FFMPEG_DIR /usr/local/Cellar/ffmpeg/4.4_2)
# 標頭檔案搜尋路徑
include_directories(${FFMPEG_DIR}/include/)
# 動態連結庫或靜態連結庫的搜尋路徑
link_directories(${FFMPEG_DIR}/lib/)

add_executable(ffmpeg_base main.c)
target_link_libraries(ffmpeg_base
        swscale swresample avcodec avutil avdevice avfilter avformat
        )

執行main.c

這裡使用ffmpeg官方提供的獲取元資料示例程式(metadata.c)來測試ffmpeg是否配置成功

/**
 * @file
 * Shows how the metadata API can be used in application programs.
 * @example metadata.c
 */

#include <stdio.h>

#include <libavformat/avformat.h>
#include <libavutil/dict.h>

int main (int argc, char **argv) {
  AVFormatContext *fmt_ctx = NULL;
  AVDictionaryEntry *tag = NULL;
  int ret;

    if (argc != 2) {
        printf("usage: %s <input_file>\n"
               "example program to demonstrate the use of the libavformat metadata API.\n"
               "\n", argv[0]);
        return 1;
    }

  if ((ret = avformat_open_input(&fmt_ctx, argv[1], NULL, NULL)))
    return ret;

  if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
    av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
    return ret;
  }

  while ((tag = av_dict_get(fmt_ctx->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
    printf("%s=%s\n", tag->key, tag->value);

  avformat_close_input(&fmt_ctx);
  return 0;
}