Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Conversion logs say "larger" when the file grew

jjsmackayc8e0cb606863
diff --git a/src/api/recordings.py b/src/api/recordings.py
index 263aa209..d378125d 100644
--- a/src/api/recordings.py
+++ b/src/api/recordings.py
@@ -2499,7 +2499,7 @@ def ingest_uploaded_recording(
if result.was_converted:
current_app.logger.info(f"File converted: {result.original_codec} -> {result.final_codec}")
if result.was_compressed:
- current_app.logger.info(f"File compressed: {result.size_reduction_percent:.1f}% size reduction")
+ current_app.logger.info(f"File compressed: {result.size_change_label}")
except FFmpegNotFoundError as e:
current_app.logger.error(f"FFmpeg not found: {e}")
diff --git a/src/file_monitor.py b/src/file_monitor.py
index c8877602..9529c9af 100644
--- a/src/file_monitor.py
+++ b/src/file_monitor.py
@@ -446,7 +446,7 @@ class FileMonitor:
if result.was_converted:
self.logger.info(f"File converted: {result.original_codec} -> {result.final_codec}")
if result.was_compressed:
- self.logger.info(f"File compressed: {result.size_reduction_percent:.1f}% size reduction")
+ self.logger.info(f"File compressed: {result.size_change_label}")
except FFmpegNotFoundError as e:
self.logger.error(f"FFmpeg not found: {e}")
diff --git a/src/utils/audio_conversion.py b/src/utils/audio_conversion.py
index 7211e408..8b92616b 100644
--- a/src/utils/audio_conversion.py
+++ b/src/utils/audio_conversion.py
@@ -16,7 +16,8 @@ from pathlib import Path
from typing import Optional, Tuple, Set, Dict, Any
from src.utils.ffprobe import get_codec_info, is_lossless_audio, FFProbeError
-from src.utils.ffmpeg_utils import compress_audio, extract_audio_from_video, FFmpegError, FFmpegNotFoundError
+from src.utils.ffmpeg_utils import (compress_audio, describe_size_change, describe_size_reduction,
+ extract_audio_from_video, FFmpegError, FFmpegNotFoundError)
from src.config.app_config import AUDIO_COMPRESS_UPLOADS, AUDIO_CODEC, AUDIO_BITRATE, AUDIO_UNSUPPORTED_CODECS
logger = logging.getLogger(__name__)
@@ -52,6 +53,11 @@ class ConversionResult:
return 0.0
return ((self.original_size - self.final_size) / self.original_size) * 100
+ @property
+ def size_change_label(self) -> str:
+ """"40.2% smaller" / "93.5% larger" / "same size", for log lines."""
+ return describe_size_reduction(self.size_reduction_percent)
+
@property
def original_size_mb(self) -> float:
"""Original size in megabytes."""
@@ -210,12 +216,11 @@ def convert_if_needed(
final_codec = video_target_codec
final_size = os.path.getsize(output_filepath)
- reduction = ((original_size - final_size) / original_size * 100) if original_size > 0 else 0
logger.info(
f"Successfully extracted audio from {original_filename}: "
f"{original_size/1024/1024:.1f}MB -> {final_size/1024/1024:.1f}MB "
- f"({reduction:.1f}% reduction)"
+ f"({describe_size_change(original_size, final_size)})"
)
return ConversionResult(
@@ -287,12 +292,11 @@ def convert_if_needed(
)
final_size = os.path.getsize(output_filepath)
- reduction = ((original_size - final_size) / original_size * 100) if original_size > 0 else 0
-
+
logger.info(
f"Successfully converted {original_filename}: "
f"{original_size/1024/1024:.1f}MB -> {final_size/1024/1024:.1f}MB "
- f"({reduction:.1f}% reduction)"
+ f"({describe_size_change(original_size, final_size)})"
)
return ConversionResult(
@@ -355,12 +359,11 @@ def convert_if_needed(
)
final_size = os.path.getsize(compressed_path)
- reduction = ((original_size - final_size) / original_size * 100) if original_size > 0 else 0
logger.info(
f"Successfully compressed {original_filename}: "
f"{original_size/1024/1024:.1f}MB -> {final_size/1024/1024:.1f}MB "
- f"({reduction:.1f}% reduction)"
+ f"({describe_size_change(original_size, final_size)})"
)
return ConversionResult(
diff --git a/src/utils/ffmpeg_utils.py b/src/utils/ffmpeg_utils.py
index b0fafb8f..3156a984 100644
--- a/src/utils/ffmpeg_utils.py
+++ b/src/utils/ffmpeg_utils.py
@@ -35,6 +35,25 @@ class FFmpegNotFoundError(FFmpegError):
pass
+def describe_size_reduction(reduction_percent: float) -> str:
+ """Label a size change for a log line: "40.2% smaller", "93.5% larger".
+
+ The old "(-93.5% reduction)" was arithmetically right and read as a
+ win for a file that had doubled.
+ """
+ if reduction_percent > 0.05:
+ return f"{reduction_percent:.1f}% smaller"
+ if reduction_percent < -0.05:
+ return f"{-reduction_percent:.1f}% larger"
+ return "same size"
+
+
+def describe_size_change(original_size: int, final_size: int) -> str:
+ """describe_size_reduction for two byte counts."""
+ pct = ((original_size - final_size) / original_size * 100) if original_size > 0 else 0.0
+ return describe_size_reduction(pct)
+
+
def convert_to_mp3(
input_path: str,
output_path: Optional[str] = None,
@@ -333,12 +352,11 @@ def compress_audio(
# Get compressed file size
compressed_size = os.path.getsize(temp_output_path)
- ratio = (1 - compressed_size / original_size) * 100 if original_size > 0 else 0
-
+
current_app.logger.info(
f"Compressed {os.path.basename(input_path)}: "
f"{original_size / 1024 / 1024:.1f}MB -> "
- f"{compressed_size / 1024 / 1024:.1f}MB ({ratio:.1f}% reduction)"
+ f"{compressed_size / 1024 / 1024:.1f}MB ({describe_size_change(original_size, compressed_size)})"
)
# Remove original and rename temp to final
diff --git a/tests/test_size_change_label.py b/tests/test_size_change_label.py
new file mode 100644
index 00000000..ec507e57
--- /dev/null
+++ b/tests/test_size_change_label.py
@@ -0,0 +1,24 @@
+"""Conversion log lines say "larger" when the file grew (#opus-to-mp3 doubling)."""
+
+import os
+import sys
+
+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+from src.utils.audio_conversion import ConversionResult
+from src.utils.ffmpeg_utils import describe_size_change, describe_size_reduction
+
+
+def test_labels_name_the_direction():
+ assert describe_size_change(1000, 598) == "40.2% smaller"
+ assert describe_size_change(226_900_000, 439_000_000) == "93.5% larger"
+ assert describe_size_change(1000, 1000) == "same size"
+ assert describe_size_change(0, 10) == "same size"
+ assert describe_size_reduction(-93.5) == "93.5% larger"
+
+
+def test_conversion_result_exposes_the_label():
+ r = ConversionResult(output_path="x", mime_type="audio/mpeg", was_converted=True,
+ was_compressed=False, original_size=1000, final_size=2000,
+ original_codec="opus", final_codec="mp3")
+ assert r.size_change_label == "100.0% larger"