关于linux:Linux安装ffmpeg并截取图片和视频的缩略图使用

Linux装置ffmpeg并截取图片和视频的缩略图应用

官网下载地址: http://www.ffmpeg.org/downloa…

  1. 我这里应用版本: ffmpeg_3.2_repo.tar.gz 能够百度网盘分享给大家
  2. 装置的环境为 Centos 64位操作系统
  3. 装置时须为 root 用户进行操作
#解压
tar -zxvf ffmpeg_3.2_repo.tar.gz

#进入目录
cd ffmpeg_3.2_repo

#装置可能须要一点工夫
sh setup.sh

截取图片缩略图命令

ffmpeg -i a.png -y -vf scale=100:100/a  thumb.jpg
  • a.png 原图片
  • 100:100 缩略图宽:缩略图高
  • thumb.jpg 缩略图片

截取视频第一帧缩略图命令

ffmpeg -i bb.mp4 -y -vframes 1 -vf scale=100:100/a thumb.jpg
  • bb.mp4 原视频
  • 100:100 缩略图宽:缩略图高
  • thumb.jpg 缩略图片

调用java命令生成缩略图工具类

package com.beyond.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.InputStreamReader;

/**
 * 执行shell命令ffmpeg工具类 liang
 */
public class ShellCommandUtil {

    private static final Logger logger = LoggerFactory.getLogger(ShellCommandUtil.class);

    /**
     *
     * @param sourceFile 原文件门路
     * @param thumbFile 指标文件门路
     * @param thumbWidth 宽度
     * @param thumbHigh 高度
     * @return
     * ffmpeg -i a.png -y -vf scale=100:100/a  thumb.jpg
     */
    public static boolean ffmpegImg(String sourceFile, String thumbFile, String thumbWidth, String thumbHigh){
        String cmd = " ffmpeg -i " + sourceFile + " -y -vf scale=" + thumbWidth + ":" + thumbHigh + "/a " + thumbFile;
        execCmd(cmd, null);
        logger.info("ShellCommandUtil---ffmpegImg==", cmd);
        File file = new File(thumbFile);
        if(!file.exists()){
            return false;
        }
        return true;
    }

    /**
     *
     * @param sourceFile
     * @param thumbFile
     * @param thumbWidth
     * @param thumbHigh
     * @return
     * ffmpeg -i bb.mp4 -y -vframes 1 -vf scale=100:100/a thumb.jpg
     */
    public static boolean ffmpegVideo(String sourceFile, String thumbFile, String thumbWidth, String thumbHigh){
        String cmd = " ffmpeg -i " + sourceFile + " -y -vframes 1 -vf scale=" + thumbWidth + ":" + thumbHigh + "/a " + thumbFile;
        execCmd(cmd, null);
        logger.info("ShellCommandUtil---ffmpegVideo==", cmd);
        File file = new File(thumbFile);
        if(!file.exists()){
            return false;
        }
        return true;
    }

    /**
     * 执行系统命令, 返回执行后果
     * @param cmd 须要执行的命令
     * @param dir 执行命令的子过程的工作目录, null 示意和以后主过程工作目录雷同
     */
    public static String execCmd(String cmd, File dir) {
        StringBuilder result = new StringBuilder();

        Process process = null;
        BufferedReader bufrIn = null;
        BufferedReader bufrError = null;

        try {
            String[] commond = {"/bin/sh","-c",cmd};
            // 执行命令, 返回一个子过程对象(命令在子过程中执行)
            process = Runtime.getRuntime().exec(commond, null, dir);

            // 办法阻塞, 期待命令执行实现(胜利会返回0)
            process.waitFor();

            // 获取命令执行后果, 有两个后果: 失常的输入 和 谬误的输入(PS: 子过程的输入就是主过程的输出)
            bufrIn = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
            bufrError = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"));

            // 读取输入
            String line = null;
            while ((line = bufrIn.readLine()) != null) {
                result.append(line).append('\n');
            }
            while ((line = bufrError.readLine()) != null) {
                result.append(line).append('\n');
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeStream(bufrIn);
            closeStream(bufrError);

            // 销毁子过程
            if (process != null) {
                process.destroy();
            }
        }

        // 返回执行后果
        return result.toString();
    }

    private static void closeStream(Closeable stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理