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

Java调用ffmepg+mencoder视频格式转换(*)的更多相关文章

  1. asp.net实现调用ffmpeg实现视频格式的转换

    视频格式转换的函数 //视频转换 public void VideoConvertFlv(string FromName, string ExportName) { string ffmpeg = H ...

  2. C#使用FFmpeg 将视频格式转换成Gif图片示例

    根据EFmpeg封装的视频转换gif工具:https://my.oschina.net/tianma3798/blog/825317 一.本次使用参数说明 /* * 参数说明: * -i 源文件位置 ...

  3. Unity 利用FFmpeg实现录屏、直播推流、音频视频格式转换、剪裁等功能

    目录 一.FFmpeg简介. 二.FFmpeg常用参数及命令. 三.FFmpeg在Unity 3D中的使用. 1.FFmpeg 录屏. 2.FFmpeg 推流. 3.FFmpeg 其他功能简述. 一. ...

  4. Ubuntu18.04下使用Blender进行视频格式转换

    Ubuntu下可以使用Blender的Video Editing功能进行视频格式转换, 具体步骤: 打开Blender后, 在顶层菜单栏中, 将Choose Screen Layout修改为Video ...

  5. 视频格式转换.ZC资料

    1.20191013: ZC:这些都是 2015年做的尝试,之前貌似没有记录下来,现在 再次用到,把用到的记录下来: ZC: (1) 使用的视频格式转换工具是 "??/XiGua Yings ...

  6. iSkysoft iMedia Converter Deluxe Mac如何制作视频?视频格式转换工具制作动图的方法

    使用iSkysoft iMedia Converter Deluxe Mac如何制作视频?使用视频格式转换工具,你可以轻松进行动图或视频的制作,也可以把你喜欢的视频的某一段提取出来,制作成你自己风格的 ...

  7. java使用ffmpeg和mencoder做视频格式转换

    首发:个人博客,持续更新和纠错 主要使用技术:1)FFmpeg,用于主流格式之间的转换,例如AVI,MP4,FLV等.2)MEncoder,用于奇葩格式转主流格式,例如RMVB转AVI.这样我们可以把 ...

  8. ffmpeg视频格式转换(Java)

    命令: 高品质: ffmpeg -i E:\input\a.wmv -ab 128 -acodec libmp3lame -ac 1 -ar 22050 -r 29.97 -qscale 4 -y E ...

  9. java视频格式转换代码

    http://blog.163.com/zzf_fly/blog/static/20958915820127217443816/ package com.gkzx.online.action; imp ...

随机推荐

  1. Js中的window.parent ,window.top,window.self ,window.openner详解

    在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层frameset或者iframe),self是当前窗口, opener是用open方法 ...

  2. 014. asp.net实现记住密码的功能

    protected void Button1_Click(object sender, EventArgs e) { if (txtname.Text.Trim().Equals("mr&q ...

  3. shell之脚本片断

    16. 以下是平台信息 CentOS Linux release 7.1.1503 (Core) Linux mysql-dev1 3.10.0-229.el7.x86_64 #1 SMP Fri M ...

  4. GridView 控制分页页码间距

    来源:http://auv2009.blog.163.com/blog/static/68858712200992793853431/ 技巧1:在分页区中改变当前页码的样式或高亮显示页码 一个简单的办 ...

  5. 【转】ASP.NET服务器控件使用之MultiView和View

    MultiView 和 View 控件和制作出选项卡的效果,MultiView 控件是一组 View 控件的容器.使用它可定义一组 View 控件,其中每个 View 控件都包含子控件. 如果要切换视 ...

  6. PHP5.3以上版本没有libmysql.dll,以及由此带来的困扰

    有朋友下载了PHP5.3,PHP5.4版本想加载mysql支持的时候发现没有libmysql.dll文件,无法完成mysql配置,其实PHP5.3版本开始,使用mysqlnd库,不再使用libmysq ...

  7. 基于LBS的地理位置附近的搜索以及由近及远的排序

    Nosql学习之Redis资料(一) http://redis.io/download 目前基于LBS地理位置的搜索已经应用非常广了,的确是个很方便的东西. 我们做程序的就是要考虑如何通过这些功能,来 ...

  8. The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine

    问题描述: 修改一个工具功能为读取excel文件中的数据(xls) 本机(windows server 2003 32位) 调试运行正常,部署到服务器(windows server 2003 64位) ...

  9. du 命令,对文件和目录磁盘使用的空间的查看

    Linux du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的. 1.命令格式: du [选项][文件] 2.命令功能 ...

  10. trunc函数

    date 为必要参数,是输入的一个日期值 fmt 参数可忽略,是日期格式,用以指定的元素格式来截去输入的日期值.忽略它则由最近的日期截去 下面是该函数的使用情况: trunc(sysdate,'yyy ...