Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A CMake Find module for FFMPEG that will tear the HD apart looking for the libs and includes ;)
# - Try to find FFMPEG
# Once done this will define
# FFMPEG_FOUND - System has FFMPEG
# FFMPEG_INCLUDE_DIRS - The FFMPEG include directories
# FFMPEG_LIBRARIES - The libraries needed to use FFMPEG
# FFMPEG_LIBRARY_DIRS - The directory to find FFMPEG libraries
#
# written by Roy Shilkrot 2013 http://www.morethantechnical.com/
#
find_package(PkgConfig)
MACRO(FFMPEG_FIND varname shortname headername)
IF(NOT WIN32)
PKG_CHECK_MODULES(PC_${varname} ${shortname})
FIND_PATH(${varname}_INCLUDE_DIR "${shortname}/${headername}"
HINTS ${PC_${varname}_INCLUDEDIR} ${PC_${varname}_INCLUDE_DIRS}
NO_DEFAULT_PATH
)
ELSE()
FIND_PATH(${varname}_INCLUDE_DIR "${shortname}/${headername}")
ENDIF()
IF(${varname}_INCLUDE_DIR STREQUAL "${varname}_INCLUDE_DIR-NOTFOUND")
message(STATUS "look for newer strcture")
IF(NOT WIN32)
PKG_CHECK_MODULES(PC_${varname} "lib${shortname}")
FIND_PATH(${varname}_INCLUDE_DIR "lib${shortname}/${headername}"
HINTS ${PC_${varname}_INCLUDEDIR} ${PC_${varname}_INCLUDE_DIRS}
NO_DEFAULT_PATH
)
ELSE()
FIND_PATH(${varname}_INCLUDE_DIR "lib${shortname}/${headername}")
IF(${${varname}_INCLUDE_DIR} STREQUAL "${varname}_INCLUDE_DIR-NOTFOUND")
#Desperate times call for desperate measures
MESSAGE(STATUS "globbing...")
FILE(GLOB_RECURSE ${varname}_INCLUDE_DIR "/ffmpeg*/${headername}")
MESSAGE(STATUS "found: ${${varname}_INCLUDE_DIR}")
IF(${varname}_INCLUDE_DIR)
GET_FILENAME_COMPONENT(${varname}_INCLUDE_DIR "${${varname}_INCLUDE_DIR}" PATH)
GET_FILENAME_COMPONENT(${varname}_INCLUDE_DIR "${${varname}_INCLUDE_DIR}" PATH)
ELSE()
SET(${varname}_INCLUDE_DIR "${varname}_INCLUDE_DIR-NOTFOUND")
ENDIF()
ENDIF()
ENDIF()
ENDIF()
IF(${${varname}_INCLUDE_DIR} STREQUAL "${varname}_INCLUDE_DIR-NOTFOUND")
MESSAGE(STATUS "Can't find includes for ${shortname}...")
ELSE()
MESSAGE(STATUS "Found ${shortname} include dirs: ${${varname}_INCLUDE_DIR}")
# GET_DIRECTORY_PROPERTY(FFMPEG_PARENT DIRECTORY ${${varname}_INCLUDE_DIR} PARENT_DIRECTORY)
GET_FILENAME_COMPONENT(FFMPEG_PARENT ${${varname}_INCLUDE_DIR} PATH)
MESSAGE(STATUS "Using FFMpeg dir parent as hint: ${FFMPEG_PARENT}")
IF(NOT WIN32)
FIND_LIBRARY(${varname}_LIBRARIES NAMES ${shortname}
HINTS ${PC_${varname}_LIBDIR} ${PC_${varname}_LIBRARY_DIR} ${FFMPEG_PARENT})
ELSE()
# FIND_PATH(${varname}_LIBRARIES "${shortname}.dll.a" HINTS ${FFMPEG_PARENT})
FILE(GLOB_RECURSE ${varname}_LIBRARIES "${FFMPEG_PARENT}/*${shortname}.lib")
# GLOBing is very bad... but windows sux, this is the only thing that works
ENDIF()
IF(${varname}_LIBRARIES STREQUAL "${varname}_LIBRARIES-NOTFOUND")
MESSAGE(STATUS "look for newer structure for library")
FIND_LIBRARY(${varname}_LIBRARIES NAMES lib${shortname}
HINTS ${PC_${varname}_LIBDIR} ${PC_${varname}_LIBRARY_DIR} ${FFMPEG_PARENT})
ENDIF()
IF(${varname}_LIBRARIES STREQUAL "${varname}_LIBRARIES-NOTFOUND")
MESSAGE(STATUS "Can't find lib for ${shortname}...")
ELSE()
MESSAGE(STATUS "Found ${shortname} libs: ${${varname}_LIBRARIES}")
ENDIF()
IF(NOT ${varname}_INCLUDE_DIR STREQUAL "${varname}_INCLUDE_DIR-NOTFOUND"
AND NOT ${varname}_LIBRARIES STREQUAL ${varname}_LIBRARIES-NOTFOUND)
MESSAGE(STATUS "found ${shortname}: include ${${varname}_INCLUDE_DIR} lib ${${varname}_LIBRARIES}")
SET(FFMPEG_${varname}_FOUND 1)
SET(FFMPEG_${varname}_INCLUDE_DIRS ${${varname}_INCLUDE_DIR})
SET(FFMPEG_${varname}_LIBS ${${varname}_LIBRARIES})
ELSE()
MESSAGE(STATUS "Can't find ${shortname}")
ENDIF()
ENDIF()
ENDMACRO(FFMPEG_FIND)
FFMPEG_FIND(LIBAVFORMAT avformat avformat.h)
FFMPEG_FIND(LIBAVDEVICE avdevice avdevice.h)
FFMPEG_FIND(LIBAVCODEC avcodec avcodec.h)
FFMPEG_FIND(LIBAVUTIL avutil avutil.h)
FFMPEG_FIND(LIBSWSCALE swscale swscale.h)
SET(FFMPEG_FOUND "NO")
IF (FFMPEG_LIBAVFORMAT_FOUND AND
FFMPEG_LIBAVDEVICE_FOUND AND
FFMPEG_LIBAVCODEC_FOUND AND
FFMPEG_LIBAVUTIL_FOUND AND
FFMPEG_LIBSWSCALE_FOUND
)
SET(FFMPEG_FOUND "YES")
SET(FFMPEG_INCLUDE_DIRS ${FFMPEG_LIBAVFORMAT_INCLUDE_DIRS})
SET(FFMPEG_LIBRARY_DIRS ${FFMPEG_LIBAVFORMAT_LIBRARY_DIRS})
SET(FFMPEG_LIBRARIES
${FFMPEG_LIBAVFORMAT_LIBS}
${FFMPEG_LIBAVDEVICE_LIBS}
${FFMPEG_LIBAVCODEC_LIBS}
${FFMPEG_LIBAVUTIL_LIBS}
${FFMPEG_LIBSWSCALE_LIBS}
)
ELSE ()
MESSAGE(STATUS "Could not find FFMPEG")
ENDIF()
message(STATUS ${FFMPEG_LIBRARIES} ${FFMPEG_LIBAVFORMAT_LIBRARIES})
include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set FFMPEG_FOUND to TRUE
# if all listed variables are TRUE
find_package_handle_standard_args(FFMPEG DEFAULT_MSG
FFMPEG_LIBRARIES FFMPEG_INCLUDE_DIRS)
mark_as_advanced(FFMPEG_INCLUDE_DIRS FFMPEG_LIBRARY_DIRS FFMPEG_LIBRARIES)
@webern

This comment has been minimized.

Copy link

webern commented Feb 25, 2019

This is awesome.

This line is suspicious though SET(FFMPEG_LIBRARY_DIRS ${FFMPEG_LIBAVFORMAT_LIBRARY_DIRS})

I have ffmpeg found but I have an empty FFMPEG_LIBRARY_DIRS

-- Checking for module 'avformat'
--   No package 'avformat' found
-- Found avformat include dirs: /usr/local/include
-- Using FFMpeg dir parent as hint: /usr/local
-- Found avformat libs: /usr/local/lib/libavformat.a
-- found avformat: include /usr/local/include lib /usr/local/lib/libavformat.a
-- Checking for module 'avdevice'
--   No package 'avdevice' found
-- Found avdevice include dirs: /usr/local/include
-- Using FFMpeg dir parent as hint: /usr/local
-- Found avdevice libs: /usr/local/lib/libavdevice.a
-- found avdevice: include /usr/local/include lib /usr/local/lib/libavdevice.a
-- Checking for module 'avcodec'
--   No package 'avcodec' found
-- Found avcodec include dirs: /usr/local/include
-- Using FFMpeg dir parent as hint: /usr/local
-- Found avcodec libs: /usr/local/lib/libavcodec.a
-- found avcodec: include /usr/local/include lib /usr/local/lib/libavcodec.a
-- Checking for module 'avutil'
--   No package 'avutil' found
-- Found avutil include dirs: /usr/local/include
-- Using FFMpeg dir parent as hint: /usr/local
-- Found avutil libs: /usr/local/lib/libavutil.a
-- found avutil: include /usr/local/include lib /usr/local/lib/libavutil.a
-- Checking for module 'swscale'
--   No package 'swscale' found
-- Found swscale include dirs: /usr/local/include
-- Using FFMpeg dir parent as hint: /usr/local
-- Found swscale libs: /usr/local/lib/libswscale.a
-- found swscale: include /usr/local/include lib /usr/local/lib/libswscale.a
-- /usr/local/lib/libavformat.a/usr/local/lib/libavdevice.a/usr/local/lib/libavcodec.a/usr/local/lib/libavutil.a/usr/local/lib/libswscale.a
FFMPEG_INCLUDE_DIRS = /usr/local/include
FFMPEG_LIBRARY_DIRS = 
FFMPEG_LIBRARIES = /usr/local/lib/libavformat.a;/usr/local/lib/libavdevice.a;/usr/local/lib/libavcodec.a;/usr/local/lib/libavutil.a;/usr/local/lib/libswscale.a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.