bytedeco/javacv

how to get notified as javacv finished writing each segment

Open

#2,275 opened on Aug 16, 2024

View on GitHub
 (0 comments) (0 reactions) (0 assignees)Java (1,583 forks)batch import
help wantedquestion

Repository metrics

Stars
 (6,985 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

I use javacv to make hls segments from mp4 and upload them to cloud, everything works fine except one drawback, the process waits all segments finished, then start to upload, which is acceptable for small video, but for large video it wastes time. is there any mechanism in javacv that I can get notified after one segment finished writing . below is my code

FFmpegFrameRecorder recorder = null;
        FFmpegLogCallback.set();
        try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(path)
        ) {
            long start = System.currentTimeMillis();
            System.out.println("start segment " + start);
            grabber.start(true);
            recorder = new FFmpegFrameRecorder(output, grabber.getImageWidth(), grabber.getImageHeight(),
                    grabber.getAudioChannels());

            recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
            recorder.setVideoOption("crf", "18");
            recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC);

            recorder.setFormat("hls");
            recorder.setOption("hls_time", "2");
            recorder.setOption("hls_list_size", "0");
            recorder.setOption("start_number", "0");

            AVFormatContext formatContext = grabber.getFormatContext();

            recorder.start();
            AVPacket pkt = null;
            Frame frame = null;
            long dts = 0, pts = 0;
            grabber.flush();
            while ((frame = grabber.grab()) != null) {
                recorder.record(frame);
            }
//            while ((pkt = grabber.grabPacket()) != null) {
//                recorder.recordPacket(pkt);
//            }
            System.out.println("end segment total time " + (System.currentTimeMillis() - start));
        } finally {
            if (recorder != null) {
                recorder.close();
            }
        }

Contributor guide