• Linux安装ffmpeg并截取图片和视频的缩略图使用


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

    官方下载地址: http://www.ffmpeg.org/download.html#releases

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    截取图片缩略图命令

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

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

    ffmpeg -i bb.mp4 -y -vframes 1 -vf scale=100:100/a thumb.jpg
    
    • 1
    • 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();
    			}
    		}
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
  • 相关阅读:
    Python数据可视化------下载数据
    海格里斯HEGERLS工程项目案例|陕西西安某新能源电池制造集团企业三期自放电立体库工程项目安装过程
    卸载重装最新版mysql数据库亲测有效
    十一、动态规划题目相关
    RocketMq快速入门(详解)
    软件测试 app自动化01 APP环境搭建 Appium自动化测试原理
    基于springboot的通知反馈系统
    墨天轮“高可用架构”干货文档分享(含Oracle、MySQL、PG资料124篇)
    RK3588移植-ffmpeg交叉编译
    ReentrantLock锁与AQS的联系
  • 原文地址:https://blog.csdn.net/yinjl123/article/details/134273788