101 lines
2.5 KiB
Python
101 lines
2.5 KiB
Python
"""Unit tests for Stage 1 (Audio extraction via ffmpeg)."""
|
|
|
|
import shutil
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from songsplit.pipeline.runner import PipelineContext
|
|
from songsplit.pipeline.s1_extract import ExtractStage, get_audio_duration
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_video_and_audio(tmp_path: Path):
|
|
"""Generate synthetic test audio and video files using ffmpeg."""
|
|
ffmpeg = shutil.which("ffmpeg")
|
|
if not ffmpeg:
|
|
pytest.skip("ffmpeg not available")
|
|
|
|
audio_file = tmp_path / "sine.mp3"
|
|
# Create 1.5s 440Hz test MP3
|
|
subprocess.run(
|
|
[
|
|
ffmpeg,
|
|
"-y",
|
|
"-f",
|
|
"lavfi",
|
|
"-i",
|
|
"sine=frequency=440:duration=1.5",
|
|
"-c:a",
|
|
"libmp3lame",
|
|
"-b:a",
|
|
"128k",
|
|
str(audio_file),
|
|
],
|
|
check=True,
|
|
capture_output=True,
|
|
)
|
|
|
|
video_file = tmp_path / "test_video.mp4"
|
|
# Create 1.0s synthetic MP4 video with test tone
|
|
subprocess.run(
|
|
[
|
|
ffmpeg,
|
|
"-y",
|
|
"-f",
|
|
"lavfi",
|
|
"-i",
|
|
"color=c=blue:s=320x240:d=1.0",
|
|
"-f",
|
|
"lavfi",
|
|
"-i",
|
|
"sine=frequency=880:duration=1.0",
|
|
"-c:v",
|
|
"libx264",
|
|
"-c:a",
|
|
"aac",
|
|
"-shortest",
|
|
str(video_file),
|
|
],
|
|
check=True,
|
|
capture_output=True,
|
|
)
|
|
|
|
return audio_file, video_file
|
|
|
|
|
|
def test_extract_stage_audio_input(sample_video_and_audio, tmp_path: Path):
|
|
audio_file, _ = sample_video_and_audio
|
|
out_dir = tmp_path / "song_pkg"
|
|
ctx = PipelineContext(source_file=audio_file, output_dir=out_dir)
|
|
|
|
stage = ExtractStage()
|
|
stage.run(ctx)
|
|
|
|
out_wav = stage.get_outputs(ctx)[0]
|
|
assert out_wav.exists()
|
|
|
|
# Check duration using get_audio_duration
|
|
duration = get_audio_duration(out_wav)
|
|
assert duration == pytest.approx(1.5, abs=0.1)
|
|
|
|
# Check updated meta.json
|
|
meta = ctx.load_or_init_meta()
|
|
assert meta.duration_sec == pytest.approx(1.5, abs=0.1)
|
|
|
|
|
|
def test_extract_stage_video_input(sample_video_and_audio, tmp_path: Path):
|
|
_, video_file = sample_video_and_audio
|
|
out_dir = tmp_path / "song_pkg_video"
|
|
ctx = PipelineContext(source_file=video_file, output_dir=out_dir)
|
|
|
|
stage = ExtractStage()
|
|
stage.run(ctx)
|
|
|
|
out_wav = stage.get_outputs(ctx)[0]
|
|
assert out_wav.exists()
|
|
|
|
duration = get_audio_duration(out_wav)
|
|
assert duration == pytest.approx(1.0, abs=0.1)
|