2011-11-29 23 views
2

我正在寫一個Adobe Air應用程序。用戶將能夠將視頻導入到應用程序中,但爲了節省應用程序的某些部分的內存,我需要先將這些視頻轉換爲縮略圖.jpgs,並僅在縮略圖中顯示縮略圖。因此這些縮略圖必須在運行時動態生成。所以我的問題是,如何將視頻轉換爲縮略圖。到目前爲止,我嘗試了兩種不同的方法。我已經使用了ImageSnapshot類,但是我只能將視頻添加到顯示列表中,而不是選項。所以然後我試圖使用BitmapData類繪製位圖數據,並遇到同樣的問題。如果我先將視頻添加到顯示列表中,它纔會起作用。有誰知道我怎樣才能從一幀視頻中獲取位圖數據,而無需先將視頻添加到屏幕上?這裏是我現在使用的代碼,它將最終的.jpg作爲test.jpg寫入桌面,並且只能用於.mp4視頻。如果你把上面寫着this.AddElement(VD),它不再適用於線:在Flex/Actionscript中捕獲視頻縮略圖

<?xml version="1.0" encoding="utf-8"?> 
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                xmlns:s="library://ns.adobe.com/flex/spark" 
                xmlns:mx="library://ns.adobe.com/flex/mx"> 

      <fx:Script> 
        <![CDATA[ 
           import mx.graphics.ImageSnapshot; 
           import mx.graphics.codec.JPEGEncoder; 

           import spark.components.VideoDisplay; 
           import spark.primitives.BitmapImage; 
           private const acceptableTypes:FileFilter = new FileFilter("Media", "*.mp4"); 
           private var vd:VideoDisplay; 

           protected function importVideo(event:MouseEvent):void 
           { 
             var file:File = File.userDirectory; 
             file.addEventListener(Event.SELECT, mediaSelectHandler); 
             file.browseForOpen("Select File To Import", [acceptableTypes]); 
           } 

           private function mediaSelectHandler(event:Event):void { 
             vd = new VideoDisplay(); 
             vd.autoPlay = false; 
             vd.horizontalCenter = 0; 
             vd.verticalCenter = 0; 
             vd.source = event.currentTarget.nativePath; 
             this.addElement(vd); 
           } 

           private function getSnapShot(event:MouseEvent):void{ 
             var bd:BitmapData = new BitmapData(vd.width, vd.height); 
             var matrix:Matrix = new Matrix(); 
             bd.draw(vd, matrix); 

             //save to hard drive 
             var file:File = File.desktopDirectory; 
             var imgfile:File = file.resolvePath("test.jpg"); 
             var jpegEncoder:JPEGEncoder = new JPEGEncoder(90); 
             var jpegStream:ByteArray = jpegEncoder.encode(bd); 
             var filestream:FileStream = new FileStream(); 
             filestream.open(imgfile, FileMode.WRITE); 
             filestream.writeBytes(jpegStream, 0, jpegStream.length); 
             filestream.close(); 
           } 
        ]]> 
      </fx:Script> 

      <fx:Declarations> 
        <!-- Place non-visual elements (e.g., services, value objects) here --> 
      </fx:Declarations> 
      <s:HGroup width="500" height="40" horizontalCenter="0" bottom="50"> 
        <s:Button width="100" height="28" label="IMPORT" click="importVideo(event)"/> 
        <s:Button width="100" height="28" label="SNAPSHOT" click="getSnapShot(event)"/> 
      </s:HGroup> 
</s:WindowedApplication> 
+0

請不要在全部首都寫出問題標題。 –

回答

0

您可能需要使用的ffmpeg程序來達到這個和解決方案可以採取一個新的水平。對於例如你上傳一個10分鐘的視頻。您可以從第1分鐘開始以1分鐘的間隔拍攝10張快照。這將爲您提供一系列視頻快照。你可以實現一個組件作爲一個時間軸,並在滾動顯示相關的快照。你可能在視頻網站上看到過這個。