/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package example;

import com.xuggle.mediatool.IMediaReader;
import com.xuggle.mediatool.IMediaTool;
import com.xuggle.mediatool.IMediaWriter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.mediatool.MediaToolAdapter;
import com.xuggle.mediatool.event.IAudioSamplesEvent;
import com.xuggle.mediatool.event.IVideoPictureEvent;
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;

/**
 *
 * @author ismailsunni
 */
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);
        
    }

    private static class StaticImageMediaTool extends MediaToolAdapter {

        private BufferedImage logoImage;
        
        public StaticImageMediaTool(String imageFilename) {
            try {
                logoImage = ImageIO.read(new File(imageFilename));
            } catch (IOException e) {
                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 ammount to insert 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));
            }
        }
    }
    
}

@@ -864,26 +864,45 @@
 name = %22
-c:/myvideo
+/home/ismailsunni/Downloads/a
 .mp4%22;%0A 
@@ -954,22 +954,42 @@
  = %22
-c:/myvideo.flv
+/home/ismailsunni/Downloads/ab.mp4
 %22;%0A 
@@ -1040,25 +1040,41 @@
  = %22
-c:/jcg_logo_small
+/home/ismailsunni/Downloads/PRETZ
 .png
@@ -1662,12 +1662,419 @@
 ol(0
-,1);
+.1);%0A        %0A        // Create a tool chain%0A        // Reader -%3E addStaticImage -%3E reduceVolume -%3E writer%0A        mediaReader.addListener(imageMediaTool);%0A        imageMediaTool.addListener(audioVolumeMediaTool);%0A        audioVolumeMediaTool.addListener(mediaWriter);%0A        %0A        int i = 0;%0A        while (mediaReader.readPacket() == null)%7B%0A            i += 1;%0A            System.out.println(i);%0A        %7D
 %0A   
@@ -4056,32 +4056,167 @@
 ;%0A            %7D%0A
+            %0A            // Cal parent which will call pass the audio onto next tool in chain%0A            super.onAudioSamples(event);%0A
         %7D%0A    %7D%0A

Add image to video