(无报错)Java截取视频生成Gif动图 视频转gif动态图 视频截帧取图 java实现方式

不废话直接上代码

1.maven坐标

        <!-- 视频截图 -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv-platform</artifactId>
            <version>1.4.4</version>
        </dependency>
        <!-- gif -->
        <dependency>
            <groupId>com.madgag</groupId>
            <artifactId>animated-gif-lib</artifactId>
            <version>1.4</version>
        </dependency>

2.工具类

import com.madgag.gif.fmsware.AnimatedGifEncoder;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author cc
 */
public class VideoUtils {

    /**
     * 截取视频指定帧生成gif
     *
     * @param videofile  视频文件
     * @param startFrame 开始帧
     * @param frameCount 截取帧数
     * @param frameRate  帧频率(默认:3)
     * @param margin     每截取一次跳过多少帧(默认:3)
     * @throws java.io.IOException 截取的长度超过视频长度
     */
    public static void buildGif(String videofile, int startFrame, int frameCount, Integer frameRate, Integer margin) throws IOException {
        if (margin == null) {
            margin = 3;
        }
        if (frameRate == null) {
            frameRate = 3;
        }
        FileOutputStream targetFile = new FileOutputStream(videofile.substring(0, videofile.lastIndexOf(".")) + ".gif");
        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videofile);
        Java2DFrameConverter converter = new Java2DFrameConverter();
        ff.start();
        try {
            if (startFrame > ff.getLengthInFrames() & (startFrame + frameCount) > ff.getLengthInFrames()) {
                throw new RuntimeException("THE VIDEO IS TOO SHORT!");
            }
            ff.setFrameNumber(startFrame);
            AnimatedGifEncoder en = new AnimatedGifEncoder();
            en.setFrameRate(frameRate);
            en.start(targetFile);
            for (int i = 0; i < frameCount; i++) {
                en.addFrame(converter.convert(ff.grabImage()));
                ff.setFrameNumber(ff.getFrameNumber() + margin);
            }
            en.finish();
        } finally {
            ff.stop();
            ff.close();
        }
    }

    /**
     * 将图片旋转指定度
     *
     * @param bufferedimage 图片
     * @param degree        旋转角度
     * @return
     */
    public static BufferedImage rotateImage(BufferedImage bufferedimage, int degree) {
        // 得到图片宽度。
        int w = bufferedimage.getWidth();
        // 得到图片高度。
        int h = bufferedimage.getHeight();
        // 得到图片透明度。
        int type = bufferedimage.getColorModel().getTransparency();
        // 空的图片。
        BufferedImage img;
        // 空的画笔。
        Graphics2D graphics2d;
        (graphics2d = (img = new BufferedImage(w, h, type))
                .createGraphics()).setRenderingHint(
                RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        // 旋转,degree是整型,度数,比如垂直90度。
        graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
        // 从bufferedimagecopy图片至img,0,0是img的坐标。
        graphics2d.drawImage(bufferedimage, 0, 0, null);
        graphics2d.dispose();
        // 返回复制好的图片,原图片依然没有变,没有旋转,下次还可以使用。
        return img;
    }

    /**
     * 截取视频指定帧保存为指定格式的图片(图片保存在视频同文件夹下)
     *
     * @param videofile  视频地址
     * @param imgSuffix  图片格式
     * @param indexFrame 第几帧(默认:5)
     * @throws Exception
     */
    public static void fetchFrame(String videofile, String imgSuffix, Integer indexFrame) throws Exception {
        if (indexFrame == null) {
            indexFrame = 5;
        }
        Integer suffixIndex = videofile.lastIndexOf(".");
        File targetFile = new File((suffixIndex != -1 ? videofile.substring(0, suffixIndex) : videofile) + imgSuffix);

        try (FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videofile);
             OutputStream outputStream = new FileOutputStream(targetFile)) {
            ff.start();
            ff.setFrameNumber(indexFrame);
            Frame f = ff.grabImage();
            Java2DFrameConverter converter = new Java2DFrameConverter();
            BufferedImage fecthedImage = converter.getBufferedImage(f);
            ImageIO.write(fecthedImage, "jpg", outputStream);
        }
    }


    public static void main(String[] args) {
        try {
           // fetchFrame("D:\\test\\QQ.mp4", ".jpg", 100);
            buildGif("D:\\test\\QQ.mp4", 50, 10, 3, 50);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若光672

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值