Skip to content

Commit 99e3e22

Browse files
KhoraLeeAniLeo
authored andcommitted
cmake: download and use only necessary prebuilt FFMPEG
1 parent f9ae368 commit 99e3e22

22 files changed

+63
-22
lines changed

CMakeLists.txt

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,75 @@ endif()
55
add_library(${FFMPEG_CORE_NAME} INTERFACE)
66

77
target_include_directories(${FFMPEG_CORE_NAME} INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
8+
9+
# Detect OS
810
if (WIN32 AND NOT MINGW)
11+
set(FFMPEG_PREBUILTS_NAME "ffmpeg-windows")
12+
elseif (APPLE)
13+
set(FFMPEG_PREBUILTS_NAME "ffmpeg-macos")
14+
elseif (UNIX)
15+
set(FFMPEG_PREBUILTS_NAME "ffmpeg-linux")
16+
else ()
17+
message(FATAL_ERROR "Unsupported OS.")
18+
endif ()
19+
20+
# Detect Architecture
21+
if(NOT DEFINED ARCHITECTURE)
22+
message(FATAL_ERROR "ARCHITECTURE variable is not set up")
23+
elseif (ARCHITECTURE STREQUAL "x86_64")
24+
set(FFMPEG_PREBUILTS_NAME "${FFMPEG_PREBUILTS_NAME}-x64.zip")
25+
elseif(ARCHITECTURE STREQUAL "arm64")
26+
set(FFMPEG_PREBUILTS_NAME "${FFMPEG_PREBUILTS_NAME}-arm64.zip")
27+
elseif(APPLE AND "x86_64" IN_LIST ARCHITECTURE AND "arm64" IN_LIST ARCHITECTURE) # macOS universal
28+
set(FFMPEG_PREBUILTS_NAME "${FFMPEG_PREBUILTS_NAME}-universal.zip")
29+
else ()
30+
message(FATAL_ERROR "Unsupported architecture.")
31+
endif()
32+
33+
# Compute current short git commit SHA
34+
execute_process(
35+
COMMAND git rev-parse --short HEAD
36+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
37+
OUTPUT_VARIABLE FFMPEG_GIT_SHA
38+
OUTPUT_STRIP_TRAILING_WHITESPACE)
39+
40+
# Download prebuilt ffmpeg
41+
if(NOT EXISTS "${CMAKE_BINARY_DIR}/external/ffmpeg.zip")
42+
message(STATUS "Downloading FFMPEG prebuilts...")
43+
file(DOWNLOAD https://github.com/Vita3K/ffmpeg-core/releases/download/${FFMPEG_GIT_SHA}/${FFMPEG_PREBUILTS_NAME}
44+
"${CMAKE_BINARY_DIR}/external/ffmpeg.zip" SHOW_PROGRESS
45+
STATUS FILE_STATUS)
46+
list(GET FILE_STATUS 0 STATUS_CODE)
47+
if (NOT STATUS_CODE EQUAL 0)
48+
file(REMOVE "${CMAKE_BINARY_DIR}/external/ffmpeg.zip") # CMake create 0 byte file even if URL is invalid. So need to delete it.
49+
message(FATAL_ERROR "No FFMPEG prebuilt found with corresponding commit SHA (${FFMPEG_GIT_SHA})")
50+
endif()
51+
endif()
52+
53+
if(NOT EXISTS "${CMAKE_BINARY_DIR}/external/ffmpeg/lib")
54+
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/external/ffmpeg/lib")
55+
execute_process(COMMAND ${CMAKE_COMMAND} -E tar xzf "${CMAKE_BINARY_DIR}/external/ffmpeg.zip"
56+
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/external/ffmpeg/lib")
57+
endif()
58+
59+
set(LIB_PREFIX "lib")
60+
set(LIB_EXT "a")
61+
if (WIN32 AND NOT MINGW)
62+
set(LIB_PREFIX "")
63+
set(LIB_EXT "lib")
964
target_link_libraries(${FFMPEG_CORE_NAME} INTERFACE
10-
"${CMAKE_CURRENT_SOURCE_DIR}/lib/windows/x86_64/avformat.lib"
11-
"${CMAKE_CURRENT_SOURCE_DIR}/lib/windows/x86_64/avcodec.lib"
12-
"${CMAKE_CURRENT_SOURCE_DIR}/lib/windows/x86_64/swscale.lib"
13-
"${CMAKE_CURRENT_SOURCE_DIR}/lib/windows/x86_64/avutil.lib"
14-
"${CMAKE_CURRENT_SOURCE_DIR}/lib/windows/x86_64/avfilter.lib"
15-
"${CMAKE_CURRENT_SOURCE_DIR}/lib/windows/x86_64/swresample.lib"
1665
"psapi;strmiids;uuid;oleaut32;shlwapi;ws2_32;ole32;user32;bcrypt")
1766
elseif (APPLE)
1867
target_link_libraries(${FFMPEG_CORE_NAME} INTERFACE
19-
"${CMAKE_CURRENT_SOURCE_DIR}/lib/macos/libavformat.a"
20-
"${CMAKE_CURRENT_SOURCE_DIR}/lib/macos/libavcodec.a"
21-
"${CMAKE_CURRENT_SOURCE_DIR}/lib/macos/libswscale.a"
22-
"${CMAKE_CURRENT_SOURCE_DIR}/lib/macos/libavutil.a"
23-
"${CMAKE_CURRENT_SOURCE_DIR}/lib/macos/libavfilter.a"
24-
"${CMAKE_CURRENT_SOURCE_DIR}/lib/macos/libswresample.a"
2568
"-framework CoreServices" "-framework CoreFoundation" "-framework AudioUnit"
2669
"-framework AudioToolbox" "-framework CoreAudio" "-framework CoreMedia"
2770
"-framework VideoToolbox" "-framework CoreVideo" "-framework Security")
28-
elseif (UNIX)
29-
target_link_libraries(${FFMPEG_CORE_NAME} INTERFACE
30-
"${CMAKE_CURRENT_SOURCE_DIR}/lib/linux/x86_64/libavformat.a"
31-
"${CMAKE_CURRENT_SOURCE_DIR}/lib/linux/x86_64/libavcodec.a"
32-
"${CMAKE_CURRENT_SOURCE_DIR}/lib/linux/x86_64/libswscale.a"
33-
"${CMAKE_CURRENT_SOURCE_DIR}/lib/linux/x86_64/libavutil.a"
34-
"${CMAKE_CURRENT_SOURCE_DIR}/lib/linux/x86_64/libavfilter.a"
35-
"${CMAKE_CURRENT_SOURCE_DIR}/lib/linux/x86_64/libswresample.a")
36-
else ()
37-
message(FATAL_ERROR "No prebuilt was found for ffmpeg")
3871
endif ()
72+
73+
target_link_libraries(${FFMPEG_CORE_NAME} INTERFACE
74+
"${CMAKE_BINARY_DIR}/external/ffmpeg/lib/${LIB_PREFIX}avformat.${LIB_EXT}"
75+
"${CMAKE_BINARY_DIR}/external/ffmpeg/lib/${LIB_PREFIX}avcodec.${LIB_EXT}"
76+
"${CMAKE_BINARY_DIR}/external/ffmpeg/lib/${LIB_PREFIX}swscale.${LIB_EXT}"
77+
"${CMAKE_BINARY_DIR}/external/ffmpeg/lib/${LIB_PREFIX}avutil.${LIB_EXT}"
78+
"${CMAKE_BINARY_DIR}/external/ffmpeg/lib/${LIB_PREFIX}avfilter.${LIB_EXT}"
79+
"${CMAKE_BINARY_DIR}/external/ffmpeg/lib/${LIB_PREFIX}swresample.${LIB_EXT}")

lib/linux/x86_64/libavcodec.a

-27 MB
Binary file not shown.

lib/linux/x86_64/libavdevice.a

-252 KB
Binary file not shown.

lib/linux/x86_64/libavfilter.a

-1.08 MB
Binary file not shown.

lib/linux/x86_64/libavformat.a

-6.02 MB
Binary file not shown.

lib/linux/x86_64/libavutil.a

-4.06 MB
Binary file not shown.

lib/linux/x86_64/libswresample.a

-631 KB
Binary file not shown.

lib/linux/x86_64/libswscale.a

-5.17 MB
Binary file not shown.

lib/macos/libavcodec.a

-34.8 MB
Binary file not shown.

lib/macos/libavdevice.a

-289 KB
Binary file not shown.

0 commit comments

Comments
 (0)