Java中调用ffmpeg转换视频截图和音频转换

最近在开发过程中移动端上传上来的视频和音频格式不统一,又无法互相播放,最后决定将格式统一转换为mp4和mp3格式,在咨询别人建议使用ffmpeg这个开源软件进行视频和音频格式的转换比较方便,视频转换时进行截图

以下附上代码:

package com.yuan;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
  
public class FormatSource {
  
    private String PATH;
    private String filerealname; // 文件名 (不包括扩展名)
    private String filename; // 文件名
    private String flvfolder = "F:\\tomcat7.0\\file\\"; // 视频目录
    private String ffmpegpath = "F:\\ffmpeg-20170223-dcd3418-win64-static\\bin\\ffmpeg.exe"; // ffmpeg.exe目录
  
    public FormatSource() {
    }
  
    public FormatSource(String path) {
        PATH = path;
    }
  
    public String getPATH() {
        return PATH;
    }
  
    public void setPATH(String path) {
        PATH = path;
    }
  
    public boolean beginFormat() {
        File fi = new File(PATH);
        filename = fi.getName();
        filerealname = filename.substring(0, filename.lastIndexOf("."))
                .toLowerCase();
        if (!checkfile(PATH)) {
            System.out.println(PATH + "文件不存在" + " ");
            return false;
        }
        if (format()) {
            if (cutImg(PATH)) {
                System.out.println("截图成功了 ");
            } else {
                System.out.println("截图不成功了 ");
            }
            PATH = null;
            return true;
        } else {
            PATH = null;
            return false;
        }
    }
  
    public boolean formatSound(String soundPath){
    	 List commend = new java.util.ArrayList();
         commend.add(ffmpegpath);
         commend.add("-i");
         commend.add(soundPath);
         Date date=new Date();
         DateFormat format=new SimpleDateFormat("yyyyMMddHH");
         String time=format.format(date);
         commend.add(flvfolder+time+"\\"+ filerealname + ".mp3");
         try {
             ProcessBuilder builder = new ProcessBuilder(commend);
             builder.command(commend);
             Process p = builder.start();
             doWaitFor(p);
             p.destroy();
             return true;
         } catch (Exception e) {
             e.printStackTrace();
             return false;
         }
    }
    
    
    public boolean cutImg(String videoRealPath) {
        List commend = new java.util.ArrayList();
        commend.add(ffmpegpath);
        commend.add("-ss"); 
        commend.add("0.01");  
        commend.add("-i");  
        commend.add(videoRealPath);  
        commend.add("-f");  
        commend.add("image2");  
        commend.add("-y");  
        Date date=new Date();
        DateFormat format=new SimpleDateFormat("yyyyMMddHH");
        String time=format.format(date);
        commend.add(flvfolder+time+"\\" + filerealname + ".jpg");
        try {
            ProcessBuilder builder = new ProcessBuilder(commend);
            builder.command(commend);
            Process p = builder.start();
            System.out.println(commend.toString());
            doWaitFor(p);
            p.destroy();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
  
    private boolean format() {
        int type = checkfileType();
        boolean status = false;
        if (type == 0) {
            status = formatVideo(PATH);
        } else if(type == 1){
        	status = formatSound(PATH);
        }
        return status;
    }
  
    private int checkfileType() {
        String type = PATH.substring(PATH.lastIndexOf(".") + 1, PATH.length())
                .toLowerCase();
        if (type.equals("3gp")) {
            return 0;
        } else if (type.equals("avi")) {
            return 0;
        } else if (type.equals("mov")) {
            return 0;
        } else if (type.equals("amr")) {
            return 1;
        } else if (type.equals("wma")) {
            return 1;
        } else if (type.equals("mp4")) {
            return 0;
        } else if (type.equals("wav")) {
            return 1;
        } else if (type.equals("asx")) {
            return 0;
        } else if (type.equals("flv")) {
            return 0;
        } else if(type.equals("amr")){
        	return 1;
        } 
        else if (type.equals("wmv9")) {
            return 1;
        } else if (type.equals("rm")) {
            return 1;
        } else if (type.equals("rmvb")) {
            return 1;
        }
        return 3;
    }
  
    private boolean checkfile(String path) {
        File file = new File(path);
        if (!file.isFile()) {
            return false;
        } else {
            return true;
        }
    }
  
  
    private boolean formatVideo(String videopath) {
  
        if (!checkfile(PATH)) {
            System.out.println(videopath + " is not file");
            return false;
        }
  
        List commend = new java.util.ArrayList();
        commend.add(ffmpegpath);
        commend.add("-i");
        commend.add(videopath);
        Date date=new Date();
        DateFormat format=new SimpleDateFormat("yyyyMMddHH");
        String time=format.format(date);
        commend.add(flvfolder+time+"\\"+ filerealname + ".mp4");
        System.out.println(commend.toString());
        try {
            ProcessBuilder builder = new ProcessBuilder(commend);
            builder.command(commend);
            Process p = builder.start();
            doWaitFor(p);
            p.destroy();     
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
  
    public int doWaitFor(Process p) {
        InputStream in = null;
        InputStream err = null;
        int exitValue = -1; 
        try {
            System.out.println("comeing");
            in = p.getInputStream();
            err = p.getErrorStream();
            boolean finished = false; 
  
            while (!finished) {
                try {
                    while (in.available() > 0) {
                        Character c = new Character((char) in.read());
                        System.out.print(c);
                    }
                    while (err.available() > 0) {
                        Character c = new Character((char) err.read());
                        System.out.print(c);
                    }
  
                    exitValue = p.exitValue();
                    finished = true;
  
                } catch (IllegalThreadStateException e) {
                    Thread.currentThread().sleep(500);
                }
            }
        } catch (Exception e) {
            System.err.println("doWaitFor();: unexpected exception - "
                    + e.getMessage());
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
  
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
            if (err != null) {
                try {
                    err.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        return exitValue;
    }
  
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值