java wmv格式转换mp4

1,044 阅读3分钟

背景

由于对方拍摄视频源格式为wmv格式,客户要求要能在线播放。两种解决方案:1.用户自己手动转换;2.后台代码转换。最终还是妥协了,代码转换吧。

1.借助FFmpeg工具进行转换

** 说明:本方法适用于windows平台,Linux平台没试过;**

(1)下载FFmpeg工具

ffmpeg.org/download.ht…

(2)代码实现

public class ConvertVideo {
   private static String inputPath = "";

   private static String outputPath = "";

   private static String ffmpegPath = "";

   public static void main(String args[]) throws IOException {

       getPath();

       if (!checkfile(inputPath)) {
           System.out.println(inputPath + " is not file");
           return;
       }
       if (process()) {
           System.out.println("ok");
       }
   }

   public static void getPath(){
       // 先获取当前项目路径,在获得源文件、目标文件、转换器的路径
       File diretory = new File("");
       try {
           String currPath = diretory.getAbsolutePath();
           //视频的地址
           inputPath = "F:\\video\\dca08834-3692-4ade-8d35-d2349f794584.wmv";
           //视频转完格式存放地址
           outputPath = "F:\\video\\";
           ffmpegPath = "F:\\javaweb\\ffepegPath\\ffmpeg.exe";

           //转换视频的插件
//            ffmpegPath = "F:\\javaweb\\ffmpeg-4.3.1-2020-10-01-full_build\\ffmpeg-4.3.1-2020-10-01-full_build\\bin\\";
//            ffmpegPath = "F:\\javaweb\\ffmpeg-4.3.1-2020-10-01-full_build\\ffmpeg-4.3.1-2020-10-01-full_build\\bin\\";
           System.out.println(currPath);
       }
       catch (Exception e) {
           System.out.println("getPath出错");
       }
   }

   public static boolean process() {
       int type = checkContentType();
       boolean status = false;
       System.out.println("直接转成mp4格式");
       System.out.println("inputPath"+inputPath);
       System.out.println("outPath"+outputPath);
       status = processFlv(inputPath);

       // 直接转成mp4格式
//        status=processMp4(inputPath);
       System.out.println("status"+status);
       return status;
   }


   private static int checkContentType() {
       String type = inputPath.substring(inputPath.lastIndexOf(".") + 1, inputPath.length())
               .toLowerCase();
       // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
       if (type.equals("avi")) {
           return 0;
       } else if (type.equals("mpg")) {
           return 0;
       } else if (type.equals("wmv")) {
           return 0;
       } else if (type.equals("3gp")) {
           return 0;
       } else if (type.equals("mov")) {
           return 0;
       } else if (type.equals("mp4")) {
           return 0;
       } else if (type.equals("asf")) {
           return 0;
       } else if (type.equals("asx")) {
           return 0;
       } else if (type.equals("flv")) {
           return 0;
       }
       // 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),
       // 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
       else if (type.equals("wmv9")) {
           return 1;
       } else if (type.equals("rm")) {
           return 1;
       } else if (type.equals("rmvb")) {
           return 1;
       }
       return 9;
   }

   private static boolean checkfile(String path) {
       File file = new File(path);
       if (!file.isFile()) {
           return false;
       }
       return true;
   }

   // 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
   private static String processAVI(int type) {
       List<String> commend = new ArrayList<String>();
       commend.add(ffmpegPath + "mencoder");
       commend.add(inputPath);
       commend.add("-oac");
       commend.add("lavc");
       commend.add("-lavcopts");
       commend.add("acodec=mp3:abitrate=64");
       commend.add("-ovc");
       commend.add("xvid");
       commend.add("-xvidencopts");
       commend.add("bitrate=600");
       commend.add("-of");
       commend.add("mp4");
       commend.add("-o");
       commend.add(outputPath + "a.AVI");
       try {
           ProcessBuilder builder = new ProcessBuilder();
           Process process = builder.command(commend).redirectErrorStream(true).start();
           new PrintStream(process.getInputStream());
           new PrintStream(process.getErrorStream());
           process.waitFor();
           return outputPath + "a.AVI";
       } catch (Exception e) {
           e.printStackTrace();
           return null;
       }
   }

   // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
   private static boolean processFlv(String oldfilepath) {

       if (checkfile(outputPath)) {
           System.out.println(oldfilepath + " is  file");
//            return false;
           File file=new File(outputPath);
           file.delete();
       }
       List<String> command = new ArrayList<String>();
       command.add(ffmpegPath);
       command.add("-i");
       command.add(oldfilepath);
       command.add("-ab");
       command.add("56");
       command.add("-ar");
       command.add("22050");
       command.add("-qscale");
       command.add("8");
       command.add("-r");
       command.add("15");
       command.add("-s");
       command.add("600x500");
       command.add(outputPath + "b7d07219-6150-4eb1-90d1-29b3c5120c0b.flv");
       try {

           // 方案1
//            Process videoProcess = Runtime.getRuntime().exec(ffmpegPath + "ffmpeg -i " + oldfilepath 
//                    + " -ab 56 -ar 22050 -qscale 8 -r 15 -s 600x500 "
//                    + outputPath + "a.flv");

           // 方案2
           Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();

           new PrintStream(videoProcess.getErrorStream()).start();

           new PrintStream(videoProcess.getInputStream()).start();

           videoProcess.waitFor();
           int i = inputPath.indexOf(".");
          System.out.println("filePath"+ inputPath.substring(0,i));
           String filePath=inputPath.substring(0,i);
           processMp4(outputPath+ "b7d07219-6150-4eb1-90d1-29b3c5120c0b.flv");
           return true;
       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }
   private static boolean processMp4(String oldfilepath) {

       if (!checkfile(inputPath)) {
           System.out.println(oldfilepath + " is not file");
           return false;
       }
       List<String> command = new ArrayList<String>();
       command.add(ffmpegPath);
//        command.add("D:\\MediaCoder\\codecs64\\ffmpeg.exe");
//        command.add("F:\\javaweb\\ffepegPath\\ffmpeg.exe");

       command.add("-i");
       command.add(oldfilepath);
       command.add("-c:v");
       command.add("libx264");
       command.add("-mbd");
       command.add("0");
       command.add("-c:a");
       command.add("aac");
       command.add("-strict");
       command.add("-2");
       command.add("-pix_fmt");
       command.add("yuv420p");
       command.add("-movflags");
       command.add("faststart");
       command.add(outputPath + "a.mp4");
       try {

           // 方案1
           //        Process videoProcess = Runtime.getRuntime().exec(ffmpegPath + "ffmpeg -i " + oldfilepath
           //                + " -ab 56 -ar 22050 -qscale 8 -r 15 -s 600x500 "
           //                + outputPath + "a.flv");

           // 方案2
           Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();

           new PrintStream(videoProcess.getErrorStream()).start();

           new PrintStream(videoProcess.getInputStream()).start();

           videoProcess.waitFor();

           return true;
       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }
}
class PrintStream extends Thread
{
   java.io.InputStream __is = null;
   public PrintStream(java.io.InputStream is)
   {
       __is = is;
   }

   public void run()
   {
       try
       {
           while(this != null)
           {
               int _ch = __is.read();
               if(_ch != -1)
                   System.out.print((char)_ch);
               else break;
           }
       }
       catch (Exception e)
       {
           e.printStackTrace();
       }
   }
}

注意:wmv转mp4不可以直接转换,必须要先转成flv格式后才能转换成功,在这里小编浪费了一点时间

说明: **inputPath、outputPath 、 ffmpegPath **分别为:待转换的文件路径、转换后的文件路径、 FFmpeg插件路径。完成以上操作静等转换结果即可。。。。。

end:本篇博客仅作为记录知识使用,感谢其他博客提供思路