2013-09-27 96 views
0

我已經基於VLCj測試文件編寫了下面的簡單程序。我想查看視頻並將其保存到音頻文件中。我已經設法使用現在的選項,以便一個文件保存良好的視頻。但是,幀中的圖像不會移動,保存的視頻也沒有音頻。VLCj錄製視頻 - 從機中沒有音頻和無運動

import com.sun.jna.NativeLibrary; 
import java.awt.BorderLayout; 
import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.event.WindowEvent; 
import java.io.File; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.concurrent.Executors; 
import java.util.concurrent.ScheduledExecutorService; 
import java.util.concurrent.TimeUnit; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JSlider; 
import javax.swing.SwingUtilities; 
import javax.swing.WindowConstants; 
import uk.co.caprica.vlcj.binding.internal.libvlc_state_t; 
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent; 
import uk.co.caprica.vlcj.discovery.NativeDiscovery; 

import uk.co.caprica.vlcj.player.MediaPlayerFactory; 
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer; 
import uk.co.caprica.vlcj.player.embedded.videosurface.CanvasVideoSurface; 
import uk.co.caprica.vlcj.runtime.RuntimeUtil; 
import uk.co.caprica.vlcj.runtime.x.LibXUtil; 
import uk.co.caprica.vlcj.version.LibVlcVersion; 
import uk.co.caprica.vlcj.binding.LibVlc; 

public class Capture { 

    private final JFrame frame; 
    private final JPanel contentPane; 
    private final Canvas canvas; 
    private static MediaPlayerFactory factory; 
    private static EmbeddedMediaPlayer mediaPlayer; 
    private static CanvasVideoSurface videoSurface; 
    private static String libvlcDir; 
    private static boolean discovered; 
    private static Capture capture; 

    public static void main(final String[] args) { 

     libvlcDir = "/Applications/VLC.app/Contents/MacOS/lib"; 
     NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), libvlcDir); 

     LibXUtil.initialise(); 

     capture = new Capture();    

     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       capture.start("qtcapture://0x1a11000005ac8509"); 
      } 
     }); 
    } 

    public Capture() { 
     canvas = new Canvas(); 
     canvas.setBackground(Color.black); 

     contentPane = new JPanel(); 
     contentPane.setBackground(Color.black); 
     contentPane.setLayout(new BorderLayout()); 
     contentPane.add(canvas, BorderLayout.CENTER); 

     frame = new JFrame("Capture"); 
     frame.setContentPane(contentPane); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocation(50, 50); 
     frame.setSize(800, 600); 

     factory = new MediaPlayerFactory(); 
     mediaPlayer = factory.newEmbeddedMediaPlayer(); 
     videoSurface = factory.newVideoSurface(canvas); 

     mediaPlayer.setVideoSurface(videoSurface); 
    } 

    private void start(String mrl) { 
     frame.setVisible(true); 

     // GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frame); 

     File dir = new File(System.getProperty("user.home"), "Videos"); 
     dir.mkdirs(); 

     DateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss"); 
     String fileName = dir.getAbsolutePath() + "/Capture-" + df.format(new Date()) + ".TS"; 
     System.out.println(fileName); 
     // Tweak the options depending on your encoding requirements and audio 
     // capture device (ALSA is not likely to work on Windows of course) 

     //String[] options = {":sout=#transcode{vcodec=mp4v,vb=4096,scale=1,acodec=mpga,ab=128,channels=2,samplerate=44100}:duplicate{dst=file{dst=" + fileName + "},dst=display}", ":input-slave=alsa://hw:0,0"}; /original options - does not work on mac  
     String options = ":sout=#transcode{vcodec=h264,vb=1024,scale=1,width=360,height=300,acodec=mp4a, aenc=fdkaac,ab=256,channels=6,samplerate=44100}:duplicate{dst=file{dst=" + fileName + "},dst=display}"; 

     mediaPlayer.playMedia(mrl, options); //Records but no audio - also doesn't play in window (still image) 

     //mediaPlayer.playMedia(mrl); //Just plays in window (no recording) 
    } 


    public static boolean setupVLC(){ 
    //Open and read VLC directory from settings file 
     String msg = ""; 
     discovered=false; 
     //If file doesn't exist, then guess 
     File temp = new File(libvlcDir); 
     String os = System.getProperty("sun.arch.data.model"); 
     if(!temp.exists()){ 
      discovered = setupLibVLC(); 
      if (discovered){ 
       //update settings if found 
       String tempDir = NativeLibrary.getInstance(RuntimeUtil.getLibVlcLibraryName()).toString();  
       tempDir = tempDir.substring(tempDir.indexOf("<")+1,tempDir.lastIndexOf("/")); 
       libvlcDir = tempDir; 
       System.out.println("VLCdir = " + libvlcDir); 
      }    
     }else{ 
      discovered=true; 
     } 
     //If still can't find it, ask for pointer to it 
     temp = new File(libvlcDir); 
     if(temp.exists() || temp.isDirectory() || discovered){ 
      NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), libvlcDir); 
     }else{ 
      msg = "Can't find correct libraries in "+libvlcDir+" Please select the file."; 
      findDllFolder(msg); 
      //If still not found then ask for directory 
      NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), libvlcDir); 
      try{ 
       System.out.println("libvlc version = " + LibVlcVersion.getVersion().toString()); 
       discovered=true; 
      }catch(java.lang.UnsatisfiedLinkError err){ 
       msg= "You may be using the wrong version of VLC. Can't find correct libraries in "+ 
           libvlcDir+ 
           " Please select the file."; 
       findDllFolder(msg); 
      }catch(java.lang.NoClassDefFoundError err){ 
       msg = "Can't find correct libraries in "+libvlcDir+" Please select the file."; 
       findDllFolder(msg);   
      } 
     } 
     //Final throw of the dice to test the work above 
     try{ 
      System.out.println("libvlc version = " + LibVlcVersion.getVersion().toString()); 
      discovered=true; 
     }catch(java.lang.UnsatisfiedLinkError err){ 
      discovered = false; 
     }catch(java.lang.NoClassDefFoundError err){ 
      discovered = false; 
     } 
     return discovered; 
    } 
     public static void findDllFolder(String msg){ 
     JOptionPane.showMessageDialog(null, msg, "Error", JOptionPane.ERROR_MESSAGE); 
       JFileChooser fileChooser = new JFileChooser(); 
       int result = fileChooser.showOpenDialog(null); 
       if (result == JFileChooser.APPROVE_OPTION){ 
        libvlcDir = fileChooser.getSelectedFile().toString(); 
        if(System.getProperty("os.name").contains("Mac")){ 
         libvlcDir = libvlcDir + "/Contents/MacOS/lib/"; 
        }else{ 
         libvlcDir = libvlcDir.substring(0,libvlcDir.lastIndexOf("\\")); 
         libvlcDir = libvlcDir.replace("\\", "\\\\"); 
        } 
        System.out.println("VLCdir = "+ libvlcDir); 

       } 
    } 
    private static boolean setupLibVLC() { 

     discovered = new NativeDiscovery().discover(); 

     // discovery()'s method return value is WRONG on Linux 
     try { 
      LibVlcVersion.getVersion(); 
     } catch (Exception e) { 
      System.out.println("error in setupLibVLC"); 
     } 
     return discovered; 
    } 
} 

錯誤信息運行時,我得到的是:

x264 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 FastShuffle SSE4.2 AVX

x264 [info]: profile High, level 2.1

x264 [info]: final ratefactor: 19.03

x264 [info]: using SAR=10/9

x264 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 FastShuffle SSE4.2 AVX

x264 [info]: profile High, level 2.1

x264 [warning]: invalid DTS: PTS is less than DTS

x264 [warning]: invalid DTS: PTS is less than DTS

x264 [warning]: invalid DTS: PTS is less than DTS

[0x7f90bb2456c0] main vout display error: Failed to resize display

[0x7f90bb2456c0] main vout display error: Failed to set on top

[h264 @ 0x7f90b4064020] mmco: unref short failure

[h264 @ 0x7f90b4064620] mmco: unref short failure

[h264 @ 0x7f90b4063a20] mmco: unref short failure

[h264 @ 0x7f90b4063a20] mmco: unref short failure

任何幫助,將不勝感激!另外,如果有一種「清潔」的方式來整理我的「SetupVLC」代碼,那麼我就是耳熟能詳。

謝謝!

回答

0

當像這樣進行代碼轉換時,您需要在媒體播放完成時調用媒體播放器上的release(),以便正確刷新本機緩衝區並正確關閉文件。

+0

謝謝。當框架關閉時,我已經把它放進去了。但是,視頻仍然不在mediaPLayer中播放(它是靜止圖像),錄製的視頻沒有音頻。我還收到一條錯誤消息:「0x11891f9b0 rip = 0x7fff92d60250」的內存訪問無效。我錯過了什麼? – ThatGuy

+0

卡普里卡,有什麼我可以嘗試嗎? – ThatGuy

+0

無論什麼時候你對代碼進行代碼轉換,在你考慮vlcj之前,我總是會首先在vlc中工作。這將突出顯示任何基本問題。原則上,當你在vlc中工作時,它應該在vlcj中工作。 – caprica