• 兼容Linux和Windows下获取视频截图


    package com.hncy.common;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.BufferedReader;
    
    /**
    * @author Bashan
    * FFMPEG homepage http://ffmpeg.org/about.html
    * By Google Get first and last thumb of a video using Java and FFMpeg
    * From http://www.codereye.com/2010/05/get-first-and-last-thumb-of-video-using.html
    */
    
    public class VideoThumbTaker
    {
    protected String ffmpegApp;
    
    public VideoThumbTaker(String ffmpegApp)
    {
    this.ffmpegApp = ffmpegApp;
    }
    
    @SuppressWarnings("unused")
    /****
    * 获取指定时间内的图片
    * @param videoFilename:视频路径
    * @param thumbFilename:图片保存路径
    * @param 图片长
    * @param height:图片宽
    * @param hour:指定时
    * @param min:指定分
    * @param sec:指定秒
    * @throws IOException
    * @throws InterruptedException
    */
    public void getThumbWindows(String videoFilename, String thumbFilename, int width,
    int height, int hour, int min, float sec) throws IOException,
    InterruptedException
    {
    ProcessBuilder processBuilder = new ProcessBuilder(ffmpegApp, "-y",
    "-i", videoFilename, "-vframes", "1", "-ss", hour + ":" + min
    + ":" + sec, "-f", "mjpeg", "-s", width + "*" + height,
    "-an", thumbFilename);
    
    Process process = processBuilder.start();
    
    InputStream stderr = process.getErrorStream();
    InputStreamReader isr = new InputStreamReader(stderr);
    BufferedReader br = new BufferedReader(isr);
    String line;
    while ((line = br.readLine()) != null)
    ;
    process.waitFor();
    
    if(br != null)
    br.close();
    if(isr != null)
    isr.close();
    if(stderr != null)
    stderr.close();
    }
    
    public static boolean getThumbLinux(String inFile, String outFile,String startTime) {
    String command = "ffmpeg -i " + inFile
    + " -y -f image2 -ss "+startTime+" -t 00:00:01 -s 480x450 "
    + outFile;
    try {
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(command);
    InputStream stderr = proc.getErrorStream();
    InputStreamReader isr = new InputStreamReader(stderr);
    BufferedReader br = new BufferedReader(isr);
    String line = null;
    while ((line = br.readLine()) != null)
    System.out.println(line);
    } catch (Throwable t) {
    t.printStackTrace();
    return false;
    }
    return true;
    }

    这儿有两个方法,一个是获取Linux一个是获取Windows的

    ****************************************************************

    当我的视频上传结束后

    //结束前保存改视频的截图
    if(fileType.equals("3gp")&&fileType.equals("mp4")){
    
    //判断是否为视频
    if(osType.equals("windows")){
    //这个地方获取到我ffmpeg.exe所在的位置
    VideoThumbTaker videoThumbTaker = new VideoThumbTaker("E:\ffmpeg\ffmpeg.exe");
    try
    {
    String videoFilePath = "E:\uploadfiles\"+mac+"\"+userId+"\"+DateFormatUtils.format(System.currentTimeMillis(),"yyyyMMddHHmmsss")
    + RandomStringUtils.randomAlphabetic(5);
    String thumbFilename = videoFilePath+".jpg";
    videoThumbTaker.getThumbWindows(videoFilePath, thumbFilename,150, 100, 0, 0, 1);
    System.out.println("over");
    } catch (Exception e)
    {
    e.printStackTrace();
    }
    }else if(osType.equals("linux")){
    
    String inFile =baseFileFolder +"/"+ mac
    + "/"
    + userId
    + "/"
    + DateFormatUtils.format(
    System.currentTimeMillis(),
    "yyyyMMddHHmmsss")
    + RandomStringUtils.randomAlphabetic(5);
    
    String outFile =inFile+".jpg";
    boolean transfer = VideoThumbTaker.getThumbLinux(inFile, outFile,"0");
    System.out.print(transfer);
    }

    获取视频的截图,上传到相应文件夹,当然,我在之前就写好了一个获取系统信息的函数,用来判断现在所处的环境

  • 相关阅读:
    (转)Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践
    (转)Spring Boot 2 (四):使用 Docker 部署 Spring Boot
    (转)Spring Boot 2 (三):Spring Boot 开源软件都有哪些?
    (转)Spring Boot 2 (二):Spring Boot 2 尝鲜-动态 Banner
    (转)Spring Boot 2(一):【重磅】Spring Boot 2.0权威发布
    环境变量和用户变量有什么区别?
    记录一下,关于错误提示:could not find a part of path “X:”的解决办法
    日积月累:ScrollView嵌套ListView只显示一行
    android PopupWindow实现从底部弹出或滑出选择菜单或窗口
    WCF 配置文件中的MaxStringContentLength & MaxReceivedMessageSize
  • 原文地址:https://www.cnblogs.com/handbang/p/6706083.html
Copyright © 2020-2023  润新知