@@ -37,11 +37,11 @@ TORCH_LIBRARY(torchcodec_ns, m) {
3737 m.def (
3838 " _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) -> ()" );
3939 m.def (
40- " encode_video_to_file(Tensor frames, int frame_rate, str filename, int? crf=None) -> ()" );
40+ " encode_video_to_file(Tensor frames, int frame_rate, str filename, str device= \" cpu \" , int? crf=None) -> ()" );
4141 m.def (
42- " encode_video_to_tensor(Tensor frames, int frame_rate, str format, int? crf=None) -> Tensor" );
42+ " encode_video_to_tensor(Tensor frames, int frame_rate, str format, str device= \" cpu \" , int? crf=None) -> Tensor" );
4343 m.def (
44- " _encode_video_to_file_like(Tensor frames, int frame_rate, str format, int file_like_context, int? crf=None) -> ()" );
44+ " _encode_video_to_file_like(Tensor frames, int frame_rate, str format, int file_like_context, str device= \" cpu \" , int? crf=None) -> ()" );
4545 m.def (
4646 " create_from_tensor(Tensor video_tensor, str? seek_mode=None) -> Tensor" );
4747 m.def (
@@ -603,9 +603,12 @@ void encode_video_to_file(
603603 const at::Tensor& frames,
604604 int64_t frame_rate,
605605 std::string_view file_name,
606+ std::string_view device = " cpu" ,
606607 std::optional<int64_t > crf = std::nullopt ) {
607608 VideoStreamOptions videoStreamOptions;
608609 videoStreamOptions.crf = crf;
610+
611+ videoStreamOptions.device = torch::Device (std::string (device));
609612 VideoEncoder (
610613 frames,
611614 validateInt64ToInt (frame_rate, " frame_rate" ),
@@ -618,10 +621,13 @@ at::Tensor encode_video_to_tensor(
618621 const at::Tensor& frames,
619622 int64_t frame_rate,
620623 std::string_view format,
624+ std::string_view device = " cpu" ,
621625 std::optional<int64_t > crf = std::nullopt ) {
622626 auto avioContextHolder = std::make_unique<AVIOToTensorContext>();
623627 VideoStreamOptions videoStreamOptions;
624628 videoStreamOptions.crf = crf;
629+
630+ videoStreamOptions.device = torch::Device (std::string (device));
625631 return VideoEncoder (
626632 frames,
627633 validateInt64ToInt (frame_rate, " frame_rate" ),
@@ -636,6 +642,7 @@ void _encode_video_to_file_like(
636642 int64_t frame_rate,
637643 std::string_view format,
638644 int64_t file_like_context,
645+ std::string_view device = " cpu" ,
639646 std::optional<int64_t > crf = std::nullopt ) {
640647 auto fileLikeContext =
641648 reinterpret_cast <AVIOFileLikeContext*>(file_like_context);
@@ -645,6 +652,7 @@ void _encode_video_to_file_like(
645652
646653 VideoStreamOptions videoStreamOptions;
647654 videoStreamOptions.crf = crf;
655+ videoStreamOptions.device = torch::Device (std::string (device));
648656
649657 VideoEncoder encoder (
650658 frames,
0 commit comments