Skip to content

Commit 697176e

Browse files
authored
Disable FFmpeg logs for encoder (#625)
1 parent 3280b90 commit 697176e

File tree

5 files changed

+38
-37
lines changed

5 files changed

+38
-37
lines changed

src/torchcodec/_core/Encoder.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ void validateSampleRate(const AVCodec& avCodec, int sampleRate) {
3737

3838
AudioEncoder::~AudioEncoder() {}
3939

40-
// TODO-ENCODING: disable ffmpeg logs by default
41-
4240
AudioEncoder::AudioEncoder(
4341
const torch::Tensor wf,
4442
int sampleRate,
@@ -51,6 +49,8 @@ AudioEncoder::AudioEncoder(
5149
wf_.dtype());
5250
TORCH_CHECK(
5351
wf_.dim() == 2, "waveform must have 2 dimensions, got ", wf_.dim());
52+
53+
setFFmpegLogLevel();
5454
AVFormatContext* avFormatContext = nullptr;
5555
auto status = avformat_alloc_output_context2(
5656
&avFormatContext, nullptr, nullptr, fileName.data());

src/torchcodec/_core/FFMPEGCommon.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,38 @@ SwrContext* allocateSwrContext(
158158
return swrContext;
159159
}
160160

161+
void setFFmpegLogLevel() {
162+
auto logLevel = AV_LOG_QUIET;
163+
const char* logLevelEnvPtr = std::getenv("TORCHCODEC_FFMPEG_LOG_LEVEL");
164+
if (logLevelEnvPtr != nullptr) {
165+
std::string logLevelEnv(logLevelEnvPtr);
166+
if (logLevelEnv == "QUIET") {
167+
logLevel = AV_LOG_QUIET;
168+
} else if (logLevelEnv == "PANIC") {
169+
logLevel = AV_LOG_PANIC;
170+
} else if (logLevelEnv == "FATAL") {
171+
logLevel = AV_LOG_FATAL;
172+
} else if (logLevelEnv == "ERROR") {
173+
logLevel = AV_LOG_ERROR;
174+
} else if (logLevelEnv == "WARNING") {
175+
logLevel = AV_LOG_WARNING;
176+
} else if (logLevelEnv == "INFO") {
177+
logLevel = AV_LOG_INFO;
178+
} else if (logLevelEnv == "VERBOSE") {
179+
logLevel = AV_LOG_VERBOSE;
180+
} else if (logLevelEnv == "DEBUG") {
181+
logLevel = AV_LOG_DEBUG;
182+
} else if (logLevelEnv == "TRACE") {
183+
logLevel = AV_LOG_TRACE;
184+
} else {
185+
TORCH_CHECK(
186+
false,
187+
"Invalid TORCHCODEC_FFMPEG_LOG_LEVEL: ",
188+
logLevelEnv,
189+
". Use e.g. 'QUIET', 'PANIC', 'VERBOSE', etc.");
190+
}
191+
}
192+
av_log_set_level(logLevel);
193+
}
194+
161195
} // namespace facebook::torchcodec

src/torchcodec/_core/FFMPEGCommon.h

+2
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,6 @@ SwrContext* allocateSwrContext(
168168
// Returns true if sws_scale can handle unaligned data.
169169
bool canSwsScaleHandleUnalignedData();
170170

171+
void setFFmpegLogLevel();
172+
171173
} // namespace facebook::torchcodec

src/torchcodec/_core/SingleStreamDecoder.cpp

-34
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "src/torchcodec/_core/SingleStreamDecoder.h"
88
#include <cstdint>
99
#include <cstdio>
10-
#include <cstdlib>
1110
#include <iostream>
1211
#include <limits>
1312
#include <sstream>
@@ -184,39 +183,6 @@ void SingleStreamDecoder::initializeDecoder() {
184183
initialized_ = true;
185184
}
186185

187-
void SingleStreamDecoder::setFFmpegLogLevel() {
188-
auto logLevel = AV_LOG_QUIET;
189-
const char* logLevelEnv = std::getenv("TORCHCODEC_FFMPEG_LOG_LEVEL");
190-
if (logLevelEnv != nullptr) {
191-
if (std::strcmp(logLevelEnv, "QUIET") == 0) {
192-
logLevel = AV_LOG_QUIET;
193-
} else if (std::strcmp(logLevelEnv, "PANIC") == 0) {
194-
logLevel = AV_LOG_PANIC;
195-
} else if (std::strcmp(logLevelEnv, "FATAL") == 0) {
196-
logLevel = AV_LOG_FATAL;
197-
} else if (std::strcmp(logLevelEnv, "ERROR") == 0) {
198-
logLevel = AV_LOG_ERROR;
199-
} else if (std::strcmp(logLevelEnv, "WARNING") == 0) {
200-
logLevel = AV_LOG_WARNING;
201-
} else if (std::strcmp(logLevelEnv, "INFO") == 0) {
202-
logLevel = AV_LOG_INFO;
203-
} else if (std::strcmp(logLevelEnv, "VERBOSE") == 0) {
204-
logLevel = AV_LOG_VERBOSE;
205-
} else if (std::strcmp(logLevelEnv, "DEBUG") == 0) {
206-
logLevel = AV_LOG_DEBUG;
207-
} else if (std::strcmp(logLevelEnv, "TRACE") == 0) {
208-
logLevel = AV_LOG_TRACE;
209-
} else {
210-
TORCH_CHECK(
211-
false,
212-
"Invalid TORCHCODEC_FFMPEG_LOG_LEVEL: ",
213-
logLevelEnv,
214-
". Use e.g. 'QUIET', 'PANIC', 'VERBOSE', etc.");
215-
}
216-
}
217-
av_log_set_level(logLevel);
218-
}
219-
220186
int SingleStreamDecoder::getBestStreamIndex(AVMediaType mediaType) {
221187
AVCodecOnlyUseForCallingAVFindBestStream avCodec = nullptr;
222188
int streamIndex =

src/torchcodec/_core/SingleStreamDecoder.h

-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ class SingleStreamDecoder {
249249
// --------------------------------------------------------------------------
250250

251251
void initializeDecoder();
252-
void setFFmpegLogLevel();
253252
// --------------------------------------------------------------------------
254253
// DECODING APIS AND RELATED UTILS
255254
// --------------------------------------------------------------------------

0 commit comments

Comments
 (0)