From 2010fa1304732a6915ecb04423fdb91e3932c093 Mon Sep 17 00:00:00 2001 From: eguid <17338527+eguid@users.noreply.github.com> Date: Sun, 2 Dec 2018 16:48:52 +0800 Subject: [PATCH] * Set `pts` and `dts` for `AVPacket` in `FFmpegFrameRecorder.recordPacket()` (pull #1097) --- CHANGELOG.md | 1 + .../bytedeco/javacv/FFmpegFrameRecorder.java | 17 +++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e19a09e..ab4fa1ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ + * Set `pts` and `dts` for `AVPacket` in `FFmpegFrameRecorder.recordPacket()` ([pull #1097](https://github.com/bytedeco/javacv/pull/1097)) * Prevent premature deallocations with `LeptonicaFrameConverter` ([issue bytedeco/javacpp#272](https://github.com/bytedeco/javacpp/issues/272)) * Fix `OpenCVFrameGrabber` from crashing when in `ImageMode.GRAY` * Add support for multiple inputs to `FFmpegFrameFilter` ([issue #955](https://github.com/bytedeco/javacv/issues/955)) diff --git a/src/main/java/org/bytedeco/javacv/FFmpegFrameRecorder.java b/src/main/java/org/bytedeco/javacv/FFmpegFrameRecorder.java index 16990aa9..1342c46a 100644 --- a/src/main/java/org/bytedeco/javacv/FFmpegFrameRecorder.java +++ b/src/main/java/org/bytedeco/javacv/FFmpegFrameRecorder.java @@ -1203,23 +1203,28 @@ public boolean recordPacket(AVPacket pkt) throws Exception { } AVStream in_stream = ifmt_ctx.streams(pkt.stream_index()); - - pkt.dts(AV_NOPTS_VALUE); - pkt.pts(AV_NOPTS_VALUE); +/** + * Repair the problem of error decoding and playback caused by the absence of dts/pts + * in the output audio/video file or audio/video stream, + * Comment out this line of code so that PTS / DTS can specify the timestamp manually. + */ +// pkt.dts(AV_NOPTS_VALUE); +// pkt.pts(AV_NOPTS_VALUE); pkt.pos(-1); - if (in_stream.codec().codec_type() == AVMEDIA_TYPE_VIDEO && video_st != null) { pkt.stream_index(video_st.index()); pkt.duration((int) av_rescale_q(pkt.duration(), in_stream.codec().time_base(), video_st.codec().time_base())); - + pkt.pts(av_rescale_q_rnd(pkt.pts(), in_stream.time_base(), video_st.time_base(),(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)));//Increase pts calculation + pkt.dts(av_rescale_q_rnd(pkt.dts(), in_stream.time_base(), video_st.time_base(),(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)));//Increase dts calculation writePacket(AVMEDIA_TYPE_VIDEO, pkt); } else if (in_stream.codec().codec_type() == AVMEDIA_TYPE_AUDIO && audio_st != null && (audioChannels > 0)) { pkt.stream_index(audio_st.index()); pkt.duration((int) av_rescale_q(pkt.duration(), in_stream.codec().time_base(), audio_st.codec().time_base())); - + pkt.pts(av_rescale_q_rnd(pkt.pts(), in_stream.time_base(), audio_st.time_base(),(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)));//Increase pts calculation + pkt.dts(av_rescale_q_rnd(pkt.dts(), in_stream.time_base(), audio_st.time_base(),(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)));//Increase dts calculation writePacket(AVMEDIA_TYPE_AUDIO, pkt); }