2013-05-27 77 views
0

我使用xuggler但不明白th​​at.I有ArrayList,我想從這個圖像,視頻製作。但是,當我在視頻圖像的第一個5秒內編譯此代碼不會改變,而在視頻上持續5秒時,則完全不會。如何解決它。圖像到視頻Xuggler

public class ScreenRecordingExample { 

    private static int FPS = 1000/25; 
    private static final String outputFilename = "c:/mydesktop.mp4"; 
    private static Dimension screenBounds; 

    public static void main(String[] args) throws IOException { 

     final IMediaWriter writer = ToolFactory.makeWriter(outputFilename); 

     screenBounds = Toolkit.getDefaultToolkit().getScreenSize(); 

     writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_MPEG4, 
       screenBounds.width/2, screenBounds.height/2); 

     long startTime = System.nanoTime(); 

     List<BufferedImage> BIList = getImagesFromDirectory(null); 

     for (int index = 0; index < BIList.size(); index++) { 

      BufferedImage bgrScreen = convertToType(BIList.get(index),BufferedImage.TYPE_3BYTE_BGR); 
      writer.encodeVideo(0, bgrScreen, System.nanoTime() - startTime, 
        TimeUnit.NANOSECONDS); 

      try { 
       Thread.sleep((long) (1000/15)); 
      } catch (InterruptedException e) {} 
     } 

     writer.close(); 

    } 

    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; 

    } 

    private static List<BufferedImage> getImagesFromDirectory(File directory) throws IOException { 


     File dir = new File("C:\\fotos\\"); 
     final String[] EXTENSIONS = new String[]{"gif", "png", "bmp"}; 

     final List<BufferedImage> imageList = new ArrayList<BufferedImage>(); 

     FilenameFilter IMAGE_FILTER = new FilenameFilter() { 

      @Override 
      public boolean accept(final File dir, final String name) { 
       for (final String ext : EXTENSIONS) { 
        if (name.endsWith("." + ext)) { 
         return (true); 
        } 
       } 
       return (false); 
      } 
     }; 
     if (dir.isDirectory()) { // make sure it's a directory 
      for (final File f : dir.listFiles(IMAGE_FILTER)) { 
       BufferedImage img = null; 
       img = ImageIO.read(f); 
       imageList.add(img); 
      } 
     } 
     return imageList; 
    } 

回答

1

編輯你的問題,它本身就是一個難題。 Dint明白你的意思是「視頻圖像的前5秒不變,視頻持續5秒沒有變化」。

0

我希望你會發現你的答案,但是這可能幫助別人,移動開始時間初始化之後,將調用getImagesFromDirectory

long startTime = System.nanoTime(); 
List<BufferedImage> BIList = getImagesFromDirectory(null); 

List<BufferedImage> BIList = getImagesFromDirectory(null); 
long startTime = System.nanoTime();*