如何用java代码调用ffmpeg进行视频转码

一:下载安装包

https://download.csdn.net/download/lushizhuo9655/11199962

二:首先安装一个虚拟机(我是最小版本安装,里面什么也没有)

二:建立几个文件夹,放视频和安装包(这样就可以直接调用我接下来的java代码实现视频转码)

三:安装jdk(配置环境)

四:安装gcc (配置环境)  yum install gcc

五:安装bzip2用于解压                 yum -y install bzip2

六:环境已经搭建成功,接下来解压压缩包

七:安装yasm并配置

tar -zxvf yasm-1.3.0.tar.gz

 cd yasm-1.3.0

./configure

make

make install

八:安装nasm

tar jxvf nasm-2.13.tar.bz2

 cd nasm-2.13

./configure

make

make install

九:安装libx264

tar jxvf last_x264.tar.bz2 

 cd x264-snapshot-20190522-2245/

./configure --enable-shared

make

make install

十:安装ffmpeg

tar jxvf ffmpeg-4.1.3.tar.bz2 

 cd ffmpeg-4.1.3

./configure --enable-gpl --enable-libx264

make

make install

编辑id.so.conf文件

vi /etc/ld.so.conf
添加  /usr/local/lib

执行 ldconfig

linux上面环境安装成功,接下来通过java代码实现视频转码(稍微修改下可以在windows和linux环境下进行视频转码)

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//转成mp4格式
public class ShiPingZhuanMa {
    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 = "D:/ShiPingZhuanMa/1.wmv";          
            inputPath = "/root/shipingzhuanma/1.avi";
            //视频转完格式存放地址   
            //outputPath = "D:/ShiPingZhuanMa/";
               outputPath = "/root/shipingzhuanma/";
            //转换视频的插件 
               // ffmpegPath = "D:/ShiPingZhuanMa/ffmpeg-20171225-be2da4c-win64-static/bin/";
              ffmpegPath = "/root/guoyebing/ffmpeg-4.1.3/";    
            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格式");
            status = processMp4(inputPath);// 直接转成mp4格式
        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(inputPath)) {
            System.out.println(oldfilepath + " is not file");
            return false;
        }
        List<String> command = new ArrayList<String>();
        command.add(ffmpegPath + "ffmpeg");
        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 + "a.flv");
        try {
 
            // 方案1
            /*Process videoProcess = Runtime.getRuntime().exec(ffmpegPath + "ffmpeg -i " + oldfilepath 
                    + " -ab 56 -ar 22050 -qscale 8 -r 15 -s 600x500 "
                    + outputPath + "2.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;
        }
    }
    
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 + "ffmpeg");
        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");
        command.add(outputPath + "2.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");   哈哈
            + outputPath + "a.mp4");  
                 
            
            
            
    
            // 方案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();
        } 
    }

}
将代码打包成jar包,运行在服务器上面即可。

 

  • 5
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

guoyebing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值