The use of ffmpeg in video conversion

Keywords: Front-end Java

1. Create a Java project

In this project, three folders ffmpeg, input and output are created to store the converter ffmpeg.exe, the video files to be converted and the converted video files.
The format that ffmpeg can parse: (asx, asf, mpg, wmv, 3gp, mp4, mov, avi, flv, etc.).
The file format that ffmpeg can't parse (wmv9, rm, rmvb, etc.) can be converted to avi (the format that ffmpeg can parse) by other tools (mencoder).
Converter download
Extraction code: ts5g

2. Create class file

Create package com
Create the class file ConvertVideo.java under the package

package com;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
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() { 
        // First get the current project path, and then get the path of source file, target file and converter.
        File diretory = new File("");
        try {
            String currPath = diretory.getAbsolutePath();
            inputPath = "D:\\input\\1.wmv";
            outputPath = "D:\\output\\";
            ffmpegPath = "D:\\ffmpeg\\ffmpeg\\";
            System.out.println(currPath);
        }
        catch (Exception e) {
            System.out.println("getPath error");
        }
    }

    public static boolean process() {
        int type = checkContentType();
        boolean status = false;
            System.out.println("Direct conversion mp4 format");
            status = processMp4(inputPath);// Direct conversion to mp4 format
        return status;
    }

    private static int checkContentType() {
        String type = inputPath.substring(inputPath.lastIndexOf(".") + 1, inputPath.length())
                .toLowerCase();
        // The format that ffmpeg can parse: (asx, asf, mpg, wmv, 3gp, mp4, mov, avi, flv, etc.)
        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;
        }
        // For the file format that ffmpeg can't parse (wmv9, rm, rmvb, etc.)
        // You can first use other tools (mencoder) to convert to avi(ffmpeg can parse) format.
        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;
    }

    // For the file formats (wmv9, rm, rmvb, etc.) that cannot be parsed by ffmpeg, you can first use other tools (mencoder) to convert them to avi (the format that ffmpeg can parse).
    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;
        }
    }

    // The format that ffmpeg can parse: (asx, asf, mpg, wmv, 3gp, mp4, mov, avi, flv, etc.)
    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 {

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

            // Option 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");		//Destination file name
    try {

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

        // Option 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();
        } 
    }
}

3. Successful transformation

Posted by locell on Fri, 01 Nov 2019 13:35:37 -0700