Skip to content

Commit 08b5851

Browse files
author
Daniel Flores
committed
Encoder adjustments to fix tests
1 parent adbf151 commit 08b5851

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/torchcodec/_core/Encoder.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -577,11 +577,8 @@ VideoEncoder::VideoEncoder(
577577

578578
void VideoEncoder::initializeEncoder(
579579
const VideoStreamOptions& videoStreamOptions) {
580-
// TODO-VideoEncoder: Allow FFmpeg to pick codec based on container format?
581-
// Currently, this causes errors for some containers (avi)
582-
// const AVCodec* avCodec =
583-
// avcodec_find_encoder(avFormatContext_->oformat->video_codec);
584-
const AVCodec* avCodec = avcodec_find_encoder(AV_CODEC_ID_H264);
580+
const AVCodec* avCodec =
581+
avcodec_find_encoder(avFormatContext_->oformat->video_codec);
585582
TORCH_CHECK(avCodec != nullptr, "Video codec not found");
586583

587584
AVCodecContext* avCodecContext = avcodec_alloc_context3(avCodec);
@@ -633,7 +630,7 @@ void VideoEncoder::initializeEncoder(
633630
if (videoStreamOptions.maxBFrames.has_value()) {
634631
avCodecContext_->max_b_frames = *videoStreamOptions.maxBFrames;
635632
} else {
636-
avCodecContext_->max_b_frames = 2; // Default max B-frames
633+
avCodecContext_->max_b_frames = 0; // No max B-frames to reduce compression
637634
}
638635

639636
int status = avcodec_open2(avCodecContext_.get(), avCodec, nullptr);
@@ -644,6 +641,9 @@ void VideoEncoder::initializeEncoder(
644641

645642
AVStream* avStream = avformat_new_stream(avFormatContext_.get(), nullptr);
646643
TORCH_CHECK(avStream != nullptr, "Couldn't create new stream.");
644+
645+
// Set the stream time base to encode correct frame timestamps
646+
avStream->time_base = avCodecContext_->time_base;
647647
status = avcodec_parameters_from_context(
648648
avStream->codecpar, avCodecContext_.get());
649649
TORCH_CHECK(
@@ -774,6 +774,8 @@ void VideoEncoder::encodeFrame(
774774
"Error receiving packet: ",
775775
getFFMPEGErrorStringFromErrorCode(status));
776776

777+
packet->stream_index = streamIndex_;
778+
777779
status = av_interleaved_write_frame(avFormatContext_.get(), packet.get());
778780
TORCH_CHECK(
779781
status == AVSUCCESS,

0 commit comments

Comments
 (0)