Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 77b3e76

Browse files
authoredMar 17, 2025··
Merge pull request #98 from NVIDIA/develop
Fix optional AV imports
2 parents e43d8c4 + bcc5285 commit 77b3e76

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed
 

‎src/megatron/energon/av/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
AVData,
66
AVDecoder,
77
)
8-
from megatron.energon.av.fastseek import Fastseek
98
from megatron.energon.av.video_data import VideoData
109

1110
__all__ = [
1211
"AVData",
1312
"AVDecoder",
14-
"Fastseek",
1513
"VideoData",
1614
]

‎src/megatron/energon/av/decode_av_frames.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@
66
from fractions import Fraction
77
from typing import Literal, Optional, Sequence, Union, overload
88

9+
import numpy as np
10+
import torch
11+
912
# Try importing optional dependencies
1013
try:
1114
import av
1215
import filetype
13-
import numpy as np
1416
import soundfile as sf
15-
import torch
17+
18+
from megatron.energon.av.fastseek import Fastseek
1619

1720
AV_DECODE_AVAILABLE = True
1821
except ImportError as e:
1922
AV_DECODE_AVAILABLE = False
2023
MISSING_DEPENDENCY = str(e)
2124

22-
from megatron.energon.av.fastseek import Fastseek
2325
from megatron.energon.av.video_data import VideoData
2426

2527

@@ -100,7 +102,7 @@ def get_frame_batch(
100102
self,
101103
frame_indices: Sequence[int],
102104
out_frame_size: Optional[tuple[int, int]] = None,
103-
seeker: Optional[Fastseek] = None,
105+
seeker: Optional["Fastseek"] = None,
104106
) -> tuple[torch.Tensor, dict]:
105107
"""Gets a batch of frames at the given indices from a video file.
106108
@@ -252,7 +254,7 @@ def decode_video_frames(
252254
The method uses the Fastseek class to optimize frame seeking, which determines
253255
whether to use frame numbers or timestamps based on the container format.
254256
"""
255-
seeker: Fastseek = Fastseek(self.stream)
257+
seeker: "Fastseek" = Fastseek(self.stream)
256258
self.stream.seek(0)
257259

258260
# --- First, decode video frames ---
@@ -512,6 +514,13 @@ def __init__(
512514
video_out_frame_size: tuple[int, int],
513515
video_decode: Literal["torch", "AVData"] = "torch",
514516
) -> None:
517+
if not AV_DECODE_AVAILABLE:
518+
raise ImportError(
519+
f"AV decoding is not available. Please install the required dependencies with:\n"
520+
f"pip install megatron-energon[av_decode]\n"
521+
f"Missing dependency: {MISSING_DEPENDENCY}"
522+
)
523+
515524
self.audio_clip_duration = audio_clip_duration
516525
self.audio_num_clips = audio_num_clips
517526
self.video_decode_audio = video_decode_audio

0 commit comments

Comments
 (0)
Please sign in to comment.