Skip to content

Commit 37a53a6

Browse files
committed
Refactor video frame extraction to improve PyNvCodec availability check
- Removed the try-except block for importing PyNvcFrameExtractor, simplifying the import logic. - Updated the condition for initializing the PyNvcFrameExtractor in the VideoFrameExtractionStage to rely solely on the _PYNVC_AVAILABLE flag. - Adjusted the handling of pixel format conversion in NvVideoDecoder to prepare for future updates to cvcuda. Signed-off-by: Abhinav Garg <abhinavg@stanford.edu>
1 parent b83c9ff commit 37a53a6

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

nemo_curator/stages/video/clipping/video_frame_extraction.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,9 @@
2424
from nemo_curator.stages.base import ProcessingStage
2525
from nemo_curator.stages.resources import Resources
2626
from nemo_curator.tasks.video import VideoTask
27+
from nemo_curator.utils.nvcodec_utils import _PYNVC_AVAILABLE, PyNvcFrameExtractor
2728
from nemo_curator.utils.operation_utils import make_pipeline_named_temporary_file
2829

29-
try:
30-
from nemo_curator.utils.nvcodec_utils import PyNvcFrameExtractor
31-
32-
_PYNVC_AVAILABLE = True
33-
except (ImportError, RuntimeError):
34-
logger.warning("PyNvcFrameExtractor not available, PyNvCodec mode will fall back to FFmpeg")
35-
PyNvcFrameExtractor = None
36-
_PYNVC_AVAILABLE = False
37-
3830

3931
def get_frames_from_ffmpeg(
4032
video_file: Path,
@@ -123,7 +115,7 @@ def setup(self, worker_metadata: WorkerMetadata | None = None) -> None: # noqa:
123115
worker_metadata (WorkerMetadata, optional): Information about the worker (provided by some backends)
124116
"""
125117
if self.decoder_mode == "pynvc":
126-
if _PYNVC_AVAILABLE and PyNvcFrameExtractor is not None:
118+
if _PYNVC_AVAILABLE:
127119
self.pynvc_frame_extractor = PyNvcFrameExtractor(
128120
width=self.output_hw[1],
129121
height=self.output_hw[0],

nemo_curator/utils/nvcodec_utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@
2222

2323
try:
2424
import cvcuda # type: ignore[import-untyped]
25-
import nvcv # type: ignore[import-untyped]
2625
import pycuda.driver as cuda # type: ignore[import-untyped]
2726
import PyNvVideoCodec as Nvc # type: ignore[import-untyped]
2827

2928
pixel_format_to_cvcuda_code = {
3029
Nvc.Pixel_Format.YUV444: cvcuda.ColorConversion.YUV2RGB, # type: ignore[import-untyped]
3130
Nvc.Pixel_Format.NV12: cvcuda.ColorConversion.YUV2RGB_NV12, # type: ignore[import-untyped]
3231
}
32+
_PYNVC_AVAILABLE = True
3333
except (ImportError, RuntimeError):
3434
logger.warning("PyNvVideoCodec is not installed, some features will be disabled.")
3535
Nvc = None
3636
cvcuda = None
37-
nvcv = None
3837
cuda = None
3938
pixel_format_to_cvcuda_code = {}
39+
_PYNVC_AVAILABLE = False
4040

4141

4242
class FrameExtractionPolicy(enum.Enum):
@@ -268,7 +268,8 @@ def generate_decoded_frames(self) -> list[torch.Tensor]:
268268
for packet in self.nvDemux:
269269
list_frames = self.nvDec.Decode(packet)
270270
for decoded_frame in list_frames:
271-
nvcv_tensor = nvcv.as_tensor(nvcv.as_image(decoded_frame.nvcv_image(), nvcv.Format.U8))
271+
# TODO: Replace nvcv with cvcuda. Before that remove the use of nvcv_image. It's deprecated
272+
nvcv_tensor = cvcuda.as_tensor(cvcuda.as_image(decoded_frame.nvcv_image(), cvcuda.Format.U8))
272273
if nvcv_tensor.layout == "NCHW":
273274
nchw_shape = nvcv_tensor.shape
274275
nhwc_shape = (nchw_shape[0], nchw_shape[2], nchw_shape[3], nchw_shape[1])

0 commit comments

Comments
 (0)