Skip to content

Commit

Permalink
Preserve log level
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Dec 1, 2024
1 parent 95a8c55 commit 445d759
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
1 change: 1 addition & 0 deletions av/filter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .filter import Filter, FilterFlags, filter_descriptor, filters_available
from .graph import Graph
from .loudnorm import stats
1 change: 1 addition & 0 deletions av/filter/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .context import *
from .filter import *
from .graph import *
from .loudnorm import *
6 changes: 6 additions & 0 deletions av/filter/loudnorm.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ from av.audio.codeccontext cimport AudioCodecContext
from av.audio.stream cimport AudioStream
from av.container.core cimport Container
from av.stream cimport Stream
from av.logging import get_level, set_level


cdef extern from "libavcodec/avcodec.h":
Expand Down Expand Up @@ -51,6 +52,9 @@ cpdef bytes stats(str loudnorm_args, AudioStream stream):
cdef const char* c_args = py_args
cdef char* result

# Save log level since C function overwrite it.
level = get_level()

with nogil:
result = loudnorm_get_stats(format_ptr, stream_index, c_args)

Expand All @@ -60,4 +64,6 @@ cpdef bytes stats(str loudnorm_args, AudioStream stream):
py_result = result[:] # Make a copy of the string
free(result) # Free the C string

set_level(level)

return py_result
16 changes: 2 additions & 14 deletions av/filter/loudnorm_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <libavfilter/avfilter.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
#include <libavutil/opt.h>
#include <string.h>

#ifdef _WIN32
Expand Down Expand Up @@ -154,22 +153,11 @@ char* loudnorm_get_stats(
av_frame_unref(filt_frame);
}

// Force stats print
if (loudnorm_ctx) {
av_log_set_level(AV_LOG_INFO);
av_opt_set(loudnorm_ctx, "print_format", "json", AV_OPT_SEARCH_CHILDREN);
av_opt_set(loudnorm_ctx, "measured_i", NULL, AV_OPT_SEARCH_CHILDREN);
av_opt_set(loudnorm_ctx, "measured_lra", NULL, AV_OPT_SEARCH_CHILDREN);
av_opt_set(loudnorm_ctx, "measured_tp", NULL, AV_OPT_SEARCH_CHILDREN);
av_opt_set(loudnorm_ctx, "measured_thresh", NULL, AV_OPT_SEARCH_CHILDREN);
avfilter_init_str(loudnorm_ctx, NULL);
}

avfilter_graph_request_oldest(filter_graph);
// Pushes graph
avfilter_graph_free(&filter_graph);

end:
avcodec_free_context(&codec_ctx);
avfilter_graph_free(&filter_graph);
avformat_close_input(&fmt_ctx);
av_frame_free(&filt_frame);
av_frame_free(&frame);
Expand Down

0 comments on commit 445d759

Please # to comment.