Add audio property to VideoDecoder#1442
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/meta-pytorch/torchcodec/1442
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
Hi @Samoed , can you share more about this:
Specifically I'd like to understand why having two seprate VideoDecoder and AudioDecoder objects lead to friction, and how does the proposed design solve it |
|
The Datasets library outputs video inputs as |
|
Hi @NicolasHug! I thought about this feature more and found more use cases. Currently Transformers have a processor kwarg |
|
Sorry I still don't get it - can you share a code example of what this would enable, and why it can't already be achieved by just keeping a ref to the source video (and creating a separate |
|
HuggingFace from datasets import load_dataset
test_ds = load_dataset("Samoed/testds")
video = test_ds["train"][0]["video"] # VideoDecoder without path
# Today: no clean way to get audio from `video`. Users either
# - reach into `video._source` with Video(decode=False)
# - split the dataset into separate `video` + `audio` columns upstream.
audio = video.audioFor huggingface transformers def __call__(self, videos: list[VideoDecoder | ...], ...):
for v in videos:
frames = v.get_frames_in_range(...)
if self.use_audio_in_video and decoder.audio is not None:
audio = v.audio.get_all_samples() |
| # Normalize file-like objects to bytes so the source can be stored and | ||
| # later passed to AudioDecoder independently (file-likes have a single | ||
| # shared cursor that two decoders would stomp on each other). | ||
| if not isinstance(source, (str, Path, bytes, Tensor)) and hasattr( | ||
| source, "read" | ||
| ): | ||
| if hasattr(source, "seek"): | ||
| source.seek(0) | ||
| source = source.read() |
There was a problem hiding this comment.
The above is one reason why we can't support the proposal as-is. We don't want to convert a file-like to bytes, because it forces materialization or full download of what is supposed to be a streaming object - it defeats the entire purpose of file-like support.
I thought of perhaps exposing the .source attribute publicly instead of exposing an AudioDecoder, but that would still be subject to the same issue with file-like objects. Let me ask the datasets maintainer to see if there's anything that can be done on that side
|
@lhoestq curious if you have any suggestion on |
|
This also affects Sentence Transformers currently, e.g.: from datasets import load_dataset
from sentence_transformers import SentenceTransformer
test_ds = load_dataset("Samoed/testds")
video = test_ds["train"][0]["video"]
model = SentenceTransformer("...") # a video-capable model
embedding = model.encode(video)Internally, ST converts this
|
Previously, accessing audio from a video file required creating a separate
AudioDecoderalongsideVideoDecoder#1158. This split made it difficult to integrate with external libraries such as huggingface datasets utilities that expect audio to be accessible directly from the video decoder - the absence of an audio attribute on VideoDecoder either required workarounds or left audio support missing entirely in those integrations.This PR adds a lazy audio property to VideoDecoder that returns an AudioDecoder for audio stream in the same source, or None if the source contains no audio stream