common : re-implement ffmpeg-transcode.cpp + clarify ffmpeg usage (#3846)

* examples : remove ffmpeg-transcode.cpp

* examples : implement ffmpeg-transcode.cpp

Assisted-by: llama.cpp:local pi

* common : switch from WHISPER_FFMPEG -> WHISPER_COMMON_FFMPEG
This commit is contained in:
Georgi Gerganov
2026-05-31 15:44:07 +03:00
committed by GitHub
parent f24588a272
commit f39cc71282
6 changed files with 275 additions and 386 deletions
+47 -33
View File
@@ -34,8 +34,8 @@
#include <cstring>
#include <fstream>
#ifdef WHISPER_FFMPEG
// as implemented in ffmpeg_trancode.cpp only embedded in common lib if whisper built with ffmpeg support
#ifdef WHISPER_COMMON_FFMPEG
// as implemented in ffmpeg-trancode.cpp only embedded in common lib if whisper built with ffmpeg support
extern bool ffmpeg_decode_audio(const std::string & ifname, std::vector<uint8_t> & wav_data);
#endif
@@ -75,7 +75,7 @@ static bool read_audio_from_decoder(ma_decoder & decoder, std::vector<float> & p
return true;
}
bool read_audio_data(const std::string & fname, std::vector<float>& pcmf32, std::vector<std::vector<float>>& pcmf32s, bool stereo) {
bool read_audio_data(const std::string & fname, std::vector<float> & pcmf32, std::vector<std::vector<float>> & pcmf32s, bool stereo) {
std::vector<uint8_t> audio_data; // used for pipe input from stdin or ffmpeg decoding output
ma_result result;
@@ -96,53 +96,67 @@ bool read_audio_data(const std::string & fname, std::vector<float>& pcmf32, std:
decoder_config = ma_decoder_config_init(ma_format_f32, stereo ? 2 : 1, WHISPER_SAMPLE_RATE);
if (fname == "-") {
#ifdef _WIN32
_setmode(_fileno(stdin), _O_BINARY);
#endif
#ifdef _WIN32
_setmode(_fileno(stdin), _O_BINARY);
#endif
uint8_t buf[1024];
while (true)
{
const size_t n = fread(buf, 1, sizeof(buf), stdin);
if (n == 0) {
break;
}
audio_data.insert(audio_data.end(), buf, buf + n);
}
uint8_t buf[1024];
while (true)
{
const size_t n = fread(buf, 1, sizeof(buf), stdin);
if (n == 0) {
break;
}
audio_data.insert(audio_data.end(), buf, buf + n);
}
result = ma_decoder_init_memory(audio_data.data(), audio_data.size(), &decoder_config, &decoder);
result = ma_decoder_init_memory(audio_data.data(), audio_data.size(), &decoder_config, &decoder);
if (result != MA_SUCCESS) {
fprintf(stderr, "Error: failed to open audio data from stdin (%s)\n", ma_result_description(result));
return false;
}
fprintf(stderr, "%s: failed to open audio data from stdin (%s)\n", __func__, ma_result_description(result));
return false;
}
decoder.initialized = true;
fprintf(stderr, "%s: read %zu bytes from stdin\n", __func__, audio_data.size());
}
else {
result = ma_decoder_init_file(fname.c_str(), &decoder_config, &decoder);
if (result == MA_SUCCESS) {
decoder.initialized = true;
fprintf(stderr, "%s: read %zu bytes from stdin\n", __func__, audio_data.size());
} else {
fprintf(stderr, "%s: reading audio data from '%s' ...\n", __func__, fname.c_str());
// first try miniaudio. if it fails (or skipped) - try ffmpeg
{
const char * skip = getenv("WHISPER_COMMON_MINIAUDIO_SKIP");
if (!skip || strlen(skip) == 0 || strcmp(skip, "0") == 0) {
fprintf(stderr, "%s: trying to decode with miniaudio\n", __func__);
result = ma_decoder_init_file(fname.c_str(), &decoder_config, &decoder);
if (result == MA_SUCCESS) {
decoder.initialized = true;
}
} else {
fprintf(stderr, "%s: skipping miniaudio\n", __func__);
}
}
#if defined(WHISPER_FFMPEG)
#if defined(WHISPER_COMMON_FFMPEG)
if (!decoder.initialized) {
fprintf(stderr, "%s: trying to decode with ffmpeg\n", __func__);
if (ffmpeg_decode_audio(fname, audio_data) != 0) {
fprintf(stderr, "error: failed to ffmpeg decode '%s'\n", fname.c_str());
fprintf(stderr, "%s: failed to ffmpeg decode\n", __func__);
return false;
}
result = ma_decoder_init_memory(audio_data.data(), audio_data.size(), &decoder_config, &decoder);
if (result != MA_SUCCESS) {
fprintf(stderr, "error: failed to read audio data as wav (%s)\n", ma_result_description(result));
fprintf(stderr, "%s: failed to read audio data as wav (%s)\n", __func__, ma_result_description(result));
return false;
}
decoder.initialized = true;
}
#else
if (!decoder.initialized) {
fprintf(stderr, "error: failed to read audio data from (%s)\n", fname.c_str());
return false;
}
#endif
if (!decoder.initialized) {
fprintf(stderr, "%s: failed to read audio data\n", __func__);
return false;
}
}
return read_audio_from_decoder(decoder.decoder, pcmf32, pcmf32s, stereo);