Skip to content

Example 11: divides audio Media file into a series of small media segments

Viktor edited this page Feb 5, 2023 · 2 revisions
    @Test
    public void testAudio() throws IOException {
        FFmpeg ffmpeg = new FFmpeg("/path/to/ffmpeg");
        FFprobe ffprobe = new FFprobe("/path/to/ffprobe");

        final FFmpegProbeResult probe = ffprobe.probe("e:/hls/test/output.wav");

        final List<FFmpegStream> streams = probe.getStreams().stream().filter(fFmpegStream -> fFmpegStream.codec_type!=null).collect(Collectors.toList());

        final Optional<FFmpegStream> audioStream = streams.stream().filter(fFmpegStream -> FFmpegStream.CodecType.AUDIO.equals(fFmpegStream.codec_type)).findFirst();

        if(!audioStream.isPresent())
        {
            System.err.println("no audio stream found!");
        }

        FFmpegBuilder builder = new FFmpegBuilder()
                .setInput("e:/hls/test/output.wav")
                .overrideOutputFiles(true)
                .addOutput("e:/hls/test/output-wav.m3u8")
                .setFormat("wav")
                .setAudioBitRate(audioStream.isPresent()?audioStream.get().bit_rate:0)
                .setAudioChannels(1)
                .setAudioCodec("aac")        // using the aac codec
                .setAudioSampleRate(audioStream.get().sample_rate)
                .setAudioBitRate(audioStream.get().bit_rate)
                .setStrict(FFmpegBuilder.Strict.STRICT)
                .setFormat("hls")
                .addExtraArgs("-hls_wrap", "0", "-hls_time", "5", "-hls_list_size","0")
                .done();



        FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);

        // Run a one-pass encode
        executor.createJob(builder).run();
    }