FFmpeg实现Java中上传视频并截取第一帧图像(两种方式)

首先,请确保已经安装了FFmpeg工具。然后可以使用Java的ProcessBuilder类来调用FFmpeg命令行工具。

除了使用FFmpeg外,还有一些其他的方式可以在Java中上传视频并截取第一帧图像,使用Java的ImageIO库来实现。

并附上maven包

1:使用这个方式确保安装了FFmpeg

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class VideoUploadExample {
    public static void main(String[] args) {
        String videoFilePath = "path/to/video.mp4";
        String outputImagePath = "path/to/output.jpg";

        try {
            // 上传视频逻辑,将视频文件从源位置上传到目标位置

            // 截取第一帧图像
            File videoFile = new File(videoFilePath);
            File outputImageFile = new File(outputImagePath);
            
            String ffmpegCommand = String.format("ffmpeg -i %s -ss 00:00:00.001 -vframes 1 %s", videoFile.getAbsolutePath(), outputImageFile.getAbsolutePath());
            ProcessBuilder processBuilder = new ProcessBuilder(ffmpegCommand.split(" "));
            Process process = processBuilder.start();
            process.waitFor();

            // 获取截取的图像文件
            if (outputImageFile.exists()) {
                byte[] imageBytes = Files.readAllBytes(outputImageFile.toPath());
                // 在这里可以保存、显示或进一步处理截取的图像
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

2:ImageIo的方法:

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

public class VideoUploadExample {
    public static void main(String[] args) {
        String videoFilePath = "path/to/video.mp4";
        String outputImagePath = "path/to/output.jpg";

        // 上传视频逻辑,将视频文件从源位置上传到目标位置

        // 截取第一帧图像
        File videoFile = new File(videoFilePath);
        File outputImageFile = new File(outputImagePath);

        try {
            FrameCaptureUtil.captureFirstFrame(videoFile, outputImageFile);

            // 获取截取的图像文件
            if (outputImageFile.exists()) {
                BufferedImage image = ImageIO.read(outputImageFile);
                // 在这里可以保存、显示或进一步处理截取的图像
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class FrameCaptureUtil {
    public static void captureFirstFrame(File videoFile, File outputImageFile) throws IOException {
        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(videoFile);
        grabber.start();
        Frame frame = grabber.grabImage();
        BufferedImage image = Java2DFrameUtils.toBufferedImage(frame);
        ImageIO.write(image, "jpg", outputImageFile);
        grabber.stop();
        //获取指定帧数的图片
        //grabber.setFrameNumber(100- 1); // 设置要获取的帧数(索引从0开始)
        //Frame frame = grabber.grab(); // 获取指定帧的图像数据
    }
}

maven包:




    <!-- JavaCV -->
<dependency>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacv-platform</artifactId>
    <version>1.5.6</version>
</dependency>

    <!-- FFmpeg -->
    <dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>ffmpeg</artifactId>
        <version>4.5</version>
    </dependency>
    
    <!-- ImageIO -->
    <dependency>
        <groupId>javax.media</groupId>
        <artifactId>jai_imageio</artifactId>
        <version>1.1</version>
    </dependency>


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

随风奔跑的十八岁

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

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

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

打赏作者

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

抵扣说明:

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

余额充值