ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java调用ffmepg+mencoder视频格式转换(*)

2019-08-04 13:38:19  阅读:319  来源: 互联网

标签:return String mencoder commend add 视频格式 ffmepg type inputFile


原文链接:http://www.cnblogs.com/coprince/p/3461664.html

PS: 建议大家在官网下载最新的资源

 

其他格式转FLV格式,可以用Java调用ffmpeg和memcoder实现

ffmepg:

D:\ffmpeg\bin\ffmpeg.exe -i E:\1.mp4 -ab 64 -acodec mp3 -ac 2 -ar 22050 -b 230 -r 24 -y E:\111.flv

 

Mencoder:

D:\WisMencoder\mencoder.exe E:\1.rmvb -oac mp3lame -lameopts preset=64 -ovc xvid -xvidencopts bitrate=600 -of avi -o E:\1.avi

 

ps:网易视频 比较清晰 50min -》170mb 的FLV格式

 

/**
 * 功能:将任意格式的视频转化为flv格式,有利于在线视频播放
 *  ps:   ffmpeg 能解析的格式:asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等
 *         mencoder 解析剩下的格式:wmv9,rm,rmvb  
 *  time:2013.12.11
 */
package softflag.core.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import softflag.index.IndexService;

public class VideoConverter {
    public synchronized void convert(String inputFile, String outputFile) {
        process(inputFile, outputFile);
    }

    private static String getAviFilePath(String filePath) {
        String path = filePath.substring(0, filePath.lastIndexOf(".")) + ".avi";
        return path;
    }

    /**
     * 转换过程 :先检查文件类型,在决定调用 processFlv还是processAVI
     *
     * @param inputFile
     * @param outputFile
     * @return
     */
    private void process(String inputFile, String outputFile) {
        int type = checkContentType(inputFile);
        if (type == 0) {
            processFLV(inputFile, outputFile);// 直接将文件转为flv文件
        } else if (type == 1) {
            processAVI(type, inputFile,outputFile);
        }
    }

    /**
     * 检查视频类型
     *
     * @param inputFile
     * @return ffmpeg 能解析返回0,不能解析返回1
     */
    private static int checkContentType(String inputFile) {
        String type = inputFile.substring(inputFile.lastIndexOf(".") + 1,
                inputFile.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;
    }

    /**
     * ffmepg: 能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
     *
     * @param inputFile
     * @param outputFile
     * @return
     */
    private void processFLV(String inputFile, String outputFile) {
        try {
            try {
                List<String> commend = new java.util.ArrayList<String>();
                // 低精度
                commend.add(IndexService.getConfigValue("check_multi_ffmpegPath"));
                commend.add("-i");
                commend.add(inputFile);
                commend.add("-ab");
                commend.add("64");
                commend.add("-acodec");
                commend.add("mp3");
                commend.add("-ac");
                commend.add("2");
                commend.add("-ar");
                commend.add("22050");
                commend.add("-b");
                commend.add("230");
                commend.add("-r");
                commend.add("24");
                commend.add("-y");
                commend.add(outputFile);
                StringBuffer test = new StringBuffer();
                for (int i = 0; i < commend.size(); i++)
                    test.append(commend.get(i) + " ");
//                            System.out.println(test);
                try {
                    ProcessBuilder builder = new ProcessBuilder();
                    builder.command(commend);
                    Process p = builder.start();
                    doWaitFor(p);
                    p.destroy();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * Mencoder: 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),
     * 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
     *
     * @param type
     * @param inputFile
     * @return
     */
    private void processAVI(int type, String inputFile,String outputFile) {
        String aviPath = getAviFilePath(inputFile);
        String outputFilePath = outputFile;
        File file = new File(aviPath);
        if (file.exists())
            file.delete();
        List<String> commend = new java.util.ArrayList<String>();
        commend.add(IndexService.getConfigValue("check_multi_mencoderPath"));
        commend.add(inputFile);
        commend.add("-oac");
        commend.add("mp3lame");
        commend.add("-lameopts");
        commend.add("preset=64");
        commend.add("-ovc");
        commend.add("xvid");
        commend.add("-xvidencopts");
        commend.add("bitrate=600");
        commend.add("-of");
        commend.add("avi");
        commend.add("-o");
        commend.add(aviPath);
        StringBuffer test = new StringBuffer();
        for (int i = 0; i < commend.size(); i++)
            test.append(commend.get(i) + " ");
//            System.out.println(test);
        try {
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commend);
            Process p = builder.start();
            /**
             * 清空Mencoder进程 的输出流和错误流 因为有些本机平台仅针对标准输入和输出流提供有限的缓冲区大小,
             * 如果读写子进程的输出流或输入流迅速出现失败,则可能导致子进程阻塞,甚至产生死锁。
             */
            final InputStream is1 = p.getInputStream();
            final InputStream is2 = p.getErrorStream();
            new Thread() {
                public void run() {
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(is1));
                    try {
                        String lineB = null;
                        while ((lineB = br.readLine()) != null) {
                            if (lineB != null)
                                System.out.println(lineB);
                        }
                        is1.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
            new Thread() {
                public void run() {
                    BufferedReader br2 = new BufferedReader(
                            new InputStreamReader(is2));
                    try {
                        String lineC = null;
                        while ((lineC = br2.readLine()) != null) {
                            if (lineC != null)
                                System.out.println(lineC);
                        }
                        br2.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
            doWaitFor(p);
            p.destroy();
            processFLV(aviPath,outputFilePath);
        } catch (Exception e) {
            System.err.println(e);
        }
    }
    
    public int doWaitFor(Process p) {
        InputStream in = null;
        InputStream err = null;
        int exitValue = -1; // returned to caller when p is finished
        try {
//            System.out.println("comeing");
            in = p.getInputStream();
            err = p.getErrorStream();
            boolean finished = false; // Set to true when p is finished

            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;
    }
}

 

转载于:https://www.cnblogs.com/coprince/p/3461664.html

标签:return,String,mencoder,commend,add,视频格式,ffmepg,type,inputFile
来源: https://blog.csdn.net/weixin_30616969/article/details/98464066

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有