基于ffmpeg使用java实现视频转图片

什么是ffmpeg?

ffmpeg下载安装教程及介绍-CSDN博客

安装后具体实现代码:

java 复制代码
package com.example.playing;

/**
 * @author : 冯子达
 * @version 1.0
 * @description :视频转换为图片 输入视频文件名和文件唯一标识 输出的是图片数量,在本方法里面完成文件夹的创建
 * @createDate : 2024/12/6 07:50
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class VideoToImages {


    /**
     * 每秒生成多少张图片
     */
    private static Integer fps = 24;
    /**
     * 存放视频和图片的文件夹资源根目录
     */
    private static String Path = "D:\\devopment_project\\java_project\\playing\\src\\main\\resources\\";

    /**
     * 将视频转换为图片
     * <p>
     * 视频存放在 video\\videoName.mp4
     * <p>
     * 分割后存储在 images\\videoId 文件夹中
     * 图片的生成名为:output%d.jpg
     *
     * @param videoId   视频的ID
     * @param videoName 视频文件的名 例如 videoName.mp4
     * @return 生成的图片数用于后面判断,如果是0,则代表失败
     */
    public static int videoToCreateImages(String videoId, String videoName) {

//        创建完成目录
        File completedir = new File( Path+"images\\" + videoId);

        // 检查是否创建目录
        boolean completeSuccess = completedir.mkdir();
        if (completeSuccess) {
            System.out.println("目录创建成功!");
        } else {
            System.out.println("目录创建失败");
            return 0;
        }
        String videoPath = Path+"video\\" + videoName;
        // 输出图片的路径前缀(每张图片将使用前缀+序号.jpg的格式)
        String outputPrefix = completedir +"\\output%d.jpg";

        // 构建FFmpeg命令
        ProcessBuilder builder = new ProcessBuilder(
                "ffmpeg",
                "-i", videoPath, // 输入文件
                "-vf", "fps="+fps, // 每秒fps帧
                outputPrefix // 输出文件(使用%d表示帧编号)
        );
        builder.redirectErrorStream(true); // 将错误输出合并到标准输出

        try {
            Process process = builder.start();
            // 读取命令的输出(可选,用于调试或捕获FFmpeg的输出)
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            // 等待进程结束
            int exitCode = process.waitFor();
            System.out.println("FFmpeg process exited with code " + exitCode);
            return countTopLevelFiles(completedir);
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 统计指定目录下的一级文件数量
     *
     * @param dir 目录名
     * @return
     */
    public static int countTopLevelFiles(File dir) {
        int count = 0;
        // 检查目录是否存在
        if (dir.exists() && dir.isDirectory()) {
            // 获取目录下的所有文件和子目录
            File[] files = dir.listFiles();

            if (files != null) {
                for (File file : files) {
                    // 如果是文件,则增加计数器
                    if (file.isFile()) {
                        count++;
                    }
                }
            }
        }
        return count;
    }
}
相关推荐
long3161 小时前
java 工厂方法设计模式 FactoryMethod
java·开发语言·后端·设计模式
萤丰信息3 小时前
智慧工地从工具叠加到全要素重构的核心引擎
java·大数据·人工智能·重构·智慧城市·智慧工地
找不到、了5 小时前
Java设计模式之《原型模式》--深、浅copy
java·设计模式·原型模式
ECC&SM95 小时前
Video_AVI_Packet(1)
笔记·音视频
Evaporator Core5 小时前
Windows批处理脚本自动合并当前目录下由You-get下载的未合并的音视频文件
windows·音视频
程序员岳焱7 小时前
Java 调用 Python 脚本:实现 HelloWorld
java·后端·python
etcix7 小时前
wrap cpp variant as dll for c to use
java·c语言·开发语言
我在北国不背锅8 小时前
基于Java的Markdown转Word工具(标题、段落、表格、Echarts图等)
java·word·echarts·markdown
pengzhuofan8 小时前
Java设计模式-建造者模式
java·设计模式·建造者模式
夕四丶9 小时前
【java实现一个接口多个实现类通用策略模式】
java·策略模式