Skip to content

Disable FFmpeg logs for encoder #625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/torchcodec/_core/Encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ void validateSampleRate(const AVCodec& avCodec, int sampleRate) {

AudioEncoder::~AudioEncoder() {}

// TODO-ENCODING: disable ffmpeg logs by default

AudioEncoder::AudioEncoder(
const torch::Tensor wf,
int sampleRate,
Expand All @@ -51,6 +49,8 @@ AudioEncoder::AudioEncoder(
wf_.dtype());
TORCH_CHECK(
wf_.dim() == 2, "waveform must have 2 dimensions, got ", wf_.dim());

setFFmpegLogLevel();
AVFormatContext* avFormatContext = nullptr;
auto status = avformat_alloc_output_context2(
&avFormatContext, nullptr, nullptr, fileName.data());
Expand Down
34 changes: 34 additions & 0 deletions src/torchcodec/_core/FFMPEGCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,38 @@ SwrContext* allocateSwrContext(
return swrContext;
}

void setFFmpegLogLevel() {
auto logLevel = AV_LOG_QUIET;
const char* logLevelEnvPtr = std::getenv("TORCHCODEC_FFMPEG_LOG_LEVEL");
if (logLevelEnvPtr != nullptr) {
std::string logLevelEnv(logLevelEnvPtr);
if (logLevelEnv == "QUIET") {
logLevel = AV_LOG_QUIET;
} else if (logLevelEnv == "PANIC") {
logLevel = AV_LOG_PANIC;
} else if (logLevelEnv == "FATAL") {
logLevel = AV_LOG_FATAL;
} else if (logLevelEnv == "ERROR") {
logLevel = AV_LOG_ERROR;
} else if (logLevelEnv == "WARNING") {
logLevel = AV_LOG_WARNING;
} else if (logLevelEnv == "INFO") {
logLevel = AV_LOG_INFO;
} else if (logLevelEnv == "VERBOSE") {
logLevel = AV_LOG_VERBOSE;
} else if (logLevelEnv == "DEBUG") {
logLevel = AV_LOG_DEBUG;
} else if (logLevelEnv == "TRACE") {
logLevel = AV_LOG_TRACE;
} else {
TORCH_CHECK(
false,
"Invalid TORCHCODEC_FFMPEG_LOG_LEVEL: ",
logLevelEnv,
". Use e.g. 'QUIET', 'PANIC', 'VERBOSE', etc.");
}
}
av_log_set_level(logLevel);
}

} // namespace facebook::torchcodec
2 changes: 2 additions & 0 deletions src/torchcodec/_core/FFMPEGCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,6 @@ SwrContext* allocateSwrContext(
// Returns true if sws_scale can handle unaligned data.
bool canSwsScaleHandleUnalignedData();

void setFFmpegLogLevel();

} // namespace facebook::torchcodec
34 changes: 0 additions & 34 deletions src/torchcodec/_core/SingleStreamDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "src/torchcodec/_core/SingleStreamDecoder.h"
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <sstream>
Expand Down Expand Up @@ -184,39 +183,6 @@ void SingleStreamDecoder::initializeDecoder() {
initialized_ = true;
}

void SingleStreamDecoder::setFFmpegLogLevel() {
auto logLevel = AV_LOG_QUIET;
const char* logLevelEnv = std::getenv("TORCHCODEC_FFMPEG_LOG_LEVEL");
if (logLevelEnv != nullptr) {
if (std::strcmp(logLevelEnv, "QUIET") == 0) {
logLevel = AV_LOG_QUIET;
} else if (std::strcmp(logLevelEnv, "PANIC") == 0) {
logLevel = AV_LOG_PANIC;
} else if (std::strcmp(logLevelEnv, "FATAL") == 0) {
logLevel = AV_LOG_FATAL;
} else if (std::strcmp(logLevelEnv, "ERROR") == 0) {
logLevel = AV_LOG_ERROR;
} else if (std::strcmp(logLevelEnv, "WARNING") == 0) {
logLevel = AV_LOG_WARNING;
} else if (std::strcmp(logLevelEnv, "INFO") == 0) {
logLevel = AV_LOG_INFO;
} else if (std::strcmp(logLevelEnv, "VERBOSE") == 0) {
logLevel = AV_LOG_VERBOSE;
} else if (std::strcmp(logLevelEnv, "DEBUG") == 0) {
logLevel = AV_LOG_DEBUG;
} else if (std::strcmp(logLevelEnv, "TRACE") == 0) {
logLevel = AV_LOG_TRACE;
} else {
TORCH_CHECK(
false,
"Invalid TORCHCODEC_FFMPEG_LOG_LEVEL: ",
logLevelEnv,
". Use e.g. 'QUIET', 'PANIC', 'VERBOSE', etc.");
}
}
av_log_set_level(logLevel);
}

int SingleStreamDecoder::getBestStreamIndex(AVMediaType mediaType) {
AVCodecOnlyUseForCallingAVFindBestStream avCodec = nullptr;
int streamIndex =
Expand Down
1 change: 0 additions & 1 deletion src/torchcodec/_core/SingleStreamDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ class SingleStreamDecoder {
// --------------------------------------------------------------------------

void initializeDecoder();
void setFFmpegLogLevel();
// --------------------------------------------------------------------------
// DECODING APIS AND RELATED UTILS
// --------------------------------------------------------------------------
Expand Down
Loading