skip audio loading if no audio in video#1397
Merged
Artiprocher merged 1 commit intomainfrom Apr 15, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the version to 2.0.9 and adds a try-except block to handle audio loading failures in operators.py. The review identified critical issues in the implementation: returning a single None value will cause unpacking errors in callers, the bare except block should be more specific, and the imageio reader needs proper resource management via a context manager.
Comment on lines
+263
to
+278
| try: | ||
| reader = self.get_reader(data) | ||
| num_frames = self.get_num_frames(reader) | ||
| duration = num_frames / self.frame_rate | ||
| waveform, sample_rate = torchaudio.load(data) | ||
| target_samples = int(duration * sample_rate) | ||
| current_samples = waveform.shape[-1] | ||
| if current_samples > target_samples: | ||
| waveform = waveform[..., :target_samples] | ||
| elif current_samples < target_samples: | ||
| padding = target_samples - current_samples | ||
| waveform = torch.nn.functional.pad(waveform, (0, padding)) | ||
| return waveform, sample_rate | ||
| except: | ||
| warnings.warn(f"Cannot load audio in {data}. The audio will be `None`.") | ||
| return None |
Contributor
There was a problem hiding this comment.
The current implementation has several issues:
- Unpacking Error: The method is expected to return a tuple
(waveform, sample_rate). ReturningNonein theexceptblock will cause aTypeErrorwhen the caller attempts to unpack the result (e.g.,waveform, sr = loader(data)). ReturningNone, Noneensures the caller can still unpack the result. - Bare
except:block: Usingexcept:without specifying an exception type catches all exceptions, includingSystemExitandKeyboardInterrupt. It is better to catchExceptionor specific errors likeRuntimeErrorfromtorchaudio. - Resource Leak: The
imageioreader is opened but never closed. Using awithstatement ensures the reader is properly closed even if an exception occurs during frame counting.
Suggested change
| try: | |
| reader = self.get_reader(data) | |
| num_frames = self.get_num_frames(reader) | |
| duration = num_frames / self.frame_rate | |
| waveform, sample_rate = torchaudio.load(data) | |
| target_samples = int(duration * sample_rate) | |
| current_samples = waveform.shape[-1] | |
| if current_samples > target_samples: | |
| waveform = waveform[..., :target_samples] | |
| elif current_samples < target_samples: | |
| padding = target_samples - current_samples | |
| waveform = torch.nn.functional.pad(waveform, (0, padding)) | |
| return waveform, sample_rate | |
| except: | |
| warnings.warn(f"Cannot load audio in {data}. The audio will be `None`.") | |
| return None | |
| try: | |
| with self.get_reader(data) as reader: | |
| num_frames = self.get_num_frames(reader) | |
| duration = num_frames / self.frame_rate | |
| waveform, sample_rate = torchaudio.load(data) | |
| target_samples = int(duration * sample_rate) | |
| current_samples = waveform.shape[-1] | |
| if current_samples > target_samples: | |
| waveform = waveform[..., :target_samples] | |
| elif current_samples < target_samples: | |
| padding = target_samples - current_samples | |
| waveform = torch.nn.functional.pad(waveform, (0, padding)) | |
| return waveform, sample_rate | |
| except Exception: | |
| warnings.warn(f"Cannot load audio in {data}. The audio will be `None`.") | |
| return None, None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.