Skip to content

Commit

Permalink
Fix: --print-stats only outputs the last stream
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetsait authored and slhck committed Nov 22, 2024
1 parent fe40f1c commit f5bb2ee
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
7 changes: 5 additions & 2 deletions ffmpeg_normalize/_ffmpeg_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
17 changes: 5 additions & 12 deletions ffmpeg_normalize/_media_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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():
Expand All @@ -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())

0 comments on commit f5bb2ee

Please sign in to comment.