Skip to content

Commit

Permalink
Add support for decoding input with ffmpeg libs in main (Linux)
Browse files Browse the repository at this point in the history
- search for ffmpeg at cmake time
- include ffmpegs/libav headers in main.cpp
Todo:
- if needed convert input file to wav 16khz in main.cpp
  • Loading branch information
WilliamTambellini committed May 8, 2024
1 parent 58210d6 commit 1099801
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
21 changes: 21 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ option(WHISPER_BUILD_EXAMPLES "whisper: build examples" ${WHISPER_STANDA

option(WHISPER_SDL2 "whisper: support for libSDL2" OFF)

if (CMAKE_SYSTEM_NAME MATCHES "Linux")
option(WHISPER_FFMPEG "whisper: support building and linking with ffmpeg libs" OFF)
endif()

option(WHISPER_NO_AVX "whisper: disable AVX" OFF)
option(WHISPER_NO_AVX2 "whisper: disable AVX2" OFF)
option(WHISPER_NO_AVX512 "whisper: disable AVX512" ON)
Expand Down Expand Up @@ -125,6 +129,23 @@ else()
set(CMAKE_CXX_STANDARD 11)
endif()

if (WHISPER_FFMPEG)
# As today (cmake 3.27), there is no official cmake support for FindFFmpeg. eg: not possible:
# find_package(FFmpeg COMPONENTS AVCODEC AVFORMAT AVUTIL AVDEVICE REQUIRED)
# Check with reviewers: worth to embed a FindFFmpeg.cmake script in whisper.cpp ?
# eg https://github.com/snikulov/cmake-modules/blob/master/FindFFmpeg.cmake
# Using PkgConfig atm:
find_package(PkgConfig REQUIRED)
pkg_check_modules(FFMPEG REQUIRED IMPORTED_TARGET libavdevice libavfilter libavformat libavcodec libswresample libswscale libavutil)
if (NOT ${FFMPEG_FOUND})
message(FATAL_ERROR "Cannot find ffmpeg libs/headers")
endif()
message(STATUS "Found ffmpeg headers in: ${FFMPEG_INCLUDE_DIRS}")
include_directories(${FFMPEG_INCLUDE_DIRS})
add_compile_definitions(WHISPER_FFMPEG)
set(WHISPER_EXTRA_LIBS ${WHISPER_EXTRA_LIBS} PkgConfig::FFMPEG)
endif()

# on APPLE
if (APPLE)
# include Accelerate framework
Expand Down
6 changes: 5 additions & 1 deletion examples/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ add_executable(${TARGET} main.cpp)

include(DefaultTargetOptions)

target_link_libraries(${TARGET} PRIVATE common whisper ${CMAKE_THREAD_LIBS_INIT})
if (WHISPER_FFMPEG)
target_link_libraries(${TARGET} PRIVATE common whisper ${CMAKE_THREAD_LIBS_INIT} PkgConfig::FFMPEG)
else()
target_link_libraries(${TARGET} PRIVATE common whisper ${CMAKE_THREAD_LIBS_INIT})
endif()
6 changes: 6 additions & 0 deletions examples/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
#include "whisper.h"
#include "grammar-parser.h"

#ifdef WHISPER_FFMPEG
#include <libavutil/frame.h>
#include <libavutil/mem.h>
#include <libavcodec/avcodec.h>
#endif

#include <cmath>
#include <fstream>
#include <cstdio>
Expand Down

0 comments on commit 1099801

Please sign in to comment.