bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.45k stars 1.57k forks source link

av_write_frame() error -22 while writing video packet. #1221

Open toskeyfine opened 5 years ago

toskeyfine commented 5 years ago

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.

public ConvertVideoPacket go() throws IOException {
    long errTime = 0;           // error times
    int no_frame_index = 0;     // no frame times
    grabber.flush();
    // It will stop if error times greater than 20
    while(switchMap.get(id) && no_frame_index < 20 && errTime < MAX_WRITE_PACKET_ERR_TIME) {
        AVPacket pkt = null;
        try {
            // get av_packet
            pkt = grabber.grabPacket();
            if(pkt == null || pkt.size() <= 0 || pkt.data() == null || pkt.data().isNull()) {
                no_frame_index++;
                continue;
            }
            // push
            int ret = (record.recordPacket(pkt) ? 0 : 1);
            errTime += ret;
            // release packet
            av_free_packet(pkt);
        } catch (Exception e) { // failed
            logger.error(e.getMessage());
            logger.error("exception! pts=" + pkt.pts() + ", dts=" + pkt.dts());
            errTime++;
        } catch (IOException e) {
            logger.error(e.getMessage());
            logger.error("exception! pts=" + pkt.pts() + ", dts=" + pkt.dts());
            errTime++;
        }
    }
    // gc
    release();
    // reconnect
    if(switchMap.get(id) && cur_reconnect_time < MAX_RECONNECT_TIME) {
        logger.warn("reconnecting... IPC ID=" + id);
        cur_reconnect_time++;
        return this.from(this.src).to(this.output).go();
    } else {
        logger.info("stop forward. IPC ID=" + id);
    }
    return this;
}

/**
 * Release grabber, recorder
 * @throws IOException
 */
private void release() throws IOException {
    record.stop();
    grabber.stop();
    record.close();
    record.release();
    grabber.close();
    grabber.release();
}


> 2019-06-03 15:54:11 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -av_write_frame() error -22 while writing video packet.
2019-06-03 15:54:11 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -exception! pts=205855, dts=205855
2019-06-03 15:54:11 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -av_write_frame() error -22 while writing video packet.
2019-06-03 15:54:11 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -exception! pts=205904, dts=205904
2019-06-03 15:54:11 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -av_write_frame() error -22 while writing video packet.
2019-06-03 15:54:11 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -exception! pts=205954, dts=205954
2019-06-03 15:54:11 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -av_write_frame() error -22 while writing video packet.
2019-06-03 15:54:11 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -exception! pts=206004, dts=206004
2019-06-03 15:54:11 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -av_write_frame() error -22 while writing video packet.
2019-06-03 15:54:11 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -exception! pts=206057, dts=206057
2019-06-03 15:54:11 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -av_write_frame() error -22 while writing video packet.
2019-06-03 15:54:11 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -exception! pts=206104, dts=206104
2019-06-03 15:54:12 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -av_write_frame() error -22 while writing video packet.
2019-06-03 15:54:12 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -exception! pts=206155, dts=206155
2019-06-03 15:54:12 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -av_write_frame() error -22 while writing video packet.
2019-06-03 15:54:12 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -exception! pts=206205, dts=206205
2019-06-03 15:54:12 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -av_write_frame() error -22 while writing video packet.
2019-06-03 15:54:12 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -exception! pts=206254, dts=206254
2019-06-03 15:54:12 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -av_write_frame() error -22 while writing video packet.
2019-06-03 15:54:12 [Thread-22] ERROR com.video.demo.util.javacv.ConvertVideoPacket -exception! pts=206304, dts=206304
2019-06-03 15:54:12 [Thread-22] WARN  com.video.demo.util.javacv.ConvertVideoPacket -reconnecting... IPC ID=4
saudet commented 5 years ago

Probably a duplicate of #1139. Please try again with JavaCV 1.5.

toskeyfine commented 5 years ago

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>
saudet commented 5 years ago

Check the error message that it display on the console. That should help us understand what the issue is.

toskeyfine commented 5 years ago

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)

saudet commented 5 years ago

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().

toskeyfine commented 5 years ago

I was updated my error message

saudet commented 5 years ago

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!

toskeyfine commented 5 years ago

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

vzhn commented 5 years ago

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 ...

yangqx commented 4 years ago

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?

TangataRereke commented 1 year ago

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);
vzhn commented 1 year ago

@TangataRereke Cool trick with av_packet_rescale_ts! Thank you.