Skip to content

Commit 80948f5

Browse files
author
Daniel Flores
committed
Make crf optional
1 parent 48df4cf commit 80948f5

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(
@@ -435,7 +435,7 @@ void encode_video_to_file(
435435
const at::Tensor& frames,
436436
int64_t frame_rate,
437437
std::string_view file_name,
438-
int64_t crf) {
438+
std::optional<int64_t> crf = std::nullopt) {
439439
VideoStreamOptions videoStreamOptions;
440440
videoStreamOptions.crf = crf;
441441
VideoEncoder(

src/torchcodec/_core/ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def encode_video_to_file_abstract(
230230
frames: torch.Tensor,
231231
frame_rate: int,
232232
filename: str,
233-
crf: int,
233+
crf: Optional[int],
234234
) -> None:
235235
return
236236

test/test_ops.py

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

13201320
encoded_path = str(tmp_path / f"encoder_output.{format}")
13211321
frame_rate = 30 # Frame rate is fixed with num frames decoded
1322-
encode_video_to_file(source_frames, frame_rate, encoded_path, 0)
1322+
encode_video_to_file(source_frames, frame_rate, encoded_path, crf=0)
13231323
round_trip_frames = self.decode(encoded_path).data
13241324

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

0 commit comments

Comments
 (0)