@@ -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,13 @@ 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));
612+ videoStreamOptions.deviceVariant = " ffmpeg" ;
609613 VideoEncoder (
610614 frames,
611615 validateInt64ToInt (frame_rate, " frame_rate" ),
@@ -618,10 +622,14 @@ at::Tensor encode_video_to_tensor(
618622 const at::Tensor& frames,
619623 int64_t frame_rate,
620624 std::string_view format,
625+ std::string_view device = " cpu" ,
621626 std::optional<int64_t > crf = std::nullopt ) {
622627 auto avioContextHolder = std::make_unique<AVIOToTensorContext>();
623628 VideoStreamOptions videoStreamOptions;
624629 videoStreamOptions.crf = crf;
630+
631+ videoStreamOptions.device = torch::Device (std::string (device));
632+ videoStreamOptions.deviceVariant = " ffmpeg" ;
625633 return VideoEncoder (
626634 frames,
627635 validateInt64ToInt (frame_rate, " frame_rate" ),
@@ -636,6 +644,7 @@ void _encode_video_to_file_like(
636644 int64_t frame_rate,
637645 std::string_view format,
638646 int64_t file_like_context,
647+ std::string_view device = " cpu" ,
639648 std::optional<int64_t > crf = std::nullopt ) {
640649 auto fileLikeContext =
641650 reinterpret_cast <AVIOFileLikeContext*>(file_like_context);
@@ -646,6 +655,9 @@ void _encode_video_to_file_like(
646655 VideoStreamOptions videoStreamOptions;
647656 videoStreamOptions.crf = crf;
648657
658+ videoStreamOptions.device = torch::Device (std::string (device));
659+ videoStreamOptions.deviceVariant = " ffmpeg" ;
660+
649661 VideoEncoder encoder (
650662 frames,
651663 validateInt64ToInt (frame_rate, " frame_rate" ),
0 commit comments