diff --git a/ffmpeg_normalize/_ffmpeg_normalize.py b/ffmpeg_normalize/_ffmpeg_normalize.py index a1d3caa..044acd6 100644 --- a/ffmpeg_normalize/_ffmpeg_normalize.py +++ b/ffmpeg_normalize/_ffmpeg_normalize.py @@ -3,6 +3,8 @@ import json import logging import os +import sys +from itertools import chain from typing import TYPE_CHECKING, Literal from tqdm import tqdm @@ -254,5 +256,6 @@ def run_normalization(self) -> None: _logger.info(f"Normalized file written to {media_file.output_file}") - if self.print_stats and self.stats: - print(json.dumps(self.stats, indent=4)) + if self.print_stats: + json.dump(list(chain.from_iterable(media_file.get_stats() for media_file in self.media_files)), sys.stdout, indent=4) + print() diff --git a/ffmpeg_normalize/_media_file.py b/ffmpeg_normalize/_media_file.py index c5c6fbc..e844ddb 100644 --- a/ffmpeg_normalize/_media_file.py +++ b/ffmpeg_normalize/_media_file.py @@ -6,13 +6,13 @@ import shlex from shutil import move, rmtree from tempfile import mkdtemp -from typing import TYPE_CHECKING, Iterator, Literal, TypedDict +from typing import TYPE_CHECKING, Iterable, Iterator, Literal, TypedDict from tqdm import tqdm from ._cmd_utils import DUR_REGEX, NUL, CommandRunner from ._errors import FFmpegNormalizeError -from ._streams import AudioStream, SubtitleStream, VideoStream +from ._streams import AudioStream, SubtitleStream, VideoStream, LoudnessStatisticsWithMetadata if TYPE_CHECKING: from ffmpeg_normalize import FFmpegNormalize @@ -240,11 +240,6 @@ def _first_pass(self) -> None: for _ in fun(): pass - # set initial stats (for dry-runs, this is the only thing we need to do) - self.ffmpeg_normalize.stats = [ - audio_stream.get_stats() for audio_stream in self.streams["audio"].values() - ] - def _get_audio_filter_cmd(self) -> tuple[str, list[str]]: """ Return the audio filter command and output labels needed. @@ -426,11 +421,6 @@ def _second_pass(self) -> Iterator[float]: if stream_id in all_stats: audio_stream.set_second_pass_stats(all_stats[stream_id]) - # collect all stats for the final report, again (overwrite the input) - self.ffmpeg_normalize.stats = [ - audio_stream.get_stats() for audio_stream in self.streams["audio"].values() - ] - # warn if self.media_file.ffmpeg_normalize.dynamic == False and any of the second pass stats contain "normalization_type" == "dynamic" if self.ffmpeg_normalize.dynamic is False: for audio_stream in self.streams["audio"].values(): @@ -445,3 +435,6 @@ def _second_pass(self) -> Iterator[float]: ) _logger.debug("Normalization finished") + + def get_stats(self) -> Iterable[LoudnessStatisticsWithMetadata]: + return (audio_stream.get_stats() for audio_stream in self.streams["audio"].values()) \ No newline at end of file