Skip to content

Commit 9ed11f3

Browse files
WilliamTambelliniggerganov
authored andcommitted
examples : add support for decoding input with ffmpeg (Linux) (whisper/2133)
- search for ffmpeg libs/headers at cmake time - added ffmpeg-transcode.cpp into libcommon if ffmpeg on - hooked ffmpeg trancoding in common read_wav(...) - passed test: ./main -m ggml-base.en.bin -f samples/jfk.mp3
1 parent ac1e9ae commit 9ed11f3

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

examples/common.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
#include <io.h>
2525
#endif
2626

27+
#ifdef WHISPER_FFMPEG
28+
// as implemented in ffmpeg_trancode.cpp only embedded in common lib if whisper built with ffmpeg support
29+
extern bool ffmpeg_decode_audio(const std::string & ifname, std::vector<uint8_t> & wav_data);
30+
#endif
31+
2732
// Function to check if the next argument exists
2833
std::string get_next_arg(int& i, int argc, char** argv, const std::string& flag, gpt_params& params) {
2934
if (i + 1 < argc && argv[i + 1][0] != '-') {
@@ -637,7 +642,7 @@ bool is_wav_buffer(const std::string buf) {
637642

638643
bool read_wav(const std::string & fname, std::vector<float>& pcmf32, std::vector<std::vector<float>>& pcmf32s, bool stereo) {
639644
drwav wav;
640-
std::vector<uint8_t> wav_data; // used for pipe input from stdin
645+
std::vector<uint8_t> wav_data; // used for pipe input from stdin or ffmpeg decoding output
641646

642647
if (fname == "-") {
643648
{
@@ -670,8 +675,19 @@ bool read_wav(const std::string & fname, std::vector<float>& pcmf32, std::vector
670675
}
671676
}
672677
else if (drwav_init_file(&wav, fname.c_str(), nullptr) == false) {
678+
#if defined(WHISPER_FFMPEG)
679+
if (ffmpeg_decode_audio(fname, wav_data) != 0) {
680+
fprintf(stderr, "error: failed to ffmpeg decode '%s' \n", fname.c_str());
681+
return false;
682+
}
683+
if (drwav_init_memory(&wav, wav_data.data(), wav_data.size(), nullptr) == false) {
684+
fprintf(stderr, "error: failed to read wav data as wav \n");
685+
return false;
686+
}
687+
#else
673688
fprintf(stderr, "error: failed to open '%s' as WAV file\n", fname.c_str());
674689
return false;
690+
#endif
675691
}
676692

677693
if (wav.channels != 1 && wav.channels != 2) {

0 commit comments

Comments
 (0)