Byron Kiourtzoglou

About Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Modify video file with Xuggler example

With this example we shall show you how to modify video file using Xuggler in a Java Desktop Application.So, let’s suppose we wish to add a static image to our video and at the same time we wish to reduce the audio volume.

Here are the basic steps you need to take:

  • Create a IMediaReader to read the video file.
  • Configure it to generate BufferImages using mediaReader.setBufferedImageTypeToGenerate.
  • Create an IMediaWriter using ToolFactory.makeWriter.
  • Create a new IMediaTool calling StaticImageMediaTool(imageFilename).
  • Create a new IMediaTool for the audio using new VolumeAdjustMediaTool(0.1).
  • Create a tool chain: reader -> addStaticImage -> reduceVolume -> writer.
  • Create a class tha extends MediaToolAdapter.
  • Override onVideoPicture method. This will help you insert the image in the position you want.

Let’s see the code that implements all the above:

package com.javacodegeeks.xuggler;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ShortBuffer;

import javax.imageio.ImageIO;

import com.xuggle.mediatool.IMediaReader;
import com.xuggle.mediatool.IMediaTool;
import com.xuggle.mediatool.IMediaWriter;
import com.xuggle.mediatool.MediaToolAdapter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.mediatool.event.IAudioSamplesEvent;
import com.xuggle.mediatool.event.IVideoPictureEvent;

public class ModifyMediaExample {

    private static final String inputFilename = "c:/myvideo.mp4";
    private static final String outputFilename = "c:/myvideo.flv";
    private static final String imageFilename = "c:/jcg_logo_small.png";

    public static void main(String[] args) {

  // create a media reader

  IMediaReader mediaReader = ToolFactory.makeReader(inputFilename);

  // configure it to generate BufferImages

  mediaReader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR);

  IMediaWriter mediaWriter = 

   ToolFactory.makeWriter(outputFilename, mediaReader);

  IMediaTool imageMediaTool = new StaticImageMediaTool(imageFilename);

  IMediaTool audioVolumeMediaTool = new VolumeAdjustMediaTool(0.1);

  // create a tool chain:

  // reader -> addStaticImage -> reduceVolume -> writer

  mediaReader.addListener(imageMediaTool);

  imageMediaTool.addListener(audioVolumeMediaTool);

  audioVolumeMediaTool.addListener(mediaWriter);

  while (mediaReader.readPacket() == null) ;

    }

    private static class StaticImageMediaTool extends MediaToolAdapter {

  private BufferedImage logoImage;

  public StaticImageMediaTool(String imageFile) {

try {

    logoImage = ImageIO.read(new File(imageFile));

} 

catch (IOException e) {

    e.printStackTrace();

    throw new RuntimeException("Could not open file");

}

  }

  @Override

  public void onVideoPicture(IVideoPictureEvent event) {

BufferedImage image = event.getImage();

// get the graphics for the image

Graphics2D g = image.createGraphics();

Rectangle2D bounds = new 

  Rectangle2D.Float(0, 0, logoImage.getWidth(), logoImage.getHeight());

// compute the amount to inset the time stamp and 

// translate the image to that position

double inset = bounds.getHeight();

g.translate(inset, event.getImage().getHeight() - inset);

g.setColor(Color.WHITE);

g.fill(bounds);

g.setColor(Color.BLACK);

g.drawImage(logoImage, 0, 0, null);

// call parent which will pass the video onto next tool in chain

super.onVideoPicture(event);

  }

    }

    private static class VolumeAdjustMediaTool extends MediaToolAdapter {

  // the amount to adjust the volume by

  private double mVolume;

  public VolumeAdjustMediaTool(double volume) {

mVolume = volume;

  }

  @Override

  public void onAudioSamples(IAudioSamplesEvent event) {

// get the raw audio bytes and adjust it's value

ShortBuffer buffer = 

   event.getAudioSamples().getByteBuffer().asShortBuffer();

for (int i = 0; i < buffer.limit(); ++i) {

    buffer.put(i, (short) (buffer.get(i) * mVolume));

}

// call parent which will pass the audio onto next tool in chain

super.onAudioSamples(event);

  }

    }

}

This was an example on how to modify video file with Xuggler.

Related Article:

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close