Linux下使用ffmpeg对视频截图

来源:互联网 发布:怎么看淘宝销售排名 编辑:程序博客网 时间:2024/05/16 17:40

Linux下使用ffmpeg对视频截图

一、安装ffmpeg

  • 1.下载ffmpeg包
wget http://www.ffmpeg.org/releases/ffmpeg-3.3.tar.bz2
  • 2.解压到指定目录

wget默认是在/root目录下

tar   -jxvf /root/ffmpeg-3.3.tar.bz2 -C /usr
  • 3.前往解压好的目录下编译
cd /usr/ffmpeg-3.3./configuremake (这个步骤可能会很慢 耐心等待)make install若./configure出现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 latestversion from Git. If the latest version fails, report the problem to theffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net.Include the log file “config.log” produced by configure as this will helpsolve the problem.则执行第四步,执行完后再回来执行此步骤
  • 4.下载最新yasm
http://yasm.tortall.net/Download.html(我下载的是1.2.0版本)解压tar -zxvf /root/yasm-1.2.0.tar.gz -C /usr 编译(完成后返回执行第三步)cd /usr/yasm-1.2.0./configuremakemake install
  • 5.查看ffmpeg是否安装成功
输入ffmpeg命令,出现以下信息则表示安装成功:ffmpeg version 3.3 Copyright (c) 2000-2017 the FFmpeg developers  built with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-17)  configuration:  libavutil      55. 58.100 / 55. 58.100  libavcodec     57. 89.100 / 57. 89.100  libavformat    57. 71.100 / 57. 71.100  libavdevice    57.  6.100 / 57.  6.100  libavfilter     6. 82.100 /  6. 82.100  libswscale      4.  6.100 /  4.  6.100  libswresample   2.  7.100 /  2.  7.100Hyper fast Audio and Video encoderusage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...Use -h to get full help or, even better, run 'man ffmpeg'

二、java实现类

  • 1.编写ffmpeg.sh文件,代码如下:
#!/bin/bashffmpeg -i $1 -ss $2 -vframes 1 -r 1 -ac 1 -ab 2 -s 320*180 -f  image2 $3
-i 输入参数-ss 从多少秒开始-s 分辨率-f 导出格式$1 $2 $3 分别为:源视频地址、开始时间、图片保存地址
  • 2.测试ffmpeg.sh文件是否有效

我的ffmpeg.sh文件保存在/usr目录下,后面的几个是参数

sh /usr/ffmpeg.sh /usr/test.mp4 5 /usr/test.jpg

查看/usr下是否有test.jpg文件,然后查看图片是否为视频的第5秒画面

  • 3.java代码
    调用getVideoSamllImage()时会返回0和1,0表示执行成功,1则表示失败
package com.tes.hsws.web.util;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class FfmpegUtil {    private final static Log logger = LogFactory.getLog(FfmpegUtil.class);    /**     * 截取视频的某一帧为视频展示图     * @param videoRealPath 视频地址     * @param imageRealPath 图片保存地址     * @return     */    public  int getVideoSamllImage(){        int i =0;        //截图开始时间        int date=0;        //批处理文件地址        String shPath="/usr/ffmpeg.sh";        //视频文件        String videoRealPath = "/usr/test.mp4";        //截图的路径(输出路径)        String imageRealPath ="/usr/test.jpg";       //获取视频时间长度ffmpeg命令        String exePath="ffmpeg";        try {        //视频总时长        int time=getVideoTime(videoRealPath,exePath);            if(time>=30){                date=30; //大于30秒 取第30秒            }else{                date=time/2;//小于30秒 取总时长/2            }            //调用批处理命令             Process ps = null;             ps=Runtime.getRuntime().exec("sh "+shPath+" "+ videoRealPath +" "+date+" " + imageRealPath);             ps.waitFor();             i = ps.exitValue();        } catch (IOException | InterruptedException e) {            logger.error("视频"+videoRealPath+"截取图片失败");            e.printStackTrace();        }        return i;    }    /**     *获取视频总时长     * @param video_path 视频地址     * @param ffmpeg_path ffmpeg地址     * @return     */    public static  int getVideoTime(String video_path, String ffmpeg_path) {        List<String> commands = new java.util.ArrayList<String>();        int time=0;        commands.add(ffmpeg_path);        commands.add("-i");        commands.add(video_path);        try {            ProcessBuilder builder = new ProcessBuilder();            builder.command(commands);            final Process p = builder.start();            //从输入流中读取视频信息            BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));            StringBuffer sb = new StringBuffer();            String line = "";            while ((line = br.readLine()) != null) {                sb.append(line);            }            br.close();            //从视频信息中解析时长            String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";            Pattern pattern = Pattern.compile(regexDuration);            Matcher m = pattern.matcher(sb.toString());            if (m.find()) {                time = getTimelen(m.group(1));                System.out.println(video_path+",视频时长:"+time+", 开始时间:"+m.group(2)+",比特率:"+m.group(3)+"kb/s");                return time;            }        } catch (Exception e) {            e.printStackTrace();        }        return time;    }    /**     * 将时间转换为秒     * @param timelen     * @return     */    private static int getTimelen(String timelen){        int min=0;        String strs[] = timelen.split(":");        if (strs[0].compareTo("0") > 0) {            min+=Integer.valueOf(strs[0])*60*60;//秒        }        if(strs[1].compareTo("0")>0){            min+=Integer.valueOf(strs[1])*60;        }        if(strs[2].compareTo("0")>0){            min+=Math.round(Float.valueOf(strs[2]));        }        return min;    }}

linux下ffmpeg命令参考
linux下ffmpeg安装借鉴

第一次写博客,写的不好的地方还希望大家海涵~

1 0
原创粉丝点击