Skip to content

Commit 7921558

Browse files
committed
Disable FFmpeg logs for encoder
1 parent 8b19f45 commit 7921558

File tree

5 files changed

+38
-37
lines changed

5 files changed

+38
-37
lines changed

src/torchcodec/_core/Encoder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ namespace facebook::torchcodec {
55

66
AudioEncoder::~AudioEncoder() {}
77

8-
// TODO-ENCODING: disable ffmpeg logs by default
9-
108
AudioEncoder::AudioEncoder(
119
const torch::Tensor wf,
1210
int sampleRate,
@@ -18,6 +16,8 @@ AudioEncoder::AudioEncoder(
1816
wf_.dtype());
1917
TORCH_CHECK(
2018
wf_.dim() == 2, "waveform must have 2 dimensions, got ", wf_.dim());
19+
20+
setFFmpegLogLevel();
2121
AVFormatContext* avFormatContext = nullptr;
2222
auto status = avformat_alloc_output_context2(
2323
&avFormatContext, nullptr, nullptr, fileName.data());

src/torchcodec/_core/FFMPEGCommon.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// LICENSE file in the root directory of this source tree.
66

77
#include "src/torchcodec/_core/FFMPEGCommon.h"
8+
#include <cstdlib>
89

910
#include <c10/util/Exception.h>
1011

@@ -158,4 +159,37 @@ SwrContext* allocateSwrContext(
158159
return swrContext;
159160
}
160161

162+
void setFFmpegLogLevel() {
163+
auto logLevel = AV_LOG_QUIET;
164+
const char* logLevelEnv = std::getenv("TORCHCODEC_FFMPEG_LOG_LEVEL");
165+
if (logLevelEnv != nullptr) {
166+
if (std::strcmp(logLevelEnv, "QUIET") == 0) {
167+
logLevel = AV_LOG_QUIET;
168+
} else if (std::strcmp(logLevelEnv, "PANIC") == 0) {
169+
logLevel = AV_LOG_PANIC;
170+
} else if (std::strcmp(logLevelEnv, "FATAL") == 0) {
171+
logLevel = AV_LOG_FATAL;
172+
} else if (std::strcmp(logLevelEnv, "ERROR") == 0) {
173+
logLevel = AV_LOG_ERROR;
174+
} else if (std::strcmp(logLevelEnv, "WARNING") == 0) {
175+
logLevel = AV_LOG_WARNING;
176+
} else if (std::strcmp(logLevelEnv, "INFO") == 0) {
177+
logLevel = AV_LOG_INFO;
178+
} else if (std::strcmp(logLevelEnv, "VERBOSE") == 0) {
179+
logLevel = AV_LOG_VERBOSE;
180+
} else if (std::strcmp(logLevelEnv, "DEBUG") == 0) {
181+
logLevel = AV_LOG_DEBUG;
182+
} else if (std::strcmp(logLevelEnv, "TRACE") == 0) {
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 0 additions & 34 deletions
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>
@@ -185,39 +184,6 @@ void SingleStreamDecoder::initializeDecoder() {
185184
initialized_ = true;
186185
}
187186

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

src/torchcodec/_core/SingleStreamDecoder.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,6 @@ class SingleStreamDecoder {
363363
// --------------------------------------------------------------------------
364364

365365
void initializeDecoder();
366-
void setFFmpegLogLevel();
367366
// --------------------------------------------------------------------------
368367
// DECODING APIS AND RELATED UTILS
369368
// --------------------------------------------------------------------------

0 commit comments

Comments
 (0)