Advertisement
Guest User

Untitled

a guest
Aug 10th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. package io.github.bananapuncher714.minecraftvideo.video;
  2.  
  3. import java.io.File;
  4. import java.util.concurrent.CountDownLatch;
  5.  
  6. import uk.co.caprica.vlcj.player.MediaPlayer;
  7. import uk.co.caprica.vlcj.player.MediaPlayerEventAdapter;
  8. import uk.co.caprica.vlcj.player.MediaPlayerFactory;
  9.  
  10. public class AudioExtractor {
  11.     private final CountDownLatch completionLatch;
  12.  
  13.     private final MediaPlayerFactory mediaPlayerFactory;
  14.  
  15.     private final MediaPlayer mediaPlayer;
  16.  
  17.     private final String mrl;
  18.     private final File output;
  19.  
  20.     public AudioExtractor( String mrl, File output ) {
  21.         this.mrl = mrl;
  22.         this.output = output;
  23.  
  24.         completionLatch = new CountDownLatch( 1 );
  25.  
  26.         // Create the media player
  27.         mediaPlayerFactory = new MediaPlayerFactory();
  28.         mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();
  29.         mediaPlayer.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
  30.             @Override
  31.             public void finished( MediaPlayer mediaPlayer ) {
  32.                 completionLatch.countDown();
  33.             }
  34.  
  35.             @Override
  36.             public void error( MediaPlayer mediaPlayer ) {
  37.                 completionLatch.countDown();
  38.             }
  39.         } );
  40.     }
  41.  
  42.     public String getMRL() {
  43.         return mrl;
  44.     }
  45.    
  46.     public File getOutput() {
  47.         return output;
  48.     }
  49.    
  50.     public void extract() {
  51.         mediaPlayer.playMedia( mrl, "sout=#transcode{vcodec=none,acodec=vorbis}:standard{dst=" + output.getAbsolutePath() + ",mux=ogg,access=file}");
  52.  
  53.         try {
  54.             completionLatch.await();
  55.         } catch(InterruptedException e ) {
  56.             e.printStackTrace();
  57.         }
  58.  
  59.         mediaPlayer.stop();
  60.         mediaPlayer.release();
  61.         mediaPlayerFactory.release();
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement