Java利用VLC实现从视频中抽取帧图片

1,资源准备

vlc官网下载地址:https://get.videolan.org/vlc/3.0.4/win64/vlc-3.0.4-win64.exe

slf4j下载地址:https://www.slf4j.org/download.html

vlcj下载地址:http://capricasoftware.co.uk/#/projects/vlcj(往下拉)

2,环境搭建

1,VLC版本对应自身电脑版本,否则会报错,报错信息如下:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'libvlc': JNA native support (win32-amd64/libvlc.dll) not found in resource path

2,将SLF4J中的SLF4J-API-1.7.13.jar,SLF4J-NOP-1.7.13.jar复制到项目的LIB下

3,将vlcj中的JNA-3.5.2.jar,平台3.5.2.jar,vlcj-3.8.0.jar复制到项目的LIB下

4,封装VLC库,新建的Java的的项目就行

5,将vlc安装目录下的  插件,libvlc.dll,libvlccore.dll,三个文件放进项目根目录下,

6,最后项目结构图就算是搭建完成了

Java利用VLC实现从视频中抽取帧图片_第1张图片

 

3,环境测试:

校验库是否加载成功

package test;

import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;

import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.discovery.NativeDiscovery;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
/**
 * 所属包:test
* 类名:PlayMain
* -------------------
* 描述:加载本地库,仅限于windows系统
* -------------------
* 日期:2018年11月21日
* 作者:cuixin */ public class TestLib{ private static final String NATIVE_LIBRARY_SEARCH_PATH = "F://VLC"; public static void main(String[] args) { NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), NATIVE_LIBRARY_SEARCH_PATH); Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class); boolean discover = new NativeDiscovery().discover(); System.out.println(discover); } }

4,预设进度值抽取截图类


package test;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;

import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.discovery.NativeDiscovery;
import uk.co.caprica.vlcj.player.MediaPlayer;
import uk.co.caprica.vlcj.player.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
/**
 * 所属包:test
* 类名:SnapshotTest
* -------------------
* 描述:预设帧数,抽取截图
* -------------------
* 日期:2018年11月22日
* 作者:cuixin */ public class SnapshotTest { private static final String NATIVE_LIBRARY_SEARCH_PATH = "F://VLC"; public static void main(String[] args) throws Exception { NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), NATIVE_LIBRARY_SEARCH_PATH); Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class); @SuppressWarnings("unused") boolean discover = new NativeDiscovery().discover(); //预设进度值 ListlistBar=new ArrayList(); listBar.add(0.20f); listBar.add(0.50f); listBar.add(0.70f); listBar.add(0.90f); MediaPlayerFactory factory = new MediaPlayerFactory(); MediaPlayer mediaPlayer = factory.newEmbeddedMediaPlayer(); mediaPlayer.addMediaPlayerEventListener(new MediaPlayerEventAdapter() { @Override public void snapshotTaken(MediaPlayer mediaPlayer, String filename) { System.out.println("截取的快照:+(filename=" + filename + ")"); } }); mediaPlayer.startMedia("E:\\百度网盘下载地址\\30HZ-264 模特.mp4"); for (Float float1 : listBar) { mediaPlayer.setPosition(float1); Thread.sleep(1000); File file3 = new File((int)(float1*100)+".png"); file3.deleteOnExit(); mediaPlayer.saveSnapshot(file3); BufferedImage image3 = ImageIO.read(file3); show("截屏", image3, 3); } mediaPlayer.stop(); } @SuppressWarnings("serial") private static void show(String title, final BufferedImage img, int i) { JFrame f = new JFrame(title); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(new JPanel() { @Override protected void paintChildren(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.drawImage(img, null, 0, 0); } @Override public Dimension getPreferredSize() { return new Dimension(img.getWidth(), img.getHeight()); } }); f.pack(); f.setLocation(50 + (i * 50), 50 + (i * 50)); //是否开启弹窗 f.setVisible(false); } }

源码地址:https://download.csdn.net/download/qq_40773484/10802973

你可能感兴趣的:(Java利用VLC实现从视频中抽取帧图片)