bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.6k stars 1.59k forks source link

The duration and audio of the merged video do not match correctly #2258

Closed lbz303 closed 3 months ago

lbz303 commented 3 months ago

My video merging code, when merging multiple videos with different bitrates and resolutions, results in the merged video's duration and audio not matching correctly.

The duration of the merged video exceeds the total duration of all the original videos. The audio does not correspond correctly with the video. The temporary files generated before merging are stored locally and appear to be normal, but the final merged video has issues.

`List videoList = Files.readAllLines(Paths.get(tempFileList)); String replaced = videoList.get(0).replace("file '", "").replace("'", ""); FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(replaced); grabber.start(); FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(mergedVideoPath, width, height); recorder.setVideoCodec(AV_CODEC_ID_H264); recorder.setAudioChannels(1); recorder.setInterleaved(true); recorder.setFormat("mp4"); recorder.setFrameRate(maxFrameRate); recorder.setVideoBitrate(16 1000 1000); // 16 Mbps

        av_log_set_level(AV_LOG_ERROR);
        recorder.setPixelFormat(AV_PIX_FMT_YUV420P);
        recorder.setVideoBitrate(jsonObject.getInteger("bitrate"));
        recorder.start();
        Frame frame;
        for (String videoPath : videoList) {
            try (PointerScope scope = new PointerScope()) {
                FFmpegFrameGrabber grabberTemp = new FFmpegFrameGrabber(videoPath.replace("file '", "").replace("'", ""));
                grabberTemp.start();
                while ((frame = grabberTemp.grabFrame()) != null) {
                    recorder.record(frame);
                }
                grabberTemp.stop();
                grabberTemp.close();
            }
        }

        recorder.stop();
        recorder.close();`

Could you also provide an example code for merging videos with different resolutions, bitrates, and audio settings into a single unified format?

saudet commented 3 months ago

We can easily accomplish this with the ffmpeg program: http://bytedeco.org/javacpp-presets/ffmpeg/apidocs/org/bytedeco/ffmpeg/ffmpeg.html

lbz303 commented 3 months ago

I am currently using the following method to merge two videos. The first video has normal audio and video, but the second video only has audio, with the screen being black. The duration and resolution of both videos are correct. Could you please explain the potential cause of this issue?

        ProcessBuilder pb = new ProcessBuilder(
                ffmpeg, "-f", "concat", "-safe", "0", "-i", FILE_PATH,
                "-vf", "scale=" + width + ":" + height, "-r", String.valueOf(maxFrameRate),
                "-b:v", String.valueOf(bitrate),
                "-c:v", "h264", "-c:a", "aac", "-strict", "experimental", mergedVideoPath);
lbz303 commented 3 months ago

I've resolved my issue, thank you for your assistance