diff --git a/src/FFmpegReader.cpp b/src/FFmpegReader.cpp index b97d7345..98d39ce1 100644 --- a/src/FFmpegReader.cpp +++ b/src/FFmpegReader.cpp @@ -768,12 +768,12 @@ void FFmpegReader::UpdateVideoInfo() { // Check for valid duration (if found) if (info.duration <= 0.0f && pFormatCtx->duration >= 0) // Use the format's duration - info.duration = pFormatCtx->duration / AV_TIME_BASE; + info.duration = float(pFormatCtx->duration) / AV_TIME_BASE; // Calculate duration from filesize and bitrate (if any) if (info.duration <= 0.0f && info.video_bit_rate > 0 && info.file_size > 0) // Estimate from bitrate, total bytes, and framerate - info.duration = (info.file_size / info.video_bit_rate); + info.duration = float(info.file_size) / info.video_bit_rate; // No duration found in stream of file if (info.duration <= 0.0f) { diff --git a/src/FFmpegWriter.cpp b/src/FFmpegWriter.cpp index 92e9f8fa..f3b12800 100644 --- a/src/FFmpegWriter.cpp +++ b/src/FFmpegWriter.cpp @@ -947,7 +947,7 @@ void FFmpegWriter::flush_encoders() { // Increment PTS by duration of packet audio_timestamp += pkt.duration; - // deallocate memory for packet + // deallocate memory for packet AV_FREE_PACKET(&pkt); } } @@ -1885,8 +1885,8 @@ void FFmpegWriter::write_audio_packets(bool is_final) { ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_audio_packets ERROR [" + (std::string) av_err2str(error_code) + "]", "error_code", error_code); } - // Increment PTS by duration of packet - audio_timestamp += pkt.duration; + // Increment PTS (no pkt.duration, so calculate with maths) + audio_timestamp += FFMIN(audio_input_frame_size, audio_input_position); // deallocate AVFrame av_freep(&(frame_final->data[0])); @@ -2134,9 +2134,6 @@ bool FFmpegWriter::write_video_packet(std::shared_ptr frame, AVFrame *fra } } - // Increment PTS (in frames and scaled to the codec's timebase) - video_timestamp += pkt.duration; - // Deallocate packet AV_FREE_PACKET(&pkt); #if USE_HW_ACCEL @@ -2149,6 +2146,9 @@ bool FFmpegWriter::write_video_packet(std::shared_ptr frame, AVFrame *fra #endif // USE_HW_ACCEL } + // Increment PTS (in frames and scaled to the codec's timebase) + video_timestamp += av_rescale_q(1, av_make_q(info.fps.den, info.fps.num), video_codec_ctx->time_base); + // Success return true; }