Skip to content

Conversation

@Dan-Flores
Copy link
Contributor

@Dan-Flores Dan-Flores commented Nov 11, 2025

This PR adds codec as an option in the VideoEncoder API.

It accepts codec specs or codec implems, similar to FFmpeg's find_codec function by using avcodec_find_encoder_by_name or avcodec_descriptor_get_by_name + avcodec_find_encoder.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 11, 2025
@Dan-Flores Dan-Flores force-pushed the codec_select_encode_option branch 2 times, most recently from 2a49a3d to 94ddcbb Compare November 12, 2025 19:18
@Dan-Flores Dan-Flores force-pushed the codec_select_encode_option branch from 94ddcbb to 976bd2c Compare November 12, 2025 19:31
Returns:
Tensor: The raw encoded bytes as 4D uint8 Tensor.
Tensor: The raw encoded bytes as 1D uint8 Tensor.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Drive by change: this should say 1D.

crf: Optional[int] = None,
codec: Optional[str] = None,
pixel_format: Optional[str] = None,
crf: Optional[int] = None,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Drive by changes: Reordering these params to keep the order consistent.

@Dan-Flores Dan-Flores marked this pull request as ready for review November 13, 2025 04:25
frame_rate=30,
)
getattr(encoder, method)(**valid_params, codec=codec)

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add a test against the CLI to make sure the parameter is properly taken into account? It will probably have to be a separate test (not part of the giant test_against_cli one), since the codec will obviously vary per format.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's also assert that using "libx264" gives the exact same result as using "h264", i.e. that there is an equivalence between codec spec and codec implem.

Copy link
Contributor

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Nice work, thanks @Dan-Flores !

codec,
" not found. To see available codecs, run: ffmpeg -encoders");
} else {
avCodec = avcodec_find_encoder(avFormatContext_->oformat->video_codec);
Copy link
Contributor

Choose a reason for hiding this comment

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

I just realized oformat is a pointer so let's add

TORCH_CHECK(avFormatContext_->oformat != nullptr, ...);


frames_spec = self.decode(spec_output).data
frames_impl = self.decode(impl_output).data
torch.testing.assert_close(frames_spec, frames_impl, rtol=0, atol=0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Great tests!

@Dan-Flores Dan-Flores merged commit 79e633c into meta-pytorch:main Nov 14, 2025
63 of 70 checks passed
@Dan-Flores Dan-Flores deleted the codec_select_encode_option branch November 14, 2025 15:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants