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 1 commit
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 @@ -5,8 +5,6 @@ namespace facebook::torchcodec {

AudioEncoder::~AudioEncoder() {}

// TODO-ENCODING: disable ffmpeg logs by default

AudioEncoder::AudioEncoder(
const torch::Tensor wf,
int sampleRate,
Expand All @@ -18,6 +16,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 @@ -5,6 +5,7 @@
// LICENSE file in the root directory of this source tree.

#include "src/torchcodec/_core/FFMPEGCommon.h"
#include <cstdlib>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need <cstring> to get std::strcmp (https://en.cppreference.com/w/cpp/string/byte/strcmp); see the errors in CI. It only sees strcmp from the C headers.

But, I think we can actually just make things simpler and avoid manipulating C-style strings:

int logLevel = AV_LOG_QUIET;
const char* logLevelPtr = std::getenv("TORCHCODEC_FFMPEG_LOG_LEVEL");
if (logLevelPtr != nullPtr) {
  std::string logLevelEnv(logLevelPtr);
  if (logLevelEnv == "QUIET") {
    logLevel = AV_LOG_QUIET;
  }
  else if (logLevelEnv == "PANIC") {
    logLevel = AV_LOG_PANIC;
  }
  ...
}

There's a marginal cost in copying the string, but we execute this code once at startup, and we should prefer C++ string operations over C-style string operations.


#include <c10/util/Exception.h>

Expand Down Expand Up @@ -158,4 +159,37 @@ SwrContext* allocateSwrContext(
return swrContext;
}

void 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);
}

} // 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 @@ -185,39 +184,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 @@ -363,7 +363,6 @@ class SingleStreamDecoder {
// --------------------------------------------------------------------------

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