Skip to content

skip audio loading if no audio in video#1397

Merged
Artiprocher merged 1 commit intomainfrom
audio-fix
Apr 15, 2026
Merged

skip audio loading if no audio in video#1397
Artiprocher merged 1 commit intomainfrom
audio-fix

Conversation

@Artiprocher
Copy link
Copy Markdown
Collaborator

No description provided.

@Artiprocher Artiprocher merged commit 8f18e24 into main Apr 15, 2026
@Artiprocher Artiprocher deleted the audio-fix branch April 15, 2026 05:52
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation has several issues:

  1. Unpacking Error: The method is expected to return a tuple (waveform, sample_rate). Returning None in the except block will cause a TypeError when the caller attempts to unpack the result (e.g., waveform, sr = loader(data)). Returning None, None ensures the caller can still unpack the result.
  2. Bare except: block: Using except: without specifying an exception type catches all exceptions, including SystemExit and KeyboardInterrupt. It is better to catch Exception or specific errors like RuntimeError from torchaudio.
  3. Resource Leak: The imageio reader is opened but never closed. Using a with statement 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant