2013-08-21 58 views
1

我已經查看過以前的主題,但無法找到類CompressedTrack。 我能夠從視頻抓取幀。 有什麼辦法可以使用BufferedImages創建mp4視頻文件(截圖)。Java將BufferedImages編碼爲視頻文件(mp4)

感謝您的幫助!

+2

*「我已查看過以前的主題」*鏈接到它們。 *「但是CompressedTrack類。」*這是什麼?鏈接到JavaDocs。 –

回答

7
package org.tkassembled.xuggle; 

import java.awt.Dimension; 
import java.awt.Toolkit; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.util.concurrent.TimeUnit; 

import javax.imageio.ImageIO; 

import com.xuggle.mediatool.IMediaWriter; 
import com.xuggle.mediatool.ToolFactory; 
import com.xuggle.xuggler.ICodec; 

public class ManualVideoCompile { 

    private static Dimension screenBounds; 
    public static int indexVideo = 1; 
    private static final double FRAME_RATE = 50; 

    private static final int SECONDS_TO_RUN_FOR = 20; 
    private static final String OUTPUT_FILE = "C:/video/today.mp4"; 

    public static void main(String[] arguments) { 
     final IMediaWriter writer = ToolFactory.makeWriter(OUTPUT_FILE); 
     screenBounds = Toolkit.getDefaultToolkit().getScreenSize(); 
     writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_MPEG4, 
       screenBounds.width/2, screenBounds.height/2); 
     long startTime = System.nanoTime(); 

     for (int index = 0; index < 15; index++) { 

      BufferedImage bgrScreen = getVideoImage(); 
      System.out.println("time stamp = "+ (System.nanoTime() - startTime)); 
      bgrScreen = convertToType(bgrScreen, BufferedImage.TYPE_3BYTE_BGR); 
      // encode the image to stream #0 
      //writer.encodeVideo(0, bgrScreen, (System.nanoTime() - startTime)/2,TimeUnit.NANOSECONDS); 
      // encode the image to stream #0 
      writer.encodeVideo(0, bgrScreen, System.nanoTime() - startTime, 
        TimeUnit.NANOSECONDS); 
      // sleep for frame rate milliseconds 
      try { 
       Thread.sleep((long) (100)); 
      } catch (InterruptedException e) { 
       // ignore 
      } 
     } 
     writer.close(); 
    } 

    private static BufferedImage getVideoImage() { 

     File imgLoc = new File("C:/images/" + indexVideo + ".png"); 
     BufferedImage img = null; 
     try { 
      img = ImageIO.read(imgLoc); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     System.out.println(imgLoc.getName()); 
     indexVideo++; 
     return img; 
    } 

    public static BufferedImage convertToType(BufferedImage sourceImage, 
      int targetType) { 

     BufferedImage image; 

     // if the source image is already the target type, return the source 
     // image 
     if (sourceImage.getType() == targetType) { 
      image = sourceImage; 
     } 
     // otherwise create a new image of the target type and draw the new 
     // image 
     else { 
      image = new BufferedImage(sourceImage.getWidth(), 
        sourceImage.getHeight(), targetType); 
      image.getGraphics().drawImage(sourceImage, 0, 0, null); 
     } 

     return image; 

    } 

} 
+0

它只是從images.Hope創建mp4視頻它可以幫助 – chandresh

+0

我可以添加錄製的聲音嗎? – 2015-05-02 10:47:35

+0

我havnt工作在:( – chandresh