ffmpeg 视频处理工具部署、JAVA应用

官方地址:https://www.ffmpeg.org

1、windows 安装

配置环境变量:D:\ffmpeg-4.3.1\bin

dos窗口输入 ffmpeg -version 命令,如有信息则说明成功

2、Linux 安装

# wget http://www.ffmpeg.org/releases/ffmpeg-4.3.tar.gz
# tar -zxvf ffmpeg-4.3.tar.gz
# cd ffmpeg-4.3
# ./configure --prefix=/usr/local/ffmpeg
# make && make install
 
等待安装完成...
 
# vi /etc/profile
在最后PATH添加环境变量:
PATH=$PATH:/usr/local/ffmpeg/bin
export PATH
保存退出
 
# source /etc/profile   设置生效
# ffmpeg -version       查看版本

若安装过程中出现以下错误:

yasm/nasm not found or too old. Use –disable-yasm for a crippled build.

If you think configure made a mistake, make sure you are using the latest
version from Git. If the latest version fails, report the problem to the
ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net.
Include the log file “config.log” produced by configure as this will help
solve the problem.

需要安装yasm

# wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
# tar -zxvf yasm-1.3.0.tar.gz
# cd yasm-1.3.0
# ./configure
# make && make install

结束!

3、JAVA 调用(第三方包 jave-1.0.2.jar)

package com.xrh.core.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import it.sauronsoftware.jave.Encoder;

/**
 * ffmpeg 图片处理工具
 * 
 * @author 李小家
 */
public class FfmpegUtil {

    private static final Logger LOGGER = Logger.getLogger(FfmpegUtil.class.getName());

    private FfmpegUtil() {
    }

    /**
     * 截取视频文件第1秒的图片
     * 
     * @param veidoPath
     *            视频文件完整路径
     * @param ffmpegPath
     *            ffmpeg.exe 文件地址
     * @param widthXheight
     *            截图的宽高,如:1920x1080, 默认为视频宽高
     * @return 截取到的图片完整路径
     */
    public static String screenShot(String veidoPath, String ffmpegPath, String widthXheight) {

        LOGGER.info("veidoPath=" + veidoPath + "  ffmpegPath=" + ffmpegPath);
        LOGGER.info("osName=" + System.getProperty("os.name"));
        String osName = System.getProperty("os.name");
        if (osName.contains("Windows") && veidoPath.startsWith("/")) {
            veidoPath = veidoPath.substring(1);
        }
        File file = new File(veidoPath);

        if (!file.exists()) {
            LOGGER.warning("路径[" + veidoPath + "]对应的视频文件不存在!");
            return null;
        }

        String imagePath = veidoPath.substring(0, veidoPath.lastIndexOf(".")) + ".jpg";

        List<String> commands = new java.util.ArrayList<String>();

        commands.add(ffmpegPath);
        commands.add("-i");
        commands.add(veidoPath);
        commands.add("-y");
        commands.add("-f");
        commands.add("image2");
        commands.add("-ss");
        commands.add("1");// 这个参数是设置截取视频多少秒时的画面
        commands.add("-t");
        commands.add("0.001");

        if (!ObjectUtil.isNull(widthXheight)) {
            commands.add("-s");
            commands.add(widthXheight);// 宽X高
        }

        commands.add(imagePath);

        try {
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commands);
            builder.start();
            while (!new File(imagePath).exists()) {
                LOGGER.info("文件尚未生成完毕....");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException ie) {
                }
            }
            LOGGER.info("截取成功,图片地址:" + imagePath);
            return imagePath;
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.warning("视频文件【" + veidoPath + "】截图失败:" + e.getMessage());
            return null;
        }

    }
    
    /**
     * 读取视频文件信息
     * @param path
     * @return
     */
    public static Map<String, Object> getVoideMsg(String path) {
        Map<String, Object> map = new HashMap<String, Object>();
        File file = new File(path);
        Encoder encoder = new Encoder();
        FileChannel fc = null;
        FileInputStream fis = null;
        BigDecimal size ;

        if (file != null) {
            try {
                it.sauronsoftware.jave.MultimediaInfo m = encoder.getInfo(file);
                fis = new FileInputStream(file);
                fc = fis.getChannel();
                BigDecimal fileSize = new BigDecimal(fc.size());
                size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP);
                
                map.put("duration", m.getDuration() / 1000);  //时长S
                map.put("height", m.getVideo().getSize().getHeight()); //高度
                map.put("width", m.getVideo().getSize().getWidth());   //宽度
                map.put("size", size);   //大小M
                map.put("format", m.getFormat()); //格式 
            } catch (Exception e) {
                LOGGER.warning("操作出现异常,截取失败:" + e.getMessage());
            } finally {
                if (null != fc) {
                    try {
                        fc.close();
                    } catch (IOException e) {
                    }
                }
                if (null != fis) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                    }
                }
            }
        }

        return map;
    }
    
    public static void main(String[] args) {
        LOGGER.info(getVoideMsg("d:/397181e6e4ca8084770c1a1284b2ed41.mp4").toString());
//        LOGGER.info(screenShot(
//                "/D:/project/xrh_api/WebRoot/upvideo/xrh_api/2020-12/5c832aad-43df-4188-8d61-07cd3bd9ab39.mp4",
//                ConfigUtil.getProperty("ffmpeg_path"), null));

    }

}

 

posted @ 2021-02-02 13:56  李小加  阅读(90)  评论(0编辑  收藏  举报