分享一个常用的ffmpeg工具类

论坛 期权论坛 脚本     
匿名网站用户   2020-12-20 10:39   55   0

最近一直在处理视频文件,然后经常会要截图、抽帧、对视频文件进行抽取音频,对wav转换成mp3等操作,使用的是ffmpeg的一个工具类,目前只有几个方面的,以后需要新的用途会更新此工具类,希望这个工具类可以帮助到一些需要的小伙伴,可以直接拿去用,代码如下:

public class FfmpegUtil {
 
 //ffmpeg安装目录
 public   static  String FFMPEG_PATH ="D:\\\\ffmpeg.exe";
// public   static  String FFMPEG_PATH =FfmpegUtil.class.getClassLoader().getResource("com/wal/utils/ffmpeg.exe").getFile();
 
 //设置图片大小
 private final static String IMG_SIZE = "1920x1080";

 /**
  * 视频截图方法(windows)
  */
 public static boolean ffmpegToImage(String videoPath,String imagePath,int timePoint){  
         List<String> commands = new java.util.ArrayList<String>();  
         FFMPEG_PATH= FFMPEG_PATH.replace("%20", " ");
         commands.add(FFMPEG_PATH);    
         commands.add("-ss");    
         commands.add(timePoint+"");//这个参数是设置截取视频多少秒时的画面    
         commands.add("-i");    
         commands.add(videoPath);    
         commands.add("-y");    
         commands.add("-f");    
         commands.add("image2");    
         commands.add("-t");    
         commands.add("0.001");    
         commands.add("-s");    
         commands.add(IMG_SIZE); //这个参数是设置截取图片的大小   
         commands.add(imagePath);    
         try {    
         ProcessBuilder builder = new ProcessBuilder();    
         builder.command(commands);    
         builder.start();    
         System.out.println("截取成功:"+imagePath);
         return true;    
         } catch (Exception e) {    
         e.printStackTrace();    
         return false;    
         }    
     } 
 
 /**
  * @Description 文件是否能被ffmpeg解析
  */
 public static int checkFileType(String fileName) {  
        String type = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length())  
                .toLowerCase();  
        if (type.equals("avi")) {  
            return 0;  
        }  else if (type.equals("mov")) {  
            return 0;  
        } else if (type.equals("mp4")) {  
            return 0;  
        }  else if (type.equals("flv")) {  
            return 0;  
        } 
        else if (type.equals("png")) {  
            return 1;  
        } else if (type.equals("jpg")) {  
            return 1;  
        } else if (type.equals("jpeg")) {  
            return 1;  
        }  
        return 9;  
    }


    /**
  * @Description 获取视频时长
  */
 static int getVideoTime(String video_path) {
  List<String> commands = new java.util.ArrayList<String>();
  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()) {
    int 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 0;
 }

 //格式:"00:00:10.68"
 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;
 }

 /**
  * 秒转化成 hh:mm:ss
  * @param duration
  * @return
  */
 public static String convertInt2Date(long duration){
  Calendar cal = Calendar.getInstance();
  cal.set(Calendar.HOUR_OF_DAY, 0);
  cal.set(Calendar.MINUTE, 0);
  cal.set(Calendar.SECOND, 0);
  cal.set(Calendar.MILLISECOND, 0);

  SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
  return formatter.format(cal.getTimeInMillis() + duration * 1000);
 }

 /**
  * 视频抽取音频文件
  * @param videoPath
  * @param type
  * @param audioPath
  * @return
  */
 public static boolean ffmpegToAudio(String videoPath,String type, String audioPath){
  List<String> commands = new java.util.ArrayList<String>();
  FFMPEG_PATH= FFMPEG_PATH.replace("%20", " ");
  commands.add(FFMPEG_PATH);
  commands.add("-i");
  commands.add(videoPath);
  commands.add("-f");
  commands.add(type);
  commands.add("-vn");
  commands.add("-y");
  commands.add("-acodec");
  if("wav".equals(type)){
   commands.add("pcm_s16le");
  }else if("mp3".equals(type)){
   commands.add("mp3");
  }
  commands.add("-ar");
  commands.add("16000");
  commands.add("-ac");
  commands.add("1");
  commands.add(audioPath);
  try {
   ProcessBuilder builder = new ProcessBuilder();
   builder.command(commands);
   Process p = builder.start();
   System.out.println("抽离成功:"+audioPath);

   // 1. start
   BufferedReader buf = null; // 保存ffmpeg的输出结果流
   String line = null;

   buf = new BufferedReader(new InputStreamReader(p.getInputStream()));

   StringBuffer sb = new StringBuffer();
   while ((line = buf.readLine()) != null) {
    System.out.println(line);
    sb.append(line);
    continue;
   }
   p.waitFor();// 这里线程阻塞,将等待外部转换进程运行成功运行结束后,才往下执行
   // 1. end
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }


 /**
  * wav 转 mp3
  * @param wavPath
  * @param mp3Path
  * @return
  */
 //wav转mp3命令:ffmpeg -i test.wav -f mp3 -acodec libmp3lame -y wav2mp3.mp3
 public static boolean ffmpegOfwavTomp3(String wavPath, String mp3Path){
  List<String> commands = new java.util.ArrayList<String>();
  FFMPEG_PATH= FFMPEG_PATH.replace("%20", " ");
  commands.add(FFMPEG_PATH);
  commands.add("-i");
  commands.add(wavPath);
  commands.add("-f");
  commands.add("mp3");
  commands.add("-acodec");
  commands.add("libmp3lame");
  commands.add("-y");
  commands.add(mp3Path);
  try {
   ProcessBuilder builder = new ProcessBuilder();
   builder.command(commands);
   Process p = builder.start();
   System.out.println("转换成功:"+mp3Path);

   // 1. start
   BufferedReader buf = null; // 保存ffmpeg的输出结果流
   String line = null;

   buf = new BufferedReader(new InputStreamReader(p.getInputStream()));

   StringBuffer sb = new StringBuffer();
   while ((line = buf.readLine()) != null) {
    System.out.println(line);
    sb.append(line);
    continue;
   }
   p.waitFor();// 这里线程阻塞,将等待外部转换进程运行成功运行结束后,才往下执行
   // 1. end
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }

 public static void main(String[] args) {
//        // 视频文件
//        String videoRealPath1 = "D:\\5.MOV";
//        // 截图的路径(输出路径)
//        String imageRealPath1 = "D:\\31.jpg";
//        String imageRealPath2 = "D:\\32.jpg";
//        if(checkFileType(videoRealPath1) ==0){
//          ffmpegToImage(videoRealPath1,imageRealPath1,10);
//          ffmpegToImage(videoRealPath1,imageRealPath2,11);
//        }
//  String videoRealPath = "D:\\5.MOV";
//  String audioRealPath = "D:\\HHH\\5.wav";
//  String audioType = "wav";
//  ffmpegToAudio(videoRealPath,audioType,audioRealPath);

  String wavPath = "D:\\HHH\\123.wav";
  String mp3Path = "D:\\HHH\\123.mp3";
  ffmpegOfwavTomp3(wavPath,mp3Path);
    } 
}

有不懂的地方可以给我留言,或者进群交流!

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP