bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.55k stars 1.58k forks source link

av_write_frame() error -10053 while writing video packet. #1541

Open banmajio opened 3 years ago

banmajio commented 3 years ago

hello: I am using pipeinputstream to convert H264 data to FLV,When I pause, use the following code to keep the loop going。

 // pause sign
 if (playstatus) {
        Thread.sleep(100);
        continue;
 }

When I continue playing, an error occurs when I loop to get the pkt and call the recordpacket function. This is an error message:

org.bytedeco.javacv.FrameRecorder$Exception: av_write_frame() error -10053 while writing video packet.
[2020-12-02 10:38:34.447] [pool-1-thread-1] [ERROR] [com.junction.play.RtmpPush] : av_write_frame() error -10053 while writing video packet.
    at org.bytedeco.javacv.FFmpegFrameRecorder.writePacket(FFmpegFrameRecorder.java:1257)
    at org.bytedeco.javacv.FFmpegFrameRecorder.recordPacket(FFmpegFrameRecorder.java:1292)
    at com.junction.play.RtmpPush.push(RtmpPush.java:238)
    at com.junction.play.RealPlay.play(RealPlay.java:99)
    at com.junction.thread.CameraThread$MyRunnable.run(CameraThread.java:45)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Warning: [flv @ 0000000026c20200] Failed to update header with correct duration.

Warning: [flv @ 0000000026c20200] Failed to update header with correct filesize.

Here is my code, hope to get your help, thank you very much!!

FFmpegLogCallback.set();
grabber = new FFmpegFrameGrabber(inputStream, 0);
grabber.setVideoOption("vcodec", "copy");
grabber.setFormat("mpeg");
grabber.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
grabber.setVideoCodec(avcodec.AV_CODEC_ID_H264);
grabber.setAudioStream(Integer.MAX_VALUE);
grabber.start();

recorder = new FFmpegFrameRecorder(pojo.getRtmp(), grabber.getImageWidth(), grabber.getImageHeight(),
                    0);
recorder.setInterleaved(true);
recorder.setVideoOptions(this.videoOption);
recorder.setVideoBitrate(bitrate);
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
recorder.setFormat("flv");
recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
recorder.setFrameRate(framerate);
recorder.setGopSize((int) framerate);
AVFormatContext fc = null;
AVPacket pkt = null;
long lasttime = System.currentTimeMillis();
int pktindex = 0;
for (int no_pkt_index = 0; no_pkt_index < 5 && err_index < 5;) {
    long time11 = System.currentTimeMillis();
    // 中断推流任务开关
    if (exitcode == 1) {
        break;
    }
    // pause sign
    if (playstatus) {
        Thread.sleep(100);
        continue;
    }
    pkt = grabber.grabPacket();
    if (pkt == null || pkt.size() <= 0 || pkt.data() == null) {
        no_pkt_index++;
        continue;
    }
    if (pkt.dts() == avutil.AV_NOPTS_VALUE && pkt.pts() == avutil.AV_NOPTS_VALUE || pkt.pts() < dts) {
        err_index++;
        av_packet_unref(pkt);
        continue;
    }
    if (pkt.stream_index() == 1) {
        av_packet_unref(pkt);
        continue;
    } else {
        pkt.pts(pts);
        pkt.dts(dts);
        err_index += (recorder.recordPacket(pkt) ? 0 : 1);
        pktindex++;
        if (pktindex >= framerate * speed) {
            long nowtime = System.currentTimeMillis();
            if (nowtime - lasttime < 1000) {
                Thread.sleep(1000 - (nowtime - lasttime));
            }
            lasttime = System.currentTimeMillis();
            pktindex = 0;
        }
    }
    // pts,dts累加
    timebase = grabber.getFormatContext().streams(pkt.stream_index()).time_base().den();
    pts += (timebase / (int) framerate) / speed;
    dts += (timebase / (int) framerate) / speed;
    av_packet_unref(pkt);
}
banmajio commented 3 years ago

can you help me? @saudet

saudet commented 3 years ago

Maybe, please consider paid support: https://xscode.com/bytedeco/javacv