zoukankan      html  css  js  c++  java
  • java 实现视频转换通用工具类:视频截图-Ffmpeg(四)

    java 实现视频转换通用工具类:获取视频元数据信息(一)

      
      

    java 实现视频转换通用工具类:视频相互转换-Ffmpeg(三)

     

    1.ffmpeg 截图,自定义命令行

    1. /** 
    2.        *  ffmpeg 截图,自定义命令行 
    3.        * @param srcVideoPath 源文件 
    4.        * @param shellLine 自定义shell命令行 
    5.        * @param tarImagePath 目标文件 
    6.        * @return 
    7.        */  
    8.        public static boolean processFfmpegImage(String srcVideoPath,String shellLine,String tarImagePath) {     
    9.          if (!checkfile(srcVideoPath)) {    
    10.             logger.error("【" + srcVideoPath + "】  不存在 !");  
    11.                 return false;    
    12.             }    
    13.            List<String> commend = new java.util.ArrayList<String>();    
    14.   
    15.            commend.add(ffmpegPath);    
    16.   
    17.            commend.add("-i");    
    18.   
    19.            commend.add(srcVideoPath);    
    20.              
    21.            commend.add(shellLine);   
    22.   
    23.            commend.add(tarImagePath);    
    24.   
    25.            try {    
    26.                ProcessBuilder builder = new ProcessBuilder();   
    27.                builder.command(commend);   
    28.                Process process = builder.start();   
    29.                InputStream is = process.getErrorStream();   
    30.                InputStreamReader inputStreamReader = new InputStreamReader(is);   
    31.                BufferedReader inputBufferedReader = new BufferedReader(   
    32.                    inputStreamReader);   
    33.                String line = null;   
    34.                StringBuilder stringBuilder = new StringBuilder();   
    35.               while ((line = inputBufferedReader.readLine()) != null) {   
    36.                  stringBuilder.append(line);   
    37.                }   
    38.                inputBufferedReader.close();   
    39.                inputBufferedReader = null;   
    40.                inputStreamReader.close();   
    41.                inputStreamReader = null;   
    42.                is.close();   
    43.                is = null;   
    44.                if (!checkfile(tarImagePath)) {    
    45.                    logger.info(tarImagePath + " is not exit! processFfmpegImage 转换不成功 !");  
    46.                    return false;    
    47.                }    
    48.                return true;    
    49.            } catch (Exception e) {  
    50.                logger.error("【" + srcVideoPath + "】  processFfmpegImage 转换不成功 !");  
    51.                return false;   
    52.            }  
    53.        }    


    2.ffmpeg 截图,并指定图片的大小

    1. /** 
    2.       * ffmpeg 截图,并指定图片的大小 
    3.       * @param srcVideoPath 
    4.       * @param tarImagePath 截取后图片路径 
    5.       * @param width 截图的宽 
    6.       * @param hight 截图的高 
    7.       * @param offsetValue 表示相对于文件开始处的时间偏移值 可以是分秒 
    8.       * @param vframes 表示截图的桢数 
    9.       *  
    10.       * @return 
    11.       */  
    12.       public static boolean processFfmpegImage(String srcVideoPath,String tarImagePath,int width,int hight,float offsetValue,float vframes) {    
    13.          if (!checkfile(srcVideoPath)) {    
    14.               logger.error("【" + srcVideoPath + "】  不存在 !");  
    15.                return false;    
    16.            }    
    17.           List<String> commend = new java.util.ArrayList<String>();    
    18.   
    19.           commend.add(ffmpegPath);    
    20.   
    21.           commend.add("-i");    
    22.   
    23.           commend.add(srcVideoPath);    
    24.             
    25.           commend.add("-y");    
    26.   
    27.           commend.add("-f");   
    28.             
    29.           commend.add("image2");    
    30.             
    31.           commend.add("-ss");    
    32.             
    33.           commend.add(offsetValue+"");  //在视频的某个插入时间截图,例子为5秒后  
    34.             
    35.           commend.add("-vframes");    
    36.             
    37.           commend.add(vframes + "");  //截图的桢数  
    38.             
    39.           commend.add("-s");    
    40.             
    41.           commend.add(width + "x" +hight); //截图的的大小  
    42.   
    43.           commend.add(tarImagePath);    
    44.   
    45.           try {    
    46.               ProcessBuilder builder = new ProcessBuilder();   
    47.               builder.command(commend);   
    48.               Process process = builder.start();   
    49.               doWaitFor(process);    
    50.               process.destroy();   
    51.               if (!checkfile(tarImagePath)) {    
    52.                   logger.info(tarImagePath + " is not exit!  processFfmpegImage 转换不成功 !");    
    53.                   return false;    
    54.               }    
    55.               return true;    
    56.           } catch (Exception e) {  
    57.               logger.error("【" + srcVideoPath + "】 processFfmpegImage  转换不成功 !");  
    58.               return false;   
    59.           }  
    60.       }    


    更多0
  • 相关阅读:
    java中的包有那些 ???
    CIO必看:跨国集团采购部报表系统的建设经验分享
    价值5000元的web报表分享
    巧用FineReport搭建成本管控监测系统
    实战篇:如何建设企业的营销管理和分析平台
    大数据分析在石化企业的应用探讨
    不动产登记证书曝光 一个“改变”必须注意(图)
    VS2005工程的Device右边内容为空问题
    WIN7电脑文件莫名其妙被删除后的恢复
    关机充电如何实现短按pwrkey灭屏
  • 原文地址:https://www.cnblogs.com/zhwl/p/3645599.html
Copyright © 2011-2022 走看看