Reprint please indicate the source: https://blog.csdn.net/l1028386804/article/details/79755510
First, create the project 1. Create Maven Project 2. Add Dependencies
Add the dependency configuration in Pom.xml, as follows:
<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactid >javacv-platform</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
Second, the concrete realization
1, get the video in the middle of the frame as a thumbnail, and return the actual location of the thumbnail image
Package com.lyz.medis.image;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;
Import Javax.imageio.ImageIO;
Import Org.bytedeco.javacpp.opencv_core; Import Org.bytedeco.javacpp.opencv_core.
Iplimage;
Import Org.bytedeco.javacv.FFmpegFrameGrabber;
Import Org.bytedeco.javacv.Frame;
Import org.bytedeco.javacv.FrameGrabber.Exception;
Import Org.bytedeco.javacv.Java2DFrameConverter;
Import Org.bytedeco.javacv.OpenCVFrameConverter;
/** * Get video thumbnail * @author liuyazhuang * */public class Videoimage {private static final String Imagemat = "png";
private static final String ROTATE = "ROTATE";
/** * Default intercept video intermediate frame for cover * * public static final int MOD = 2; public static void Main (string[] args) throws Exception {System.out.println (Randomgrabberffmpegimage ("c:/lyz/
1522372294724_79583.mov ", 2)); /** * Get video thumbnail * @param filePath: Video path * @param mod: Video length/mod get firstSeveral frames * @throws Exception */public static string Randomgrabberffmpegimage (String filePath, int mod) throws Exception {
String Targetfilepath = "";
Ffmpegframegrabber ff = Ffmpegframegrabber.createdefault (FilePath);
Ff.start ();
String rotate = ff.getvideometadata (rotate);
int fflength = Ff.getlengthinframes ();
Frame F;
int i = 0;
int index = FFLENGTH/MOD;
while (I < fflength) {f = ff.grabimage (); if (i = = index) {if (null!= rotate && rotate.length () > 1) {opencvframeconverter.toiplimage Converter
= new Opencvframeconverter.toiplimage ();
Iplimage src = Converter.convert (f);
f = converter.convert (rotate (src, integer.valueof (rotate));
} Targetfilepath = Getimagepath (FilePath, i);
Doexecuteframe (f, Targetfilepath);
Break
} i++;
} ff.stop ();
return targetfilepath; /** * Generates thumbnails based on video path * @param filePath: Video Path * @param index: Number of frames * @return: Storage path of thumbnails/private static Strin G Getimagepath(String filePath, int index) {if (Filepath.contains (".") && Filepath.lastindexof (".") < Filepath.length ()-1) {FilePath = Filepath.sub String (0, Filepath.lastindexof (".")). Concat ("_"). Concat (string.valueof (Index)). Concat (".").
Concat (Imagemat);
return filePath; /** * Rotate picture * @param src * @param angle * @return * * public static iplimage rotate (iplimage src, int angle)
{iplimage img = iplimage.create (Src.height (), Src.width (), src.depth (), Src.nchannels ());
Opencv_core.cvtranspose (SRC, img);
Opencv_core.cvflip (IMG, img, angle);
return img; /** * Intercept thumbnail * @param f * @param targerfilepath: Cover picture */public static void Doexecuteframe (Frame F, String Targ
Erfilepath) {if (null = f | | | null = = f.image) {return;
} java2dframeconverter Converter = new Java2dframeconverter ();
BufferedImage bi = converter.getbufferedimage (f);
File output = new file (Targerfilepath);
try {imageio.write (bi, Imagemat, output); catch (IoexcEption e) {e.printstacktrace (); }/** * Randomly generates random numbers based on video length * @param basenum: Base number, here for video length * @param length: Random number Set length * @return: Random number set/public St atic list<integer> Random (int basenum, int length) {list<integer> List = new Arraylist<integer> (lengt
h);
while (List.size () < length) {Integer next = (int) (Math.random () * basenum);
if (List.contains (next)) {continue;
} list.add (next);
} collections.sort (list);
return list;
}
}
2, random generation of multiple thumbnails, do not return the actual location of the thumbnail image address
Package com.lyz.medis.image;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;
Import Javax.imageio.ImageIO;
Import Org.bytedeco.javacpp.opencv_core; Import Org.bytedeco.javacpp.opencv_core.
Iplimage;
Import Org.bytedeco.javacv.FFmpegFrameGrabber;
Import Org.bytedeco.javacv.Frame;
Import org.bytedeco.javacv.FrameGrabber.Exception;
Import Org.bytedeco.javacv.Java2DFrameConverter;
Import Org.bytedeco.javacv.OpenCVFrameConverter; /** * Get picture thumbnail * @author liuyazhuang * */Public abstract class Videoimageframe {public static void main (string[] Ar
GS) throws Exception {randomgrabberffmpegimage ("E:/lyz/ffmpeg.mp4", "./target", "screenshot", 5); /** * Generate picture thumbnail * @param filePath: Video Full path * @param targerfilepath: Thumbnail store directory * @param targetfilename: Thumbnail file name * @pa Ram randomsize: Number of random numbers generated * @throws Exception */public static void RandomgrabberffmpegimAge (String FilePath, String Targerfilepath, string targetfilename, int randomsize) throws Exception {Ffmpegframegrabber
FF = Ffmpegframegrabber.createdefault (FilePath);
Ff.start ();
String rotate = ff.getvideometadata ("rotate");
int fflength = Ff.getlengthinframes ();
list<integer> randomgrab = Random (Fflength, randomsize);
int maxrandomgrab = Randomgrab.get (Randomgrab.size ()-1);
Frame F;
int i = 0;
while (I < fflength) {f = ff.grabimage (); if (Randomgrab.contains (i)) {if (null!= rotate && rotate.length () > 1) {opencvframeconverter.toipli
Mage converter = new Opencvframeconverter.toiplimage ();
Iplimage src = Converter.convert (f);
f = converter.convert (rotate (src, integer.valueof (rotate));
} doexecuteframe (f, Targerfilepath, TargetFileName, i);
} if (I >= maxrandomgrab) {break;
} i++;
} ff.stop (); /** * Rotate picture * @param src: Picture * @param angle: Rotation angle * @return/public static IpLimage rotate (iplimage src, int angle) {iplimage img = iplimage.create (Src.height (), Src.width (), src.depth (), Src.ncha
Nnels ());
Opencv_core.cvtranspose (SRC, img);
Opencv_core.cvflip (IMG, img, angle);
return img; /** * Generates thumbnails * @param f Frame Object * @param targerfilepath * @param targetfilename * @param index/Public static void Doexecuteframe (Frame F, String Targerfilepath, string targetfilename, int index) {if (null = f | | | null = =
F.image) {return;
} java2dframeconverter Converter = new Java2dframeconverter ();
String Imagemat = "png";
String FileName = targerfilepath + file.separator + targetfilename + "_" + Index + "." + Imagemat;
BufferedImage bi = converter.getbufferedimage (f);
File output = new file (FileName);
try {imageio.write (bi, Imagemat, output);
catch (IOException e) {e.printstacktrace (); /** * Randomly generated random number set * @param basenum: Random seed * @param length: Random number Set length * @return: Random number set/public static list<i Nteger> random (int basenum, int length) {list<integer> List = new arraylist<> (length);
while (List.size () < length) {Integer next = (int) (Math.random () * basenum);
if (List.contains (next)) {continue;
} list.add (next);
} collections.sort (list);
return list; }
}