Java transcodes for different video formats

1. Reasons

  The limitation of the front-end Video tag restricts video playback, and there is no sound but no picture when the interface is displayed. As follows:

 Video supports video formats as follows:

preview

Two, the solution

  1. Before uploading the video, use the format factory to encode the format corresponding to the video.

  2. When uploading a video, the Java background processes the video encoding format. 

 Now explain the Java background processing process:

    First introduce the maven package:

<!-- 用于视频转码-->
      <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-core</artifactId>
            <version>2.7.3</version>
        </dependency>

        <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-all-deps</artifactId>
            <version>2.7.3</version>
        </dependency>

Related code:


import ws.schild.jave.*;

import java.io.File;

/**
 * @author allen小哥 2020/1/21 13:55
 *
 * 用于视频转码,更好支持前端video标签播放视频
 *
 */
public class VideoEncoderUtil {

   public static void main(String[] args) {
        String source = "D:\\material\\VIDEO\\视频2.mp4";
        File file = new File(source);
        String target ="D:\\个人素材\\视频\\89898.mp4";
        try {
            transform(file,target);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     *
     * @param source 源文件
     * @param destPath 目标文件
     * @return
     */
    public static File transform(File source , String destPath) {
        File target = new File(destPath);

        EncodingAttributes attrs = getEncodingAttributes("aac","libx264","mp4");

        transferEncoder(source, target, attrs);

        return target;
    }

    /**
     *
     * @param source 源文件
     * @param destPath 目标文件
     * @param audioCodec 音频格式
     * @param videoCodec 视频编码格式
     * @param format 格式
     * @return
     */
    public static File transform(File source , String destPath,String audioCodec,String videoCodec,String format) {
        File target = new File(destPath);

        EncodingAttributes attrs = getEncodingAttributes(audioCodec,videoCodec,format);

        transferEncoder(source, target, attrs);

        return target;
    }

    /**
     *
     * @param source 源文件
     * @param target 目标文件
     * @param attrs 相关配置信息
     */
    private static void transferEncoder(File source, File target, EncodingAttributes attrs) {
        Encoder encoder = new Encoder();
        try {
            encoder.encode(new MultimediaObject(source), target, attrs);
        } catch (Exception e) {
            System.out.println("视频转码失败:" + e);
        }
    }


    /**
     * 转码相关配置信息
     * @param audioCodec 音频格式
     * @param videoCodec  视频编码格式
     * @param format 格式
     * @return
     */
    private static EncodingAttributes getEncodingAttributes(String audioCodec,String videoCodec,String format) {
        AudioAttributes audio = new AudioAttributes();
        //audio.setCodec("libmp3lame"); // mp3
        audio.setCodec(audioCodec);
        audio.setBitRate(new Integer(36000));
        audio.setChannels(new Integer(2)); //1 mono 单声道 2 stereo 立体声
        audio.setSamplingRate(new Integer(44100));

        VideoAttributes video = new VideoAttributes();
        video.setCodec(videoCodec);
        video.setBitRate(new Integer(160000));
        video.setFrameRate(new Integer(15));
        video.setSize(new VideoSize(400, 300));

        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat(format);
        attrs.setAudioAttributes(audio);
        attrs.setVideoAttributes(video);
        return attrs;
    }


}

Three, related documents

https://blog.csdn.net/qq_34806812/article/details/81361817

https://blog.csdn.net/luohai859/article/details/52496054

https://segmentfault.com/q/1010000014656374

https://github.com/a-schild/jave2

http://www.sauronsoftware.it/projects/jave/manual.php

Four, related issues

A、Unknown encoder 'libmp3lame4'

  audio.setCodec("libmp3lame4"); // means there is no such audio encoder

Adjustment: audio.setCodec("aac");

Guess you like

Origin blog.csdn.net/baidu_28068985/article/details/104062431