Get the first frame of the video, the specified frame (java implementation, Ali oss automatic implementation)

In the actual project, after uploading the video, the first frame of the video needs to be cut out and displayed, so that it can be clearly seen which video is the specific one. This requires a function to cut out the specified frame of the video.

Implemented in java with the help of third-party libraries

First, use maven to manage the project and import dependencies:


The specific java code is as follows:

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;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class GetFirstFrame {

    public static void main(String[] args) throws Exception {
        String url = ""
        randomGrabberFFmpegImage(url, "C:\\Users\\Desktop", "first");

     * @param filePath video path
     * @param targerFilePath The first frame picture storage location
     * @param targetFileName image name
     * @throws Exception
    public static void randomGrabberFFmpegImage(String filePath, String targerFilePath, String targetFileName)
            throws Exception {

        FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(filePath);
        String rotate =ff.getVideoMetadata("rotate");
        Frame f;
        int i = 0;
        while (i <1) {
            f =ff.grabImage();
            IplImage src = null;
            if(null !=rotate &&rotate.length() > 1) {
                OpenCVFrameConverter.ToIplImage converter =new OpenCVFrameConverter.ToIplImage();
                src =converter.convert(f);
                f =converter.convert(rotate(src, Integer.valueOf(rotate)));
     * angle of rotation. This is to ensure that the rotation information in the captured image is consistent with that in the video
    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;

    public static void doExecuteFrame(Frame f, String targerFilePath, String targetFileName) {
        if (null ==f ||null ==f.image) {
        Java2DFrameConverter converter =new Java2DFrameConverter();
        String imageMat ="jpg";
        String FileName =targerFilePath + File.separator +targetFileName +"." +imageMat;
        BufferedImage bi =converter.getBufferedImage(f);
        File output =new File(FileName);
        try {
        }catch (IOException e) {

Ali's own video capture

If our video is uploaded and saved on the Alibaba Cloud server, then we can use the video frame interception method provided by Alibaba to directly intercept the specified frame of the video according to the url.
The specific method is to add some parameters after the url of the video, and then access the new url to get the specified screenshot.

parameters to add
new url
visit this new url to get screenshots

The meanings of the added parameters are as follows:

One thing to note is that currently Alibaba only supports video clipping for video files whose video encoding format is H264.
H264 refers to the encoding format, not the type of video file. After testing it, mainstream video formats, such as mp4, mov, avi, etc., can be cut in this way.

Additionally, signed video clips can be generated. For details, please refer to Alibaba's official documentation: Ali video clipping document

Posted by ArizonaJohn on Sat, 14 May 2022 19:41:26 +0300