@@ -32,12 +32,14 @@ TORCH_LIBRARY(torchcodec_ns, m) {
3232 m.def (" create_from_file(str filename, str? seek_mode=None) -> Tensor" );
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) -> ()" );
35- m.def (
36- " encode_video_to_file(Tensor frames, int frame_rate, str filename, int? crf=None) -> ()" );
3735 m.def (
3836 " 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" );
3937 m.def (
4038 " _encode_audio_to_file_like(Tensor samples, int sample_rate, str format, int file_like_context, int? bit_rate=None, int? num_channels=None, int? desired_sample_rate=None) -> ()" );
39+ m.def (
40+ " encode_video_to_file(Tensor frames, int frame_rate, str filename, int? crf=None) -> ()" );
41+ m.def (
42+ " encode_video_to_tensor(Tensor frames, int frame_rate, str format, int? crf=None) -> Tensor" );
4143 m.def (
4244 " create_from_tensor(Tensor video_tensor, str? seek_mode=None) -> Tensor" );
4345 m.def (
@@ -498,21 +500,6 @@ OpsAudioFramesOutput get_frames_by_pts_in_range_audio(
498500 return makeOpsAudioFramesOutput (result);
499501}
500502
501- void encode_video_to_file (
502- const at::Tensor& frames,
503- int64_t frame_rate,
504- std::string_view file_name,
505- std::optional<int64_t > crf = std::nullopt ) {
506- VideoStreamOptions videoStreamOptions;
507- videoStreamOptions.crf = crf;
508- VideoEncoder (
509- frames,
510- validateInt64ToInt (frame_rate, " frame_rate" ),
511- file_name,
512- videoStreamOptions)
513- .encode ();
514- }
515-
516503void encode_audio_to_file (
517504 const at::Tensor& samples,
518505 int64_t sample_rate,
@@ -587,6 +574,38 @@ void _encode_audio_to_file_like(
587574 encoder.encode ();
588575}
589576
577+ void encode_video_to_file (
578+ const at::Tensor& frames,
579+ int64_t frame_rate,
580+ std::string_view file_name,
581+ std::optional<int64_t > crf = std::nullopt ) {
582+ VideoStreamOptions videoStreamOptions;
583+ videoStreamOptions.crf = crf;
584+ VideoEncoder (
585+ frames,
586+ validateInt64ToInt (frame_rate, " frame_rate" ),
587+ file_name,
588+ videoStreamOptions)
589+ .encode ();
590+ }
591+
592+ at::Tensor encode_video_to_tensor (
593+ const at::Tensor& frames,
594+ int64_t frame_rate,
595+ std::string_view format,
596+ std::optional<int64_t > crf = std::nullopt ) {
597+ auto avioContextHolder = std::make_unique<AVIOToTensorContext>();
598+ VideoStreamOptions videoStreamOptions;
599+ videoStreamOptions.crf = crf;
600+ return VideoEncoder (
601+ frames,
602+ validateInt64ToInt (frame_rate, " frame_rate" ),
603+ format,
604+ std::move (avioContextHolder),
605+ videoStreamOptions)
606+ .encodeToTensor ();
607+ }
608+
590609// For testing only. We need to implement this operation as a core library
591610// function because what we're testing is round-tripping pts values as
592611// double-precision floating point numbers from C++ to Python and back to C++.
@@ -847,9 +866,10 @@ TORCH_LIBRARY_IMPL(torchcodec_ns, BackendSelect, m) {
847866
848867TORCH_LIBRARY_IMPL (torchcodec_ns, CPU, m) {
849868 m.impl (" encode_audio_to_file" , &encode_audio_to_file);
850- m.impl (" encode_video_to_file" , &encode_video_to_file);
851869 m.impl (" encode_audio_to_tensor" , &encode_audio_to_tensor);
852870 m.impl (" _encode_audio_to_file_like" , &_encode_audio_to_file_like);
871+ m.impl (" encode_video_to_file" , &encode_video_to_file);
872+ m.impl (" encode_video_to_tensor" , &encode_video_to_tensor);
853873 m.impl (" seek_to_pts" , &seek_to_pts);
854874 m.impl (" add_video_stream" , &add_video_stream);
855875 m.impl (" _add_video_stream" , &_add_video_stream);
0 commit comments