From 3a9ca109c30bebecc94b3675b34eca99a757108d Mon Sep 17 00:00:00 2001
From: jo <ljonas@riseup.net>
Date: Fri, 3 Feb 2023 18:01:13 +0100
Subject: [PATCH] refactor(analyzer): split analyze_cuepoint steps

---
 .../pipeline/analyze_cuepoint.py              | 21 ++++++++++++++++---
 .../libretime_analyzer/pipeline/pipeline.py   |  3 ++-
 .../tests/pipeline/analyze_cuepoint_test.py   |  8 +++++--
 3 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/analyzer/libretime_analyzer/pipeline/analyze_cuepoint.py b/analyzer/libretime_analyzer/pipeline/analyze_cuepoint.py
index 3a75e6bf0..8c88dfc17 100644
--- a/analyzer/libretime_analyzer/pipeline/analyze_cuepoint.py
+++ b/analyzer/libretime_analyzer/pipeline/analyze_cuepoint.py
@@ -8,11 +8,10 @@ from loguru import logger
 from ._ffmpeg import compute_silences, probe_duration
 
 
-def analyze_cuepoint(filepath: str, metadata: Dict[str, Any]) -> Dict[str, Any]:
+def analyze_duration(filepath: str, metadata: Dict[str, Any]) -> Dict[str, Any]:
     """
-    Extracts the cuein and cueout times along and sets the file duration using ffmpeg.
+    Extracts the file duration using ffmpeg.
     """
-
     try:
         duration = probe_duration(filepath)
 
@@ -30,7 +29,23 @@ def analyze_cuepoint(filepath: str, metadata: Dict[str, Any]) -> Dict[str, Any]:
         metadata["length"] = str(timedelta(seconds=duration))
         metadata["cuein"] = 0.0
         metadata["cueout"] = duration
+    except (CalledProcessError, OSError):
+        pass
 
+    return metadata
+
+
+def analyze_cuepoint(filepath: str, metadata: Dict[str, Any]) -> Dict[str, Any]:
+    """
+    Extracts the cuein and cueout times using ffmpeg.
+
+    This step must run after the 'analyze_duration' step.
+    """
+
+    # Duration has been computed in the 'analyze_duration' step
+    duration = metadata["length_seconds"]
+
+    try:
         silences = compute_silences(filepath)
 
         if len(silences) > 2:
diff --git a/analyzer/libretime_analyzer/pipeline/pipeline.py b/analyzer/libretime_analyzer/pipeline/pipeline.py
index fd9ff5d04..296f93511 100644
--- a/analyzer/libretime_analyzer/pipeline/pipeline.py
+++ b/analyzer/libretime_analyzer/pipeline/pipeline.py
@@ -4,7 +4,7 @@ from typing import Any, Dict, Protocol
 
 from loguru import logger
 
-from .analyze_cuepoint import analyze_cuepoint
+from .analyze_cuepoint import analyze_cuepoint, analyze_duration
 from .analyze_metadata import analyze_metadata
 from .analyze_playability import UnplayableFileError, analyze_playability
 from .analyze_replaygain import analyze_replaygain
@@ -77,6 +77,7 @@ class Pipeline:
             # First, we extract the ID3 tags and other metadata:
             metadata = {}
             metadata = analyze_metadata(audio_file_path, metadata)
+            metadata = analyze_duration(audio_file_path, metadata)
             metadata = analyze_cuepoint(audio_file_path, metadata)
             metadata = analyze_replaygain(audio_file_path, metadata)
             metadata = analyze_playability(audio_file_path, metadata)
diff --git a/analyzer/tests/pipeline/analyze_cuepoint_test.py b/analyzer/tests/pipeline/analyze_cuepoint_test.py
index ced307922..4a6065649 100644
--- a/analyzer/tests/pipeline/analyze_cuepoint_test.py
+++ b/analyzer/tests/pipeline/analyze_cuepoint_test.py
@@ -1,6 +1,9 @@
 import pytest
 
-from libretime_analyzer.pipeline.analyze_cuepoint import analyze_cuepoint
+from libretime_analyzer.pipeline.analyze_cuepoint import (
+    analyze_cuepoint,
+    analyze_duration,
+)
 
 from ..fixtures import FILES
 
@@ -15,7 +18,8 @@ from ..fixtures import FILES
     ),
 )
 def test_analyze_cuepoint(filepath, length, cuein, cueout):
-    metadata = analyze_cuepoint(filepath, {})
+    metadata = analyze_duration(filepath, {})
+    metadata = analyze_cuepoint(filepath, metadata)
 
     assert metadata["length_seconds"] == pytest.approx(length, abs=0.1)
     assert float(metadata["cuein"]) == pytest.approx(float(cuein), abs=1)