FFMPEG

Calling FFMPEG from Java

FFmpeg is the leading multimedia framework, able to decodeencodetranscodemuxdemuxstreamfilter and play pretty much anything that humans and machines have created. It supports the most obscure ancient formats up to the cutting edge. No matter if they were designed by some standards committee, the community or a corporation. It is also highly portable: FFmpeg compiles, runs, and passes our testing infrastructure FATE across Linux, Mac OS X, Microsoft Windows, the BSDs, Solaris, etc. under a wide variety of build environments, machine architectures, and configurations.

Way 1:
package ffmpeg;

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

import sun.rmi.runtime.Log;

public class myFFMPEG {
    //ffmpeg path :   C:/Users/arsingh/Desktop/try/ffmpeg
    //input img path: C:/Users/arsingh/Desktop/AnupData/sampleImages
    //input audio path :   C:/Users/arsingh/Desktop/AnupData/AAJ.mp3
    public static void main(String[] args) throws IOException {
System.out.println(“Creation Started ….k”);
//ffmpeg -r 1/5 -i img%03d.png -c:v libx264 -vf “fps=25,format=yuv420p” out.mp4
//ffmpeg -loop 1 -i img.jpg -i audio.wav -c:v libx264 -c:a aac -strict experimental -b:a 192k -shortest out.mp4
    //String cmd[] = {“C://Program Files (x86)//ffmpeg//ffmpeg”,”-i”,”C://Users//arsingh//Desktop//AnupData//sampleImages//img%03d.png”,”C://Users//arsingh//Desktop//AnupData//sampleImages//out.mp4″};
String cmd[] = {“C://Program Files (x86)//ffmpeg//ffmpeg”,”-r 1/5″,”-i”,”C://Users//arsingh//Desktop//AnupData//sampleImages//img%03d.jpg”,”-i”,”C://Users//arsingh//Desktop//AnupData//AAJ.mp3″,”C://Users//arsingh//Desktop//AnupData//sampleImages//out.mp4″};

//Runtime.getRuntime().exec(cmd);
convertImg_to_vid(cmd);
        System.out.println(“Video Created”);
   }//end of psvm

    static void convertImg_to_vid(String cmd[])
    {
        Process chperm;
        try {
            chperm=Runtime.getRuntime().exec(“su”);
              DataOutputStream os =
                  new DataOutputStream(chperm.getOutputStream());

                 // os.writeBytes(“ffmpeg -f image2 -i img%d.jpg /tmp/a.mpg\n”);
                  os.writeBytes(cmd.toString());
                  os.flush();

                  chperm.waitFor();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

Way 2:
package ffmpeg;

import java.io.IOException;

public class ffMpegStich {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        System.out.println(“Creation Started ….k”);
        //String cmd[] = {“C://Program Files (x86)//ffmpeg//ffmpeg”,”-r 1/5″,”-i”,”C://Users//arsingh//Desktop//AnupData//sampleImages//img%03d.jpg”,”-i”,”C://Users//arsingh//Desktop//AnupData//AAJ.mp3″,”C://Users//arsingh//Desktop//AnupData//sampleImages//out.mp4″};
        String ffMpegPath=”C://Program Files (x86)//ffmpeg//ffmpeg”;
        String imagePath=”C://Users//arsingh//Desktop//AnupData//sampleImages//img%03d.jpg”;
        String audioPath=”C://Users//arsingh//Desktop//AnupData//AAJ.mp3″;
        String videoPath=”C://Users//arsingh//Desktop//AnupData//sampleImages//out.mp4″;
        String cmd[] = {ffMpegPath,”-r 1/5″,”-i”,imagePath,”-i”,audioPath,videoPath};

        Runtime.getRuntime().exec(cmd);
        System.out.println(“Video Created”);

    }

}

Author: Arun Singh

Learning is an Habit.