当前位置:   article > 正文

C++ CMake FFmpeg配置

C++ CMake FFmpeg配置
SDK下载  github

环境变量配置

cmake_modules/FindFFmpeg.cmake
  1. # This module defines the following variables:
  2. #
  3. # FFmpeg_FOUND - All required components and the core library were found
  4. # FFmpeg_INCLUDE_DIRS - Combined list of all components include dirs
  5. # FFmpeg_LIBRARIES - Combined list of all components libraries
  6. # FFmpeg_VERSION - Version defined in libavutil/ffversion.h
  7. #
  8. # ffmpeg::ffmpeg - FFmpeg target
  9. #
  10. # For each requested component the following variables are defined:
  11. #
  12. # FFmpeg_<component>_FOUND - The component was found
  13. # FFmpeg_<component>_INCLUDE_DIRS - The components include dirs
  14. # FFmpeg_<component>_LIBRARIES - The components libraries
  15. # FFmpeg_<component>_VERSION - The components version
  16. #
  17. # ffmpeg::<component> - The component target
  18. #
  19. # Usage:
  20. # find_package(FFmpeg REQUIRED)
  21. # find_package(FFmpeg 5.1.2 COMPONENTS avutil avcodec avformat avdevice avfilter REQUIRED)
  22. find_package(PkgConfig QUIET)
  23. # find the root dir for specified version
  24. function(find_ffmpeg_root_by_version EXPECTED_VERSION)
  25. set(FFMPEG_FIND_PATHS $ENV{PATH} $ENV{FFMPEG_PATH} $ENV{FFMPEG_ROOT} /opt /usr /sw)
  26. set(FOUND_VERSION)
  27. set(FOUND_ROOT_DIR)
  28. foreach(ROOT_DIR ${FFMPEG_FIND_PATHS})
  29. unset(FFMPEG_VERSION_HEADER CACHE)
  30. find_file(
  31. FFMPEG_VERSION_HEADER
  32. NAMES "ffversion.h"
  33. PATHS ${ROOT_DIR} ${ROOT_DIR}/include ${ROOT_DIR}/include/${CMAKE_LIBRARY_ARCHITECTURE}
  34. PATH_SUFFIXES libavutil
  35. NO_DEFAULT_PATH
  36. )
  37. mark_as_advanced(FFMPEG_VERSION_HEADER)
  38. if(NOT "${FFMPEG_VERSION_HEADER}" STREQUAL "FFMPEG_VERSION_HEADER-NOTFOUND")
  39. file(STRINGS "${FFMPEG_VERSION_HEADER}" FFMPEG_VERSION_STRING REGEX "FFMPEG_VERSION")
  40. # #define FFMPEG_VERSION "6.0-full_build-www.gyan.dev"
  41. # fixme: #define FFMPEG_VERSION "N-111059-gd78bffbf3d"
  42. string(REGEX REPLACE "#define FFMPEG_VERSION[ \t]+\"[n]?([0-9\\.]*).*\"" "\\1" CURRENT_VERSION "${FFMPEG_VERSION_STRING}")
  43. set(CURRENT_VERSION ${FFmpeg_FIND_VERSION})
  44. # not specified, return the first one
  45. if("${EXPECTED_VERSION}" STREQUAL "")
  46. set(FOUND_VERSION ${CURRENT_VERSION})
  47. set(FOUND_ROOT_DIR ${ROOT_DIR})
  48. break()
  49. endif()
  50. # otherwise, the minimum one of suitable versions
  51. if(${CURRENT_VERSION} VERSION_GREATER_EQUAL "${EXPECTED_VERSION}")
  52. if((NOT FOUND_VERSION) OR(${CURRENT_VERSION} VERSION_LESS "${FOUND_VERSION}"))
  53. set(FOUND_VERSION ${CURRENT_VERSION})
  54. set(FOUND_ROOT_DIR ${ROOT_DIR})
  55. endif()
  56. endif()
  57. endif()
  58. endforeach()
  59. set(FFmpeg_VERSION ${FOUND_VERSION} PARENT_SCOPE)
  60. set(FFmpeg_ROOT_DIR ${FOUND_ROOT_DIR} PARENT_SCOPE)
  61. endfunction()
  62. # find a ffmpeg component
  63. function(find_ffmpeg_component ROOT_DIR COMPONENT HEADER)
  64. # header
  65. find_path(
  66. FFmpeg_${COMPONENT}_INCLUDE_DIR
  67. NAMES "lib${COMPONENT}/${HEADER}" "lib${COMPONENT}/version.h"
  68. PATHS ${ROOT_DIR} ${ROOT_DIR}/include/${CMAKE_LIBRARY_ARCHITECTURE}
  69. PATH_SUFFIXES ffmpeg libav include
  70. NO_DEFAULT_PATH
  71. )
  72. # version
  73. if(EXISTS "${FFmpeg_${COMPONENT}_INCLUDE_DIR}/lib${COMPONENT}/version.h")
  74. if(EXISTS "${FFmpeg_${COMPONENT}_INCLUDE_DIR}/lib${COMPONENT}/version_major.h")
  75. file(STRINGS "${FFmpeg_${COMPONENT}_INCLUDE_DIR}/lib${COMPONENT}/version_major.h" MAJOR_VERSION_STRING REGEX "^.*VERSION_MAJOR[ \t]+[0-9]+[ \t]*$")
  76. endif()
  77. # other
  78. file(STRINGS "${FFmpeg_${COMPONENT}_INCLUDE_DIR}/lib${COMPONENT}/version.h" VERSION_STRING REGEX "^.*VERSION_(MAJOR|MINOR|MICRO)[ \t]+[0-9]+[ \t]*$")
  79. list(APPEND VERSION_STRING ${MAJOR_VERSION_STRING})
  80. string(REGEX REPLACE ".*VERSION_MAJOR[ \t]+([0-9]+).*" "\\1" MAJOR "${VERSION_STRING}")
  81. string(REGEX REPLACE ".*VERSION_MINOR[ \t]+([0-9]+).*" "\\1" MINOR "${VERSION_STRING}")
  82. string(REGEX REPLACE ".*VERSION_MICRO[ \t]+([0-9]+).*" "\\1" PATCH "${VERSION_STRING}")
  83. set(FFmpeg_${COMPONENT}_VERSION "${MAJOR}.${MINOR}.${PATCH}" PARENT_SCOPE)
  84. else()
  85. message(STATUS "'${FFmpeg_${COMPONENT}_INCLUDE_DIR}/lib${COMPONENT}/version.h' does not exist.")
  86. endif()
  87. # library
  88. if(WIN32)
  89. find_library(
  90. FFmpeg_${COMPONENT}_IMPLIB
  91. NAMES "${COMPONENT}" "lib${COMPONENT}"
  92. PATHS ${ROOT_DIR} ${ROOT_DIR}/lib/${CMAKE_LIBRARY_ARCHITECTURE}
  93. PATH_SUFFIXES lib lib64 bin bin64
  94. NO_DEFAULT_PATH
  95. )
  96. find_program(
  97. FFmpeg_${COMPONENT}_LIBRARY
  98. NAMES "${COMPONENT}-${MAJOR}.dll" "${COMPONENT}.dll"
  99. PATHS ${ROOT_DIR} ${ROOT_DIR}/bin
  100. NO_DEFAULT_PATH
  101. )
  102. else()
  103. find_library(
  104. FFmpeg_${COMPONENT}_LIBRARY
  105. NAMES "${COMPONENT}" "lib${COMPONENT}"
  106. PATHS ${ROOT_DIR} ${ROOT_DIR}/lib/${CMAKE_LIBRARY_ARCHITECTURE}
  107. PATH_SUFFIXES lib lib64 bin bin64
  108. NO_DEFAULT_PATH
  109. )
  110. endif()
  111. mark_as_advanced(FFmpeg_${COMPONENT}_INCLUDE_DIR FFmpeg_${COMPONENT}_LIBRARY FFmpeg_${COMPONENT}_IMPLIB)
  112. if(FFmpeg_${COMPONENT}_INCLUDE_DIR AND FFmpeg_${COMPONENT}_LIBRARY)
  113. set(FFmpeg_${COMPONENT}_FOUND TRUE PARENT_SCOPE)
  114. set(FFmpeg_${COMPONENT}_IMPLIBS ${FFmpeg_${COMPONENT}_IMPLIB} PARENT_SCOPE)
  115. set(FFmpeg_${COMPONENT}_LIBRARIES ${FFmpeg_${COMPONENT}_LIBRARY} PARENT_SCOPE)
  116. set(FFmpeg_${COMPONENT}_INCLUDE_DIRS ${FFmpeg_${COMPONENT}_INCLUDE_DIR} PARENT_SCOPE)
  117. endif()
  118. endfunction()
  119. # start finding
  120. if(NOT FFmpeg_FIND_COMPONENTS)
  121. list(APPEND FFmpeg_FIND_COMPONENTS avutil avcodec avdevice avfilter avformat swresample swscale postproc)
  122. endif()
  123. find_ffmpeg_root_by_version("${FFmpeg_FIND_VERSION}")
  124. if((NOT FFmpeg_VERSION) OR(NOT FFmpeg_ROOT_DIR))
  125. message(FATAL_ERROR "Can not find the suitable version.")
  126. endif()
  127. list(REMOVE_DUPLICATES FFmpeg_FIND_COMPONENTS)
  128. foreach(COMPONENT ${FFmpeg_FIND_COMPONENTS})
  129. if(COMPONENT STREQUAL "postproc")
  130. find_ffmpeg_component(${FFmpeg_ROOT_DIR} ${COMPONENT} "postprocess.h")
  131. else()
  132. find_ffmpeg_component(${FFmpeg_ROOT_DIR} ${COMPONENT} "${component}.h")
  133. endif()
  134. if(FFmpeg_${COMPONENT}_FOUND)
  135. list(APPEND FFmpeg_LIBRARIES ${FFmpeg_${COMPONENT}_LIBRARIES})
  136. list(APPEND FFmpeg_INCLUDE_DIRS ${FFmpeg_${COMPONENT}_INCLUDE_DIRS})
  137. endif()
  138. endforeach()
  139. list(REMOVE_DUPLICATES FFmpeg_LIBRARIES)
  140. list(REMOVE_DUPLICATES FFmpeg_INCLUDE_DIRS)
  141. #
  142. include(FindPackageHandleStandardArgs)
  143. find_package_handle_standard_args(
  144. FFmpeg
  145. FOUND_VAR FFmpeg_FOUND
  146. REQUIRED_VARS FFmpeg_ROOT_DIR FFmpeg_INCLUDE_DIRS FFmpeg_LIBRARIES
  147. VERSION_VAR FFmpeg_VERSION
  148. HANDLE_COMPONENTS
  149. )
  150. if(FFmpeg_FOUND)
  151. if(NOT TARGET ffmpeg::ffmpeg)
  152. add_library(ffmpeg::ffmpeg INTERFACE IMPORTED)
  153. endif()
  154. foreach(component IN LISTS FFmpeg_FIND_COMPONENTS)
  155. if(FFmpeg_${component}_FOUND AND NOT TARGET ffmpeg::${component})
  156. if(IS_ABSOLUTE "${FFmpeg_${component}_LIBRARIES}")
  157. if(DEFINED FFmpeg_${component}_IMPLIBS)
  158. if(FFmpeg_${component}_IMPLIBS STREQUAL FFmpeg_${component}_LIBRARIES)
  159. add_library(ffmpeg::${component} STATIC IMPORTED)
  160. else()
  161. add_library(ffmpeg::${component} SHARED IMPORTED)
  162. set_property(TARGET ffmpeg::${component} PROPERTY IMPORTED_IMPLIB "${FFmpeg_${component}_IMPLIBS}")
  163. endif()
  164. else()
  165. add_library(ffmpeg::${component} UNKNOWN IMPORTED)
  166. endif()
  167. set_property(TARGET ffmpeg::${component} PROPERTY IMPORTED_LOCATION "${FFmpeg_${component}_LIBRARIES}")
  168. else()
  169. add_library(ffmpeg::${component} INTERFACE IMPORTED)
  170. set_target_properties(ffmpeg::${component} PROPERTIES IMPORTED_LIBNAME "${FFmpeg_${component}_LIBRARIES}")
  171. endif()
  172. set_target_properties(ffmpeg::${component} PROPERTIES
  173. INTERFACE_INCLUDE_DIRECTORIES "${FFmpeg_${component}_INCLUDE_DIRS}"
  174. VERSION "${FFmpeg_${component}_VERSION}"
  175. )
  176. get_target_property(FFMPEG_INTERFACE_LIBRARIES ffmpeg::ffmpeg INTERFACE_LINK_LIBRARIES)
  177. if(NOT ffmpeg::${component} IN_LIST FFMPEG_INTERFACE_LIBRARIES)
  178. set_property(TARGET ffmpeg::ffmpeg APPEND PROPERTY INTERFACE_LINK_LIBRARIES ffmpeg::${component})
  179. endif()
  180. endif()
  181. endforeach()
  182. endif()
CMakeLists.txt
  1. cmake_minimum_required(VERSION 3.10)
  2. # 设置项目名称
  3. project(MyFFmpegExample VERSION 1.0.0 LANGUAGES CXX)
  4. set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
  5. find_package(FFmpeg 6 REQUIRED)
  6. target_link_libraries(test
  7.     PRIVATE
  8.         ffmpeg::ffmpeg
  9. )
  10. add_executable(${PROJECT_NAME} main.cpp)
main.cpp 
  1. #include <iostream>
  2. #include <libavformat/avformat.h>
  3. #include <libavcodec/avcodec.h>
  4. #include <libswscale/swscale.h>
  5. #include <libavutil/imgutils.h>
  6. using namespace std;
  7. int main(int argc, char** argv) {
  8. if (argc != 2) {
  9. cerr << "Usage: " << argv[0] << " <input-file>" << endl;
  10. return 1;
  11. }
  12. const char* input_filename = argv[1];
  13. // 初始化 FFmpeg 库
  14. av_register_all();
  15. avformat_network_init();
  16. // 打开输入文件
  17. AVFormatContext* format_ctx = nullptr;
  18. if (avformat_open_input(&format_ctx, input_filename, nullptr, nullptr) != 0) {
  19. cerr << "Could not open input file." << endl;
  20. return 1;
  21. }
  22. // 获取文件信息
  23. if (avformat_find_stream_info(format_ctx, nullptr) < 0) {
  24. cerr << "Failed to retrieve input stream information." << endl;
  25. return 1;
  26. }
  27. // 查找视频流
  28. int video_stream = -1;
  29. for (unsigned int i = 0; i < format_ctx->nb_streams; i++) {
  30. if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  31. video_stream = i;
  32. break;
  33. }
  34. }
  35. if (video_stream == -1) {
  36. cerr << "No video stream found." << endl;
  37. return 1;
  38. }
  39. // 获取视频编解码器上下文
  40. AVCodecParameters* codecpar = format_ctx->streams[video_stream]->codecpar;
  41. const AVCodec* decoder = avcodec_find_decoder(codecpar->codec_id);
  42. if (!decoder) {
  43. cerr << "Failed to find decoder for the video stream." << endl;
  44. return 1;
  45. }
  46. AVCodecContext* codec_ctx = avcodec_alloc_context3(decoder);
  47. if (avcodec_parameters_to_context(codec_ctx, codecpar) < 0) {
  48. cerr << "Failed to copy codec parameters to codec context." << endl;
  49. return 1;
  50. }
  51. // 打开编解码器
  52. if (avcodec_open2(codec_ctx, decoder, nullptr) < 0) {
  53. cerr << "Failed to open decoder." << endl;
  54. return 1;
  55. }
  56. // 分配帧
  57. AVFrame* frame = av_frame_alloc();
  58. // 分配包
  59. AVPacket packet;
  60. av_init_packet(&packet);
  61. // 逐帧解码
  62. int frame_count = 0;
  63. while (av_read_frame(format_ctx, &packet) >= 0) {
  64. if (packet.stream_index == video_stream) {
  65. // 解码帧
  66. if (avcodec_send_packet(codec_ctx, &packet) == 0) {
  67. while (avcodec_receive_frame(codec_ctx, frame) == 0) {
  68. // 输出帧信息
  69. cout << "Decoded frame " << ++frame_count << endl;
  70. // 重置包
  71. av_packet_unref(&packet);
  72. }
  73. }
  74. }
  75. // 重置包
  76. av_packet_unref(&packet);
  77. }
  78. // 清理
  79. avcodec_free_context(&codec_ctx);
  80. av_frame_free(&frame);
  81. avformat_close_input(&format_ctx);
  82. return 0;
  83. }


创作不易,小小的支持一下吧!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/972749
推荐阅读
相关标签
  

闽ICP备14008679号