Skip to content

I use grabber and record for video processing vs directly using ffmpeg has different effects. #2377

@Aismy

Description

@Aismy

javacv version 1.5.9 ffmpeg version 6.0

I have two test cases, one is to use ffmpeg directly through javacv, and the other is to process videos through grabber and recorder in Java. The code is as follows

@Test
public void testFfmpegWithJavaCV() throws IOException, InterruptedException {
    String inputPath = "D:\\video\\test.mp4";
    String outputPath = "D:\\video\\output_ffmpeg_500k.mp4";
    String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
    long startTime = System.currentTimeMillis();
    ProcessBuilder pb = new ProcessBuilder(ffmpeg, "-i", inputPath, "-vcodec", "h264", "-b:v", "500k",  "-bufsize", "2M", outputPath);
    pb.inheritIO().start().waitFor();
    log.info("Compression completed in {}ms", System.currentTimeMillis() - startTime); 
}


@Test
public void testRecord() throws FrameRecorder.Exception, FrameGrabber.Exception {
    String inputPath = "D:\\video\\test.mp4";
    String outputPath = "D:\\video\\output_javacv_500k.mp4";
    try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputPath);
         ) {
        grabber.start();
        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(outputPath, grabber.getImageWidth(), grabber.getImageHeight(),grabber.getAudioChannels());
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
        recorder.setFrameRate(grabber.getFrameRate());
        recorder.setVideoBitrate(500 * 1000);
        recorder.setOption("bufsize", "2M");

        recorder.setAudioCodecName("copy");
        recorder.setAudioBitrate(grabber.getAudioBitrate());
        recorder.start();
        long startTime = System.currentTimeMillis();
        Frame frame;
        while ((frame = grabber.grab()) != null) {
            recorder.record(frame);
        }
        recorder.stop();
        recorder.release();
        log.info("Compression completed in {}ms", System.currentTimeMillis() - startTime);
    }
}

@Test
public void testFfmpegWithCmd() throws IOException, InterruptedException {
    String inputPath = "D:\\视频素材\\35532179568-1-192.mp4";
    String outputPath = "D:\\视频素材\\output_ffmpeg_500k.mp4";
    String ffmpegcmd = "E:\\server\\ffmpeg-6.0-essentials\\bin\\ffmpeg.exe";
    long startTime = System.currentTimeMillis();
    ProcessBuilder pb = new ProcessBuilder(ffmpegcmd, "-i", inputPath, "-vcodec", "h264", "-b:v", "500k",  "-bufsize", "2M", outputPath);
    pb.inheritIO().start().waitFor();
    log.info("Compression completed in {}ms", System.currentTimeMillis() - startTime);
}

testFfmpegWithJavaCV log output

19:54:28.673 [main] INFO com.macronsan.FfmpegTest - Compression completed in 41226ms
ffmpeg version 6.0 Copyright (c) 2000-2023 the FFmpeg developers
built with gcc 10.3.0 (Rev8, Built by MSYS2 project)
configuration: --prefix=.. --disable-iconv --disable-opencl --disable-vulkan --disable-sdl2 --disable-bzlib --disable-lzma --disable-linux-perf --disable-xlib --enable-shared --enable-version3 --enable-runtime-cpudetect --enable-zlib --enable-libmp3lame --enable-libspeex --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-openssl --enable-libopenh264 --enable-libvpx --enable-libfreetype --enable-libopus --enable-libxml2 --enable-libsrt --enable-libwebp --enable-libaom --enable-libsvtav1 --enable-cuda --enable-cuvid --enable-nvenc --enable-libmfx --enable-w32threads --enable-indev=dshow --target-os=mingw32 --cc='gcc -m64' --extra-cflags='-DLIBXML_STATIC -I../include/ -I../include/libxml2 -I../include/mfx/ -I../include/svt-av1' --extra-ldflags=-L../lib/ --extra-libs='-static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lgcc_eh -lWs2_32 -lcrypt32 -lpthread -lz -lm -Wl,-Bdynamic -lole32 -luuid'
libavutil 58. 2.100 / 58. 2.100
libavcodec 60. 3.100 / 60. 3.100
libavformat 60. 3.100 / 60. 3.100
libavdevice 60. 1.100 / 60. 1.100
libavfilter 9. 3.100 / 9. 3.100
libswscale 7. 1.100 / 7. 1.100
libswresample 4. 10.100 / 4. 10.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'D:\视频素材\35532179568-1-192.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf59.27.100
description : Packed by Bilibili XCoder v2.0.2
Duration: 00:03:39.41, start: 0.000000, bitrate: 1067 kb/s
Stream #0:00x1: Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1280x720 [SAR 1:1 DAR 16:9], 859 kb/s, 30 fps, 30 tbr, 16k tbn (default)
Metadata:
handler_name : VideoHandler
vendor_id : [0][0][0][0]
Stream #0:10x2: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 198 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (libopenh264))
Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[libopenh264 @ 000001c031135f00] [OpenH264] this = 0x000001c0332fefa0, Warning:layerId(0) doesn't support profile(578), change to UNSPECIFIC profile
[libopenh264 @ 000001c031135f00] [OpenH264] this = 0x000001c0332fefa0, Warning:bEnableFrameSkip = 0,bitrate can't be controlled for RC_QUALITY_MODE,RC_BITRATE_MODE and RC_TIMESTAMP_MODE without enabling skip frame.
Output #0, mp4, to 'D:\视频素材\output_javacv_ffmpeg_500k.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
description : Packed by Bilibili XCoder v2.0.2
encoder : Lavf60.3.100
Stream #0:0(und): Video: h264 (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 500 kb/s, 30 fps, 15360 tbn (default)
Metadata:
handler_name : VideoHandler
vendor_id : [0][0][0][0]
encoder : Lavc60.3.100 libopenh264
Side data:
cpb: bitrate max/min/avg: 500000/0/500000 buffer size: 0 vbv_delay: N/A
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]
encoder : Lavc60.3.100 aac
frame= 6582 fps=160 q=-0.0 Lsize= 17327kB time=00:03:39.38 bitrate= 647.0kbits/s speed=5.33x
video:13703kB audio:3441kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.067410%
[aac @ 000001c02f86b740] Qavg: 424.198

testRecord log output

19:57:16.675 [main] INFO com.macronsan.FfmpegTest - Compression completed in 56071ms
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'D:\视频素材\35532179568-1-192.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf59.27.100
description : Packed by Bilibili XCoder v2.0.2
Duration: 00:03:39.41, start: 0.000000, bitrate: 1067 kb/s
Stream #0:00x1: Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1280x720 [SAR 1:1 DAR 16:9], 859 kb/s, 30 fps, 30 tbr, 16k tbn (default)
Metadata:
handler_name : VideoHandler
vendor_id : [0][0][0][0]
Stream #0:10x2: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 198 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]
[libopenh264 @ 0000000025958dc0] [OpenH264] this = 0x0000000024021890, Warning:layerId(0) doesn't support profile(578), change to UNSPECIFIC profile
[libopenh264 @ 0000000025958dc0] [OpenH264] this = 0x0000000024021890, Warning:bEnableFrameSkip = 0,bitrate can't be controlled for RC_QUALITY_MODE,RC_BITRATE_MODE and RC_TIMESTAMP_MODE without enabling skip frame.
Output #0, mp4, to 'D:\视频素材\output_javacv_500k.mp4':
Metadata:
encoder : Lavf60.3.100
Stream #0:0: Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1280x720, q=2-31, 500 kb/s, 30 fps, 658203 tbn
Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 198 kb/s
[aac @ 00000000259575c0] Qavg: 405.536
[aac @ 00000000259575c0] 1 frames left in the queue on closing

testFfmpegWithCmd log output

19:58:35.378 [main] INFO com.macronsan.FfmpegTest - Compression completed in 24832ms
ffmpeg version 6.0-essentials_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers
built with gcc 12.2.0 (Rev10, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libvpl --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberband
libavutil 58. 2.100 / 58. 2.100
libavcodec 60. 3.100 / 60. 3.100
libavformat 60. 3.100 / 60. 3.100
libavdevice 60. 1.100 / 60. 1.100
libavfilter 9. 3.100 / 9. 3.100
libswscale 7. 1.100 / 7. 1.100
libswresample 4. 10.100 / 4. 10.100
libpostproc 57. 1.100 / 57. 1.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'D:\视频素材\35532179568-1-192.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf59.27.100
description : Packed by Bilibili XCoder v2.0.2
Duration: 00:03:39.41, start: 0.000000, bitrate: 1067 kb/s
Stream #0:00x1: Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1280x720 [SAR 1:1 DAR 16:9], 859 kb/s, 30 fps, 30 tbr, 16k tbn (default)
Metadata:
handler_name : VideoHandler
vendor_id : [0][0][0][0]
Stream #0:10x2: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 198 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 000001edbf5b5800] VBV maxrate unspecified, assuming CBR
[libx264 @ 000001edbf5b5800] using SAR=1/1
[libx264 @ 000001edbf5b5800] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 000001edbf5b5800] profile High, level 3.1, 4:2:0, 8-bit
[libx264 @ 000001edbf5b5800] 264 - core 164 r3106 eaa68fa - H.264/MPEG-4 AVC codec - Copyleft 2003-2023 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=18 lookahead_threads=3 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=cbr mbtree=1 bitrate=500 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 vbv_maxrate=500 vbv_bufsize=2000 nal_hrd=none filler=0 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'D:\视频素材\output_ffmpeg_500k.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
description : Packed by Bilibili XCoder v2.0.2
encoder : Lavf60.3.100
Stream #0:0(und): Video: h264 (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 500 kb/s, 30 fps, 15360 tbn (default)
Metadata:
handler_name : VideoHandler
vendor_id : [0][0][0][0]
encoder : Lavc60.3.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/500000 buffer size: 2000000 vbv_delay: N/A
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]
encoder : Lavc60.3.100 aac
frame= 6582 fps=298 q=-1.0 Lsize= 16735kB time=00:03:39.38 bitrate= 624.9kbits/s speed=9.93x
video:13060kB audio:3441kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.416561%
[libx264 @ 000001edbf5b5800] frame I:31 Avg QP:23.97 size: 51399
[libx264 @ 000001edbf5b5800] frame P:1857 Avg QP:26.29 size: 4609
[libx264 @ 000001edbf5b5800] frame B:4694 Avg QP:29.91 size: 686
[libx264 @ 000001edbf5b5800] consecutive B-frames: 1.4% 9.8% 2.4% 86.4%
[libx264 @ 000001edbf5b5800] mb I I16..4: 27.3% 57.1% 15.6%
[libx264 @ 000001edbf5b5800] mb P I16..4: 2.6% 2.0% 0.0% P16..4: 30.3% 3.9% 2.6% 0.0% 0.0% skip:58.6%
[libx264 @ 000001edbf5b5800] mb B I16..4: 0.0% 0.0% 0.0% B16..8: 21.9% 0.1% 0.0% direct: 0.0% skip:77.9% L0:41.0% L1:58.8% BI: 0.2%
[libx264 @ 000001edbf5b5800] 8x8 transform intra:46.6% inter:85.2%
[libx264 @ 000001edbf5b5800] coded y,uvDC,uvAC intra: 21.4% 37.3% 9.2% inter: 2.2% 3.1% 0.1%
[libx264 @ 000001edbf5b5800] i16 v,h,dc,p: 17% 45% 10% 29%
[libx264 @ 000001edbf5b5800] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 9% 26% 46% 2% 3% 2% 7% 1% 4%
[libx264 @ 000001edbf5b5800] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 9% 43% 11% 4% 6% 3% 14% 2% 7%
[libx264 @ 000001edbf5b5800] i8c dc,h,v,p: 58% 31% 8% 3%
[libx264 @ 000001edbf5b5800] Weighted P-Frames: Y:2.4% UV:1.2%
[libx264 @ 000001edbf5b5800] ref P L0: 62.6% 15.2% 15.9% 6.3% 0.0%
[libx264 @ 000001edbf5b5800] ref B L0: 91.7% 6.8% 1.5%
[libx264 @ 000001edbf5b5800] ref B L1: 97.1% 2.9%
[libx264 @ 000001edbf5b5800] kb/s:487.61
[aac @ 000001edbf5b6d40] Qavg: 424.198

test result

output size:testFfmpegWithJavaCV output video is 16.3M ; testRecord output vide is 18.7M testFfmpegWithCmd 16.3M
Execution time : testFfmpegWithJavaCV 41s; testRecord 56s ; testFfmpegWithCmd 24s

Image

why result has different effects? Looking forward to your reply

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions