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:

		<dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacpp</artifactId>
            <version>1.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv</artifactId>
            <version>1.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.bytedeco.javacpp-presets</groupId>
            <artifactId>opencv-platform</artifactId>
            <version>3.4.1-1.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.bytedeco.javacpp-presets</groupId>
            <artifactId>ffmpeg-platform</artifactId>
            <version>3.4.2-1.4.1</version>
        </dependency>

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;
import java.io.File;
import java.io.IOException;

public class GetFirstFrame {

    public static void main(String[] args) throws Exception {
        String url = "http://www.baidu.com/group1/M00/00/9E/rBIuClzSQHaAPClMAA5OVT6OM7w328.mp4"
        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);
        ff.start();
        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)));
            }
            doExecuteFrame(f,targerFilePath,targetFileName);
            i++;
        }
        ff.stop();
    }
    
    /*
     * 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) {
            return;
        }
        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 {
            ImageIO.write(bi,imageMat,output);
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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.
example:

parameters to add
"?x-oss-process=video/snapshot,t_0,f_jpg,w_0,h_0,m_fast,ar_auto"
new url
"https://lawyer-finder.oss-cn-hangzhou.aliyuncs.com/sjfifnbufbvv.mp4?x-oss-process=video/snapshot,t_0,f_jpg,w_0,h_0,m_fast,ar_auto" 
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