1+ import json
12import math
23import shutil
4+ import subprocess
35import wave
46from pathlib import Path
57
@@ -34,6 +36,29 @@ def _has_ffmpeg() -> bool:
3436 return shutil .which ("ffmpeg" ) is not None
3537
3638
39+ def _has_ffprobe () -> bool :
40+ return shutil .which ("ffprobe" ) is not None
41+
42+
43+ def _probe_audio (path : Path ) -> tuple [int , int ]:
44+ out = subprocess .check_output (
45+ [
46+ "ffprobe" ,
47+ "-v" ,
48+ "error" ,
49+ "-select_streams" ,
50+ "a:0" ,
51+ "-show_entries" ,
52+ "stream=sample_rate,channels" ,
53+ "-of" ,
54+ "json" ,
55+ str (path ),
56+ ]
57+ )
58+ stream = json .loads (out )["streams" ][0 ]
59+ return int (stream ["sample_rate" ]), int (stream ["channels" ])
60+
61+
3762@pytest .mark .parametrize ("channels" , [1 , 2 ])
3863def test_write_wav_mono_and_stereo_with_float_normalization (
3964 tmp_path : Path , channels : int
@@ -56,11 +81,11 @@ def test_write_wav_mono_and_stereo_with_float_normalization(
5681 assert 32000 <= max_abs <= 32767
5782
5883
59- @pytest .mark .skipif (not _has_ffmpeg (), reason = "ffmpeg not available" )
84+ @pytest .mark .skipif (
85+ not (_has_ffmpeg () and _has_ffprobe ()), reason = "ffmpeg/ffprobe not available"
86+ )
6087@pytest .mark .parametrize ("channels" , [1 , 2 ])
6188def test_write_mp3_mono_and_stereo (tmp_path : Path , channels : int ) -> None :
62- from pydub import AudioSegment
63-
6489 mono = _tone (0.1 , 440.0 , SAMPLE_RATE , amp = 0.5 )
6590 data = mono if channels == 1 else np .stack ([mono , mono ], axis = 1 )
6691
@@ -69,9 +94,10 @@ def test_write_mp3_mono_and_stereo(tmp_path: Path, channels: int) -> None:
6994 data = data , sample_rate = SAMPLE_RATE , filename = out , format = "mp3"
7095 )
7196
72- seg = AudioSegment .from_file (str (out ), format = "mp3" )
73- assert seg .frame_rate == SAMPLE_RATE
74- assert seg .channels == channels
97+ assert out .exists () and out .stat ().st_size > 0
98+ sr , ch = _probe_audio (out )
99+ assert sr == SAMPLE_RATE
100+ assert ch == channels
75101
76102
77103@pytest .mark .parametrize (
0 commit comments