Skip to content

Commit 3eb8e27

Browse files
authored
Import torchcodec lazily in processing_utils (#50325)
The module-level import loaded torchcodec (and its FFmpeg libs) for every processor, and could break importing processing_utils when torchcodec is installed but not importable. Import it only in the audio-from-video path, after the version check, which now raises ImportError.
1 parent fd3cf01 commit 3eb8e27

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

‎src/transformers/processing_utils.py‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
direct_transformers_import,
5858
hf_api,
5959
is_torch_available,
60-
is_torchcodec_available,
6160
is_torchcodec_greater_or_equal,
6261
list_repo_templates,
6362
logging,
@@ -78,9 +77,6 @@
7877
from .video_utils import VideoInput, VideoMetadataType, make_batched_videos
7978

8079

81-
if is_torchcodec_available():
82-
import torchcodec
83-
8480
if is_torch_available():
8581
import torch
8682

@@ -2571,7 +2567,10 @@ def _resolve_audio_from_video(
25712567
)
25722568

25732569
if not is_torchcodec_greater_or_equal("0.17"):
2574-
raise ValueError("Loading audio track from video file requires torchcodec>=0.17 installed!")
2570+
raise ImportError(
2571+
"`load_audio_from_video=True` requires torchcodec>=0.17. Install it with `pip install -U torchcodec`."
2572+
)
2573+
from torchcodec.decoders import get_container_metadata
25752574

25762575
standalone_audio = iter(audio)
25772576
audio_resolved = []
@@ -2584,7 +2583,7 @@ def _resolve_audio_from_video(
25842583

25852584
# Only `torchcodec` can decode audio track from all types of video file formats
25862585
# Don't raise a load-error if video is silent and instead skip it
2587-
cont_metadata = torchcodec.decoders.get_container_metadata(videos[video_idx])
2586+
cont_metadata = get_container_metadata(videos[video_idx])
25882587
if not any(stream.media_type == "audio" for stream in cont_metadata.streams):
25892588
continue
25902589

0 commit comments

Comments
 (0)