Springboot integrates jave2 to realize audio format conversion

A common framework for processing audio in java

First understand FFmpeg

FFmpeg is an open source software used to generate various libraries and programs for processing multimedia data. FFmpeg can transcode, process videos and pictures (adjust video, picture size, denoise, etc.), package, transmit and play videos. Being the most popular video and image processing software, it is widely used by different companies from all walks of life.

Link: A brief introduction to FFmpeg

  • video compression
  • Support video packaging
  • Video screenshot function
  • add watermark to video
  • Support audio and video container format conversion
  • download video
  • ffprobe is used to obtain information about audio and video files

  • libavformat: used for the generation and analysis of various audio and video encapsulation formats, including functions such as obtaining information required for decoding to generate decoding context structures and reading audio and video frames;
  • libavcodec: used for various types of sound/image codecs;
  • libavutil: Contains some public utility functions;
  • libswscale: for video scene scaling, color mapping conversion;
  • libpostproc: for post-effect processing;
  • ffmpeg: A tool provided by this project, which can be used for format conversion, decoding or real-time encoding of TV cards, etc.;
  • ffsever: an HTTP multimedia instant broadcast streaming server;
  • ffplay: It is a simple player that uses the ffmpeg library to parse and decode, and displays it through SDL;

JAVE2

The JAVE2 (Java Audio Video Encoder) class library is a Java language wrapper of the ffmpeg project. Developers can use JAVE to convert video and audio between different formats. For example, converting AVI to MPEG animation, etc., which can be done in ffmpeg, has corresponding methods in JAVE.

Link: jave2-github address


Advantages: easy to use, can be directly imported into the project to process media files, and can be packaged and released together with the project after development, no need to manually install FFmpeg-related class libraries in the target operating environment

Code

Dependency packages of other systems go to https://github.com/a-schild/jave2 official website to view

  <!-- JAVE2(Java音频视频编码器)库是ffmpeg项目上的Java包装器。 -->
        <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-core</artifactId>
            <version>3.1.1</version>
        </dependency>

        <!-- 在windows上开发 开发机可实现压缩效果 window64位 -->
        <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-nativebin-win32</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-nativebin-win64</artifactId>
            <version>3.1.1</version>
        </dependency>
        
  /**
     * 获取音频文件的编码信息
     *
     * @param filePath
     * @throws EncoderException
     */
    private static void info(String filePath) throws EncoderException {
    
    
        File file = new File(filePath);
        MultimediaObject multimediaObject = new MultimediaObject(file);
        MultimediaInfo info = multimediaObject.getInfo();
        // 时长
        long duration = info.getDuration();
        String format = info.getFormat();
        // format:mov
        System.out.println("format:" + format);
        AudioInfo audio = info.getAudio();
        // 它设置将在重新编码的音频流中使用的音频通道数(1 =单声道,2 =立体声)。如果未设置任何通道值,则编码器将选择默认值。
        int channels = audio.getChannels();
        // 它为新的重新编码的音频流设置比特率值。如果未设置比特率值,则编码器将选择默认值。
        // 该值应以每秒位数表示。例如,如果您想要128 kb / s的比特率,则应调用setBitRate(new Integer(128000))。
        int bitRate = audio.getBitRate();
        // 它为新的重新编码的音频流设置采样率。如果未设置采样率值,则编码器将选择默认值。该值应以赫兹表示。例如,如果您想要类似CD
        // 采样率、音频采样级别 16000 = 16KHz
        int samplingRate = audio.getSamplingRate();

        // 设置音频音量
        // 可以调用此方法来更改音频流的音量。值为256表示音量不变。因此,小于256的值表示音量减小,而大于256的值将增大音频流的音量。
        // setVolume(Integer volume)

        String decoder = audio.getDecoder();

        System.out.println("声音时长:毫秒" + duration);
        System.out.println("声道:" + channels);
        System.out.println("bitRate:" + bitRate);
        System.out.println("samplingRate 采样率、音频采样级别 16000 = 16KHz:" + samplingRate);
        // aac (LC) (mp4a / 0x6134706D)
        System.out.println("decoder:" + decoder);
    }
 /**
     * 音频格式转换
     * @param inputFormatPath
     * @param outputFormatPath
     * @return
     */
    public static boolean audioEncode(String inputFormatPath, String outputFormatPath) {
    
    
        String outputFormat = getSuffix(outputFormatPath);
        String inputFormat = getSuffix(inputFormatPath);
        File source = new File(inputFormatPath);
        File target = new File(outputFormatPath);
        try {
    
    
            MultimediaObject multimediaObject = new MultimediaObject(source);
            // 获取音频文件的编码信息
            MultimediaInfo info = multimediaObject.getInfo();
            AudioInfo audioInfo = info.getAudio();
            //设置音频属性
            AudioAttributes audio = new AudioAttributes();
            audio.setBitRate(audioInfo.getBitRate());
            audio.setSamplingRate(audioInfo.getSamplingRate());
            audio.setChannels(audioInfo.getChannels());
            // 设置转码属性
            EncodingAttributes attrs = new EncodingAttributes();
            attrs.setInputFormat(inputFormat);
            attrs.setOutputFormat(outputFormat);
            attrs.setAudioAttributes(audio);
            // 音频转换格式类
            Encoder encoder = new Encoder();
            // 进行转换
            encoder.encode(new MultimediaObject(source), target, attrs);
            return true;
        } catch (IllegalArgumentException | EncoderException e) {
    
    
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获取文件路径的.后缀
     * @param outputFormatPath
     * @return
     */
    private static String getSuffix(String outputFormatPath) {
    
    
        return outputFormatPath.substring(outputFormatPath.lastIndexOf(".") + 1);
    }

Can call FFmpeg

 /**
     * 剪切视频
     *
     * @param sourcePath
     * @param targetPath
     * @param offetTime  起始时间,格式 00:00:00.000   小时:分:秒.毫秒
     * @param endTime    同上
     * @throws Exception
     */
    public static void cutAv(String sourcePath, String targetPath, String offetTime, String endTime) {
    
    
        try {
    
    
            ProcessWrapper ffmpeg = new DefaultFFMPEGLocator().createExecutor();
            ffmpeg.addArgument("-ss");
            ffmpeg.addArgument(offetTime);
            ffmpeg.addArgument("-t");
            ffmpeg.addArgument(endTime);
            ffmpeg.addArgument("-i");
            ffmpeg.addArgument(sourcePath);
            ffmpeg.addArgument("-vcodec");
            ffmpeg.addArgument("copy");
            ffmpeg.addArgument("-acodec");
            ffmpeg.addArgument("copy");
            ffmpeg.addArgument(targetPath);
            ffmpeg.execute();
            try (BufferedReader br = new BufferedReader(new InputStreamReader(ffmpeg.getErrorStream()))) {
    
    
                blockFfmpeg(br);
            }
            log.info("切除视频成功={}", targetPath);
        } catch (IOException e) {
    
    
            throw new RuntimeException("剪切视频失败");
        }


    }
    /**
     * 等待命令执行成功,退出
     *
     * @param br
     * @throws IOException
     */
    private static void blockFfmpeg(BufferedReader br) throws IOException {
    
    
        String line;
        // 该方法阻塞线程,直至合成成功
        while ((line = br.readLine()) != null) {
    
    
            doNothing(line);
        }
    }
    /**
     * 打印日志,调试阶段可解开注释,观察执行情况
     *
     * @param line
     */
    private static void doNothing(String line) {
    
    
//    log.info(line);
    }

You can search for FFmpeg common commands

reference connection

Link: Java implements video and audio transcoding

Link: audio processing with jave2

Links: 1. Video cutting, compression, music extraction and other operations can be realized through JAVE

Link: JAVE official document

Links: Introduction to audio formats and PCM to WAV conversion

Link: spring boot integrated ffmpeg

Guess you like

Origin blog.csdn.net/qq_41604890/article/details/130409144