Java Swing AWT FFMPEG Example Project to Build Video & Image Converter GUI Desktop App Full Project For Beginners

 

 

swingffmpeg.java

 

 

/*
 * Use FFMPEG to procude a video file:
 * ffmpeg -re -r 10 -s 400x480 -f rawvideo -pix_fmt bgr24 -i /tmp/tempfile.bgr24 test.flv
 */
package screenstudio.sources;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author patrick
 */
public class OverlayPipe implements Runnable {

    // Make a file pipe...

    private File mPipe = null;
    private JPanel mPanel = null;
    private Thread mThread = null;
    private long mFPS = 10;
    private boolean stopMe = false;
    private boolean mIsRunning = false;

    public OverlayPipe(JPanel panel, long fps) {
        mPanel = panel;
        mFPS = fps;
    }

    public void start() {
        mThread = new Thread(this);
        mThread.start();
    }

    @Override
    public void run() {
        mIsRunning = true;
        stopMe = false;
        java.io.OutputStream output = null;
        try {
            mPipe = createPipeFile();
            // Pipe created. so we need to paint
            // the panel in the fifo each x ms seconds..
            long nextTimeStamp;
            long delay;
            // Use a BGR 24 bits images as ffmpeg will read  -pix_format BGR24
            BufferedImage img = new BufferedImage(mPanel.getWidth(),mPanel.getHeight(),BufferedImage.TYPE_3BYTE_BGR);
            Graphics2D graphics = img.createGraphics();
            output = new FileOutputStream(mPipe);
            while (!stopMe){
                nextTimeStamp = System.currentTimeMillis() + (1000/mFPS);
                mPanel.paint(graphics);
                byte[] imageBytes = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
                output.write(imageBytes);
                output.flush();
                //Try to sleep just what is needed to keep a constant fps
                delay = nextTimeStamp - System.currentTimeMillis();
                if (delay >0){
                    Thread.sleep(delay);
                }
            }
            graphics.dispose();
            output.close();
            mPipe.delete();
            mPipe = null;
        } catch (IOException | InterruptedException ex) {
            Logger.getLogger(OverlayPipe.class.getName()).log(Level.SEVERE, null, ex);
            if (output != null){
                try {
                    output.close();
                } catch (IOException ex1) {
                    Logger.getLogger(OverlayPipe.class.getName()).log(Level.SEVERE, null, ex1);
                }
            }
            if (mPipe != null){
                if (mPipe.exists()){
                    mPipe.delete();
                }
                mPipe = null;
            }
        }
        mIsRunning = false;
    }

    public void stop() {
        stopMe =true;
    }

    public boolean isRunning(){
        return mIsRunning;
    }
    // Create a temp fifo file for output
    private File createPipeFile() throws IOException, InterruptedException {
        File tempFile = File.createTempFile("screenstudio", ".bgr24");
        tempFile.deleteOnExit();
        java.lang.Process p = Runtime.getRuntime().exec("/usr/bin/mkfifo", new String[]{tempFile.getAbsolutePath()});
        p.waitFor();
        p.destroy();
        System.out.println("Output Overlay: " + tempFile.getAbsolutePath());
        return tempFile;
    }

    //For testing...
    public static void main(String[] args) {
        JPanel  p = new JPanel();
        p.setSize(400, 480);
        JTextField text = new JTextField();
        text.setSize(400,30);
        p.add(text);
        OverlayPipe op = new OverlayPipe(p,10);
        op.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException ex) {
            Logger.getLogger(OverlayPipe.class.getName()).log(Level.SEVERE, null, ex);
        }
        for(int i = 0;i<10000 && op.isRunning();i++){
            text.setText(new java.util.Date().toString());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(OverlayPipe.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        op.stop();
    }
}

Leave a Reply