In my application, I want record a video from web cam and also get snapshot of current frame at certain intervals for processing at the time of recording. When i used :sout=#transcode{ } as mediaPlayer() option for recording video, getSanpshot() method dosen't work it gives error and dosen't get the current frame or image.
This is the code I am using:
Code: Select all
package vlctut;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.player.embedded.videosurface.CanvasVideoSurface;
public class TestLoad1 implements ActionListener {
private final JFrame frame;
private final JPanel contentPane;
private final Canvas canvas;
private final String [] Vlc_args={"--quiet","--video-filter=motion","--no-overlay"};
private MediaPlayerFactory factory;
private EmbeddedMediaPlayer mediaPlayer,mPlayer1;
private BufferedImage img1;
private CanvasVideoSurface videoSurface;
public static void main(String[] args) {
Vlctut loadVlcJ = new Vlctut();
loadVlcJ.load_lib();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestLoad1();
}
});
}
public TestLoad1() {
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);
this.mediaPlayerPanel();
this.start("dshow://");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent ev){
factory.release();
mediaPlayer.release();
}
});
}
private void mediaPlayerPanel()
{
factory = new MediaPlayerFactory(Vlc_args);
mediaPlayer = factory.newEmbeddedMediaPlayer();
videoSurface = factory.newVideoSurface(canvas);
mediaPlayer.setVideoSurface(videoSurface);
}
private void start(String mrl) {
frame.setVisible(true);
System.out.println(System.getProperty("user.dir"));
File dir = new File(System.getProperty("user.dir"), "Videos");
dir.mkdirs();
DateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss");
String fileName = dir.getAbsolutePath() + "/Capture-" + df.format(new Date()) + ".mpg";
//streaming only video
String[] options = {":dshow-vdev=USB Video Device","dshow-size=640x480",
":sout=#transcode{vcodec=mp2v, vb=4096,scale=1, acodec=mpga, ab=128, channels=2, samplerate=44100}"
+ ":duplicate{dst=file{dst="+ fileName+"}, dst=display}"};
mediaPlayer.playMedia(mrl,options);
Timer time=new Timer(5000, this);
time.start();
}
@Override
public void actionPerformed(ActionEvent ae)
{
img1=mPlayer1.getSnapshot();
File f1=new File("D://xyz.png");
try {
ImageIO.write(img1, "png", f1);
} catch (IOException ex) {
Logger.getLogger(TestLoad1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at vlctut.TestLoad1.actionPerformed(TestLoad1.java:119)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)