@@ -666,7 +666,6 @@ void VideoEncoder::initializeEncoder(
666
666
status == AVSUCCESS,
667
667
" avcodec_parameters_from_context failed: " ,
668
668
getFFMPEGErrorStringFromErrorCode (status));
669
- streamIndex_ = avStream_->index ;
670
669
}
671
670
672
671
void VideoEncoder::encode () {
@@ -791,15 +790,16 @@ void VideoEncoder::encodeFrame(
791
790
" Error receiving packet: " ,
792
791
getFFMPEGErrorStringFromErrorCode (status));
793
792
793
+ // The code below is borrowed from torchaudio:
794
+ // https://github.com/pytorch/audio/blob/b6a3368a45aaafe05f1a6a9f10c68adc5e944d9e/src/libtorio/ffmpeg/stream_writer/encoder.cpp#L46
795
+ // Setting packet->duration to 1 allows the last frame to be properly
796
+ // encoded, and needs to be set before calling av_packet_rescale_ts.
794
797
if (packet->duration == 0 ) {
795
798
packet->duration = 1 ;
796
799
}
797
- // av_packet_rescale_ts ensures encoded frames have correct timestamps.
798
- // This prevents "no more frames" errors when decoding encoded frames,
799
- // https://github.com/pytorch/audio/blob/b6a3368a45aaafe05f1a6a9f10c68adc5e944d9e/src/libtorio/ffmpeg/stream_writer/encoder.cpp#L46
800
800
av_packet_rescale_ts (
801
801
packet.get (), avCodecContext_->time_base , avStream_->time_base );
802
- packet->stream_index = streamIndex_ ;
802
+ packet->stream_index = avStream_-> index ;
803
803
804
804
status = av_interleaved_write_frame (avFormatContext_.get (), packet.get ());
805
805
TORCH_CHECK (
0 commit comments