fix: make torchcodec optional (fix #1925) (#1926)

This commit is contained in:
Hervé BREDIN authored and GitHub committed 2025-09-29 13:52:32 +02:00
1 parent 5c44c82ecf
commit 6b3f45f9c5
1 file changed
+16 -5
+16 -5
View File
@@ -29,6 +29,7 @@ pyannote.audio relies on torchcodec for reading and torchaudio for resampling.
"""
import random
import warnings
from io import IOBase
from pathlib import Path
from typing import Mapping, Optional, Tuple
@@ -37,8 +38,19 @@ import torch.nn.functional as F
import torchaudio
from pyannote.core import Segment
from torch import Tensor
from torchcodec import AudioSamples
from torchcodec.decoders import AudioDecoder, AudioStreamMetadata
try:
import torchcodec
from torchcodec import AudioSamples
from torchcodec.decoders import AudioDecoder, AudioStreamMetadata
except Exception as e:
warnings.warn(
"\ntorchcodec is not installed correctly so built-in audio decoding will fail. Solutions are:\n"
"* use audio preloaded in-memory as a {'waveform': (channel, time) torch.Tensor, 'sample_rate': int} dictionary;\n"
"* fix torchcodec installation. Error message was:\n\n"
f"{e}"
)
AudioFile = str | Path | IOBase | Mapping
@@ -55,7 +67,7 @@ integer to load a specific channel: {"audio": "stereo.wav", "channel": 0}
"""
def get_audio_metadata(file: AudioFile) -> AudioStreamMetadata:
def get_audio_metadata(file: AudioFile) -> "AudioStreamMetadata":
"""Protocol preprocessor used to cache audio metadata
This is useful to speed future random access to this file, e.g.
@@ -403,8 +415,7 @@ class Audio:
start = 0.0
pad_end: int = (
max(self.get_num_samples(end, sample_rate), num_samples)
- num_samples
max(self.get_num_samples(end, sample_rate), num_samples) - num_samples
)
if end > duration:
if mode == "raise":