The magic of FFMPEG library

Posted by Tunde Michael | Thu Oct 13, 2016 08:25 PM | Viewed: 1098

I have always found myself with some video files and might just prefer having them in MP3 format for my audio devices and phones. FFMPEG is the de facto media processing library around at this moment. You can use it for both image and video processing

Although there is xuggler built around ffmpeg for java developers, it's a hell of work building for any OS and for some reason, I just seem to like working with ffmpeg directly either via the command line or Java applications as we shall see in this post. Let me warn you, ffmpeg is overkill for this simple job as the library is so robust that companies like YouTube, Vimeo use it for their media processing.

In this post I will show you my little secret for converting all those “stolen” videos to any format I want. It’s really simple

Step 1:

Download ffmpeg static build at the https://ffmpeg.zeranoe.com/builds/ and follow this step for installation (You can also go to the official download page, click on the Windows logo and then click on Windows build and it will direct you to this same website ffmpeg.zeranoe.com/builds/ )

  • Unzip and copy the folder to any location on your system (say C:\Program Files). You can also rename the folder to ffmpeg for shot. Now let set the system variable for ffmpeg.
  • Double-click to open the ffmpeg folder and double-click on the bin folder and copy the bin folder location from the window tab.
  • Click on start (on windows), right-click on Computer and click Properties
  • When the new window opens, click on Advanced system settings to popup another window.
  • On this new smaller window, click on Environment variable button to popup another window.
  • Look for the section with System variables, search for Path and click on it to highlight it then click on Edit.
  • At the end of this text in the Variable value textfield, add semi-colon and paste the bin folder path you copied earlier. Click Ok and Ok to close all the open windows.

Now that’s all you have to do to setup ffmpeg on your system forever.

For How to install ffempeg on windows with pictorial guides, check out this WikiHow Post

To test your installation, open a command prompt and type the code below and click enter

C:\>ffmpeg –version

If you get something similar to below, your installation is perfect and we’re ready to write java codes.

C:\>ffmpeg -version
ffmpeg version N-81154-gfb91143 Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 5.4.0 (GCC)
configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-dxva
2 --enable-libmfx --enable-nvenc --enable-avisynth --enable-bzlib --enable-libeb
ur128 --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enabl
e-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreety
pe --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable
-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libop
enjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsn
appy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame -
-enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --
enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-li
bxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-decklink --enable
-zlib
libavutil      55. 28.100 / 55. 28.100
libavcodec     57. 51.100 / 57. 51.100
libavformat    57. 44.100 / 57. 44.100
libavdevice    57.  0.102 / 57.  0.102
libavfilter     6. 49.100 /  6. 49.100
libswscale      4.  1.100 /  4.  1.100
libswresample   2.  1.100 /  2.  1.100
libpostproc    54.  0.100 / 54.  0.100

C:\>

Lets create s simple Java application for this say MP3Converter and here is the media conversion code.

// MP3Converter.java
package videoprocessor;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Tunde Michael
 * 
 */

public class MP3Converter {

    private static final Logger LOG = Logger.getLogger(MP3Converter.class.getName());
    
    public static void main(String[] args) {

        try {
            String line;
            String mp4File = "C:\\Users\\TundeMichael\\Videos\\School_Scam3.mp4";
            String mp3File = "C:\\Users\\TundeMichael\\Music\\myAdio.mp3";

            // ffmpeg -i input.mp4 output.avi as it's on www.ffmpeg.org
            String cmd = "ffmpeg -i " + mp4File + " " + mp3File;
            Process p = Runtime.getRuntime().exec(cmd);
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(p.getErrorStream()));
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            p.waitFor();
            System.out.println("Video converted successfully!");
            in.close();
        } catch (IOException | InterruptedException e) {
            LOG.log(Level.SEVERE, null, e);
        }

    }

}

 

Conversion can take a while depending on the size of your video file but ffmpeg gets the job done all the time.

This post is just scratching the surface of media processing. If it fuels your interest in video processing (transcoding, muxing and demuxing and all those jargons), start from the ffmpeg home page and also check out these tutorials:

  1. FFMPEG Tutorial For Beginners
  2. A Guide To Video And Audio Conversion Using FFmpeg

 


Share this post
facebook twitter googleplus linkedin





Comments (0)


Newsletter: Never miss a thing