Open toskeyfine opened 5 years ago
Probably a duplicate of #1139. Please try again with JavaCV 1.5.
I tried to JavaCV 1.5, but it's also has this problem.
this is my pom.xml
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg</artifactId>
<version>4.1.3-1.5</version>
</dependency>
<dependency>
<groupId>com.github.hoary.ffmpeg</groupId>
<artifactId>FFmpeg-linux-x86_64</artifactId>
<version>2.0.1.1</version>
</dependency>
Check the error message that it display on the console. That should help us understand what the issue is.
this is my expcetion message:
Input #0, rtsp, from 'rtsp://admin:12345678a@192.168.9.235:554/h264/ch1/main/av_stream': Metadata: title : stream server comment : stream1 Duration: N/A, start: 0.000000, bitrate: N/A Stream #0:0: Video: h264 (Main), yuvj420p(pc, progressive), 1280x960, 20 tbr, 90k tbn, 180k tbc Stream #0:1: Audio: pcm_mulaw, 8000 Hz, 1 channels, s16, 64 kb/s [flv @ 0000000026775c80] Using AVStream.codec to pass codec parameters to muxers is deprecated, use AVStream.codecpar instead. Output #0, flv, to 'rtmp://192.168.9.120:1936/live/camera_04': Metadata: encoder : Lavf58.20.100 Stream #0:0: Video: h264 (Main) ([7][0][0][0] / 0x0007), yuvj420p(pc, progressive), 1280x960, q=2-31, SAR 1:1 DAR 4:3, 20 fps, 1k tbn, 20 tbc [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7125 [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7175 [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7226 [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7276 [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7325 [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7375 [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7427 [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7483 [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7527 [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7575 [flv @ 0000000026775c80] Failed to update header with correct duration. [flv @ 0000000026775c80] Failed to update header with correct filesize. Input #0, rtsp, from 'rtsp://admin:12345678a@192.168.9.235:554/h264/ch1/main/av_stream': Metadata: title : stream server comment : stream1 Duration: N/A, start: 0.000000, bitrate: N/A Stream #0:0: Video: h264 (Main), yuvj420p(pc, progressive), 1280x960, 20 tbr, 90k tbn, 180k tbc Stream #0:1: Audio: pcm_mulaw, 8000 Hz, 1 channels, s16, 64 kb/s [flv @ 0000000028dd75c0] Using AVStream.codec to pass codec parameters to muxers is deprecated, use AVStream.codecpar instead. Output #0, flv, to 'rtmp://192.168.9.120:1936/live/camera_04': Metadata: encoder : Lavf58.20.100 Stream #0:0: Video: h264 (Main) ([7][0][0][0] / 0x0007), yuvj420p(pc, progressive), 1280x960, q=2-31, SAR 1:1 DAR 4:3, 20 fps, 1k tbn, 20 tbc [flv @ 0000000028dd75c0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41352031 >= 311414 [flv @ 0000000028dd75c0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41352031 >= 311464 [flv @ 0000000028dd75c0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41352031 >= 311514 [flv @ 0000000028dd75c0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41352031 >= 311564 [flv @ 0000000028dd75c0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41352031 >= 311614 [flv @ 0000000028dd75c0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41352031 >= 311665 [flv @ 0000000028dd75c0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41352031 >= 311714 [flv @ 0000000028dd75c0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41352031 >= 311768 [flv @ 0000000028dd75c0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 413520org.bytedeco.javacv.FrameRecorder$Exception: av_write_frame() error -22 while writing video packet. org.bytedeco.javacv.FrameRecorder$Exception: av_write_frame() error -22 while writing video packet. at org.bytedeco.javacv.FFmpegFrameRecorder.writePacket(FFmpegFrameRecorder.java:1171) at org.bytedeco.javacv.FFmpegFrameRecorder.recordPacket(FFmpegFrameRecorder.java:1189) at com.video.demo.util.javacv.ConvertVideoPacket.go(ConvertVideoPacket.java:216) at com.video.demo.service.impl.DeviceServiceImpl$1.run(DeviceServiceImpl.java:75) org.bytedeco.javacv.FrameRecorder$Exception: av_write_frame() error -22 while writing video packet. at org.bytedeco.javacv.FFmpegFrameRecorder.writePacket(FFmpegFrameRecorder.java:1171) at org.bytedeco.javacv.FFmpegFrameRecorder.recordPacket(FFmpegFrameRecorder.java:1189) at com.video.demo.util.javacv.ConvertVideoPacket.go(ConvertVideoPacket.java:216) at com.video.demo.service.impl.DeviceServiceImpl$1.run(DeviceServiceImpl.java:75) org.bytedeco.javacv.FrameRecorder$Exception: av_write_frame() error -22 while writing video packet. at org.bytedeco.javacv.FFmpegFrameRecorder.writePacket(FFmpegFrameRecorder.java:1171) at org.bytedeco.javacv.FFmpegFrameRecorder.recordPacket(FFmpegFrameRecorder.java:1189) at com.video.demo.util.javacv.ConvertVideoPacket.go(ConvertVideoPacket.java:216) at com.video.demo.service.impl.DeviceServiceImpl$1.run(DeviceServiceImpl.java:75) org.bytedeco.javacv.FrameRecorder$Exception: av_write_frame() error -22 while writing video packet. at org.bytedeco.javacv.FFmpegFrameRecorder.writePacket(FFmpegFrameRecorder.java:1171) at org.bytedeco.javacv.FFmpegFrameRecorder.recordPacket(FFmpegFrameRecorder.java:1189) at com.video.demo.util.javacv.ConvertVideoPacket.go(ConvertVideoPacket.java:216) at com.video.demo.service.impl.DeviceServiceImpl$1.run(DeviceServiceImpl.java:75) org.bytedeco.javacv.FrameRecorder$Exception: av_write_frame() error -22 while writing video packet. at org.bytedeco.javacv.FFmpegFrameRecorder.writePacket(FFmpegFrameRecorder.java:1171) at org.bytedeco.javacv.FFmpegFrameRecorder.recordPacket(FFmpegFrameRecorder.java:1189) at com.video.demo.util.javacv.ConvertVideoPacket.go(ConvertVideoPacket.java:216) at com.video.demo.service.impl.DeviceServiceImpl$1.run(DeviceServiceImpl.java:75) org.bytedeco.javacv.FrameRecorder$Exception: av_write_frame() error -22 while writing video packet. at org.bytedeco.javacv.FFmpegFrameRecorder.writePacket(FFmpegFrameRecorder.java:1171) at org.bytedeco.javacv.FFmpegFrameRecorder.recordPacket(FFmpegFrameRecorder.java:1189) at com.video.demo.util.javacv.ConvertVideoPacket.go(ConvertVideoPacket.java:216) at com.video.demo.service.impl.DeviceServiceImpl$1.run(DeviceServiceImpl.java:75)
Look also at the messages that it displays on the console. If you're not seeing any other messages there, make sure to call FFmpegLogCallback.set()
.
I was updated my error message
I see, thanks! grabPacket()
and recordPacket()
are missing some functionality to set the timestamp properly, see: https://github.com/bytedeco/javacv/blob/master/src/main/java/org/bytedeco/javacv/FFmpegFrameRecorder.java#L1241
Contributions are welcome!
I'm overwrite FFmpegFrameRecorder.recordPacket function. this is my code:
public boolean recordPacket(AVPacket pkt) throws Exception {
if (pkt == null) {
return false;
} else {
AVStream in_stream = this.ifmt_ctx.streams(pkt.stream_index());
pkt.pos(-1L);
if (in_stream.codecpar().codec_type() == AVMEDIA_TYPE_VIDEO && this.video_st != null) {
pkt.stream_index(this.video_st.index());
pkt.duration((long)((int)avutil.av_rescale_q(pkt.duration(), in_stream.time_base(), this.video_st.time_base())));
pkt.pts(avutil.av_rescale_q_rnd(pkt.pts(), in_stream.time_base(), this.video_st.time_base(), 8197));
pkt.dts(avutil.av_rescale_q_rnd(pkt.dts(), in_stream.time_base(), this.video_st.time_base(), 8197));
this.writePacket(AVMEDIA_TYPE_VIDEO, pkt);
} else if (in_stream.codecpar().codec_type() == 1 && this.audio_st != null && this.audioChannels > 0) {
pkt.stream_index(this.audio_st.index());
pkt.duration((long)((int)avutil.av_rescale_q(pkt.duration(), in_stream.time_base(), this.audio_st.time_base())));
pkt.pts(avutil.av_rescale_q_rnd(pkt.pts(), in_stream.time_base(), this.audio_st.time_base(), 8197));
pkt.dts(avutil.av_rescale_q_rnd(pkt.dts(), in_stream.time_base(), this.audio_st.time_base(), 8197));
this.writePacket(AVMEDIA_TYPE_AUDIO, pkt);
}
return true;
}
}
it can not fix error.
I see some code was commented
pkt.dts(AV_NOPTS_VALUE); pkt.pts(AV_NOPTS_VALUE);
should I comment out?
I think point is
Error: [flv @ 0000000026745380] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41208477 >= 167340
The gist of the problem is that packets received by RTSP does not contain DTS
RTP packet contains only PTS (timestamp field in RTP header) There is no field for DTS in RTP packet, so it needed to be computed somehow
You need to design an algorithm that computes proper DTS that meets 3 conditions:
1) pts >= dts
2) dts is monotonically increasing
3) difference between pts and dts is reasonably small, like this:
PTS 1 3 2 4 ...
DTS 0 1 2 3 ...
this is my expcetion message:
Input #0, rtsp, from 'rtsp://admin:12345678a@192.168.9.235:554/h264/ch1/main/av_stream': Metadata: title : stream server comment : stream1 Duration: N/A, start: 0.000000, bitrate: N/A Stream #0:0: Video: h264 (Main), yuvj420p(pc, progressive), 1280x960, 20 tbr, 90k tbn, 180k tbc Stream #0:1: Audio: pcm_mulaw, 8000 Hz, 1 channels, s16, 64 kb/s [flv @ 0000000026775c80] Using AVStream.codec to pass codec parameters to muxers is deprecated, use AVStream.codecpar instead. Output #0, flv, to 'rtmp://192.168.9.120:1936/live/camera_04': Metadata: encoder : Lavf58.20.100 Stream #0:0: Video: h264 (Main) ([7][0][0][0] / 0x0007), yuvj420p(pc, progressive), 1280x960, q=2-31, SAR 1:1 DAR 4:3, 20 fps, 1k tbn, 20 tbc [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7125 [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7175 [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7226 [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7276 [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7325 [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7375 [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7427 [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7483 [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7527 [flv @ 0000000026775c80] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41048024 >= 7575 [flv @ 0000000026775c80] Failed to update header with correct duration. [flv @ 0000000026775c80] Failed to update header with correct filesize. Input #0, rtsp, from 'rtsp://admin:12345678a@192.168.9.235:554/h264/ch1/main/av_stream': Metadata: title : stream server comment : stream1 Duration: N/A, start: 0.000000, bitrate: N/A Stream #0:0: Video: h264 (Main), yuvj420p(pc, progressive), 1280x960, 20 tbr, 90k tbn, 180k tbc Stream #0:1: Audio: pcm_mulaw, 8000 Hz, 1 channels, s16, 64 kb/s [flv @ 0000000028dd75c0] Using AVStream.codec to pass codec parameters to muxers is deprecated, use AVStream.codecpar instead. Output #0, flv, to 'rtmp://192.168.9.120:1936/live/camera_04': Metadata: encoder : Lavf58.20.100 Stream #0:0: Video: h264 (Main) ([7][0][0][0] / 0x0007), yuvj420p(pc, progressive), 1280x960, q=2-31, SAR 1:1 DAR 4:3, 20 fps, 1k tbn, 20 tbc [flv @ 0000000028dd75c0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41352031 >= 311414 [flv @ 0000000028dd75c0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41352031 >= 311464 [flv @ 0000000028dd75c0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41352031 >= 311514 [flv @ 0000000028dd75c0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41352031 >= 311564 [flv @ 0000000028dd75c0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41352031 >= 311614 [flv @ 0000000028dd75c0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41352031 >= 311665 [flv @ 0000000028dd75c0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41352031 >= 311714 [flv @ 0000000028dd75c0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 41352031 >= 311768 [flv @ 0000000028dd75c0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 413520org.bytedeco.javacv.FrameRecorder$Exception: av_write_frame() error -22 while writing video packet. org.bytedeco.javacv.FrameRecorder$Exception: av_write_frame() error -22 while writing video packet. at org.bytedeco.javacv.FFmpegFrameRecorder.writePacket(FFmpegFrameRecorder.java:1171) at org.bytedeco.javacv.FFmpegFrameRecorder.recordPacket(FFmpegFrameRecorder.java:1189) at com.video.demo.util.javacv.ConvertVideoPacket.go(ConvertVideoPacket.java:216) at com.video.demo.service.impl.DeviceServiceImpl$1.run(DeviceServiceImpl.java:75) org.bytedeco.javacv.FrameRecorder$Exception: av_write_frame() error -22 while writing video packet. at org.bytedeco.javacv.FFmpegFrameRecorder.writePacket(FFmpegFrameRecorder.java:1171) at org.bytedeco.javacv.FFmpegFrameRecorder.recordPacket(FFmpegFrameRecorder.java:1189) at com.video.demo.util.javacv.ConvertVideoPacket.go(ConvertVideoPacket.java:216) at com.video.demo.service.impl.DeviceServiceImpl$1.run(DeviceServiceImpl.java:75) org.bytedeco.javacv.FrameRecorder$Exception: av_write_frame() error -22 while writing video packet. at org.bytedeco.javacv.FFmpegFrameRecorder.writePacket(FFmpegFrameRecorder.java:1171) at org.bytedeco.javacv.FFmpegFrameRecorder.recordPacket(FFmpegFrameRecorder.java:1189) at com.video.demo.util.javacv.ConvertVideoPacket.go(ConvertVideoPacket.java:216) at com.video.demo.service.impl.DeviceServiceImpl$1.run(DeviceServiceImpl.java:75) org.bytedeco.javacv.FrameRecorder$Exception: av_write_frame() error -22 while writing video packet. at org.bytedeco.javacv.FFmpegFrameRecorder.writePacket(FFmpegFrameRecorder.java:1171) at org.bytedeco.javacv.FFmpegFrameRecorder.recordPacket(FFmpegFrameRecorder.java:1189) at com.video.demo.util.javacv.ConvertVideoPacket.go(ConvertVideoPacket.java:216) at com.video.demo.service.impl.DeviceServiceImpl$1.run(DeviceServiceImpl.java:75) org.bytedeco.javacv.FrameRecorder$Exception: av_write_frame() error -22 while writing video packet. at org.bytedeco.javacv.FFmpegFrameRecorder.writePacket(FFmpegFrameRecorder.java:1171) at org.bytedeco.javacv.FFmpegFrameRecorder.recordPacket(FFmpegFrameRecorder.java:1189) at com.video.demo.util.javacv.ConvertVideoPacket.go(ConvertVideoPacket.java:216) at com.video.demo.service.impl.DeviceServiceImpl$1.run(DeviceServiceImpl.java:75) org.bytedeco.javacv.FrameRecorder$Exception: av_write_frame() error -22 while writing video packet. at org.bytedeco.javacv.FFmpegFrameRecorder.writePacket(FFmpegFrameRecorder.java:1171) at org.bytedeco.javacv.FFmpegFrameRecorder.recordPacket(FFmpegFrameRecorder.java:1189) at com.video.demo.util.javacv.ConvertVideoPacket.go(ConvertVideoPacket.java:216) at com.video.demo.service.impl.DeviceServiceImpl$1.run(DeviceServiceImpl.java:75)
I have the same problem. Have you solved it?
I've had this issue. I found vzhn's useful. After hunting around I did come across av_packet_rescale_ts which helped me:
gPacket->pts = gAvStream->cur_dts;
lResult = avcodec_receive_packet(gCodecContext, gPacket);
av_packet_rescale_ts(gPacket, gCodecContext->time_base, gAvStream->time_base);
gPacket->dts = **av_rescale_q(gAvStream->cur_dts, AV_TIME_BASE_Q, gAvStream->time_base);**
gPacket->stream_index = gAvStream->index;
av_interleaved_write_frame(gFormatContext, gPacket);
@TangataRereke Cool trick with av_packet_rescale_ts
! Thank you.
I use FFmpegFrameRecorder to forward live stream. But every once in a while, always throw exception av_write_frame() error -22 while writing video packet. I find some answer, such like pts must be greater than or equal to dts, but I print pts and dts, they're always equal.