Skip to content

Commit 397cf6e

Browse files
author
Daniel Flores
committed
Make crf optional
1 parent a4ef8db commit 397cf6e

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

src/torchcodec/_core/custom_ops.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ TORCH_LIBRARY(torchcodec_ns, m) {
3333
m.def(
3434
"encode_audio_to_file(Tensor samples, int sample_rate, str filename, int? bit_rate=None, int? num_channels=None, int? desired_sample_rate=None) -> ()");
3535
m.def(
36-
"encode_video_to_file(Tensor frames, int frame_rate, str filename, int crf) -> ()");
36+
"encode_video_to_file(Tensor frames, int frame_rate, str filename, int? crf=None) -> ()");
3737
m.def(
3838
"encode_audio_to_tensor(Tensor samples, int sample_rate, str format, int? bit_rate=None, int? num_channels=None, int? desired_sample_rate=None) -> Tensor");
3939
m.def(
@@ -457,7 +457,7 @@ void encode_video_to_file(
457457
const at::Tensor& frames,
458458
int64_t frame_rate,
459459
std::string_view file_name,
460-
int64_t crf) {
460+
std::optional<int64_t> crf = std::nullopt) {
461461
VideoStreamOptions videoStreamOptions;
462462
videoStreamOptions.crf = crf;
463463
VideoEncoder(

src/torchcodec/_core/ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def encode_video_to_file_abstract(
244244
frames: torch.Tensor,
245245
frame_rate: int,
246246
filename: str,
247-
crf: int,
247+
crf: Optional[int],
248248
) -> None:
249249
return
250250

test/test_ops.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,11 +1351,12 @@ def test_video_encoder_test_round_trip(self, tmp_path, format):
13511351

13521352
encoded_path = str(tmp_path / f"encoder_output.{format}")
13531353
frame_rate = 30 # Frame rate is fixed with num frames decoded
1354-
encode_video_to_file(source_frames, frame_rate, encoded_path, 0)
1354+
encode_video_to_file(source_frames, frame_rate, encoded_path, crf=0)
13551355
round_trip_frames = self.decode(encoded_path).data
13561356

1357-
# The output pixel format depends on the codecs available, and FFmpeg version.
1358-
# In the cases where YUV420P is chosen and chroma subsampling happens, assert_close needs higher tolerance.
1357+
# In the cases where a lossy pixel format conversion occurs, higher tolerance is needed.
1358+
# Converting to the output format may perform chroma subsampling.
1359+
# Other times, no conversion between YUV and RGB is required.
13591360
if ffmpeg_version == 6 or format in ("avi", "flv"):
13601361
atol = 55
13611362
else:

0 commit comments

Comments
 (0)