Java 跨平台多媒体处理样例

代码

java 复制代码
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import ws.schild.jave.Encoder;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.AudioAttributes;
import ws.schild.jave.encode.EncodingAttributes;
import ws.schild.jave.process.ProcessWrapper;
import ws.schild.jave.process.ffmpeg.DefaultFFMPEGLocator;

import java.io.File;
import java.io.InputStream;

/**
 * 多媒体文件处理工具包
 */
@Slf4j
public class FfmpegUtil {

    /**
     * mp4转mp3
     *
     * @param src  源文件
     * @param dest 目标文件
     */
    public static void mp4ToMp3(String src, String dest) {
        FileUtil.writeUtf8String("", dest);
        File fileMp4 = new File(src);
        File fileMp3 = new File(dest);
        AudioAttributes audio = new AudioAttributes();
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setOutputFormat("mp3");
        attrs.setAudioAttributes(audio);
        Encoder encoder = new Encoder();
        MultimediaObject mediaObject = new MultimediaObject(fileMp4);
        try {
            encoder.encode(mediaObject, fileMp3, attrs);
        } catch (Exception e) {
            log.error(ExceptionUtil.stacktraceToString(e));
        }
    }

    /**
     * 获取第一帧
     */
    public static void firstFrame(String src, String dest) {
        FileUtil.writeUtf8String("", dest);
        ProcessWrapper ffmpeg = new DefaultFFMPEGLocator().createExecutor();
        try {
            ffmpeg.addArgument("-i");
            ffmpeg.addArgument(src);
            ffmpeg.addArgument("-y");
            ffmpeg.addArgument("-ss");
            // 此处可自定义视频的秒数
            ffmpeg.addArgument("1");
            ffmpeg.addArgument("-vframes");
            ffmpeg.addArgument("1");
            ffmpeg.addArgument(dest);
            ffmpeg.execute();
            InputStream errorStream = ffmpeg.getErrorStream();
            String read = IoUtil.read(errorStream, CharsetUtil.UTF_8);
            System.out.println(read);
        } catch (Exception e) {
            log.error(ExceptionUtil.stacktraceToString(e));
        } finally {
            ffmpeg.close();
        }

    }
}

依赖

xml 复制代码
<!--hutool工具包-->
 <dependency>
     <groupId>cn.hutool</groupId>
     <artifactId>hutool-all</artifactId>
     <version>5.7.15</version>
 </dependency>

<!--java多媒体处理工具包-->
 <dependency>
     <groupId>ws.schild</groupId>
     <artifactId>jave-all-deps</artifactId>
     <version>3.2.0</version>
 </dependency>
相关推荐
非优秀程序员3 小时前
第04章 邮箱大战,163 与 QQ 的暗战|《互联网产品恩仇录》
java·vue.js·架构
知已难寻3 小时前
Docker 入门:容器技术与 Docker 基础
java·运维·服务器·docker·云原生·容器
沐晓时光3 小时前
C语言入门,内存函数知识讲解
c语言·开发语言
文丑颜不良啊5 小时前
LeetCode2481~2500题目解析
java·leetcode
Flynn Liang6 小时前
同一需求三套写法:用 Spring AI、LangChain4j、AgentScope Java 各实现一个带工具调用的 RAG 助手(实战对比)
java·人工智能·大模型·架构设计·spring ai·langchain4j·agentscope
聪明的笨猪猪7 小时前
实际工程问题:AI 生成 Spring Cache(@Cacheable / @CachePut / @CacheEvict)缓存注解踩坑
java·经验分享·面试·ai编程
愿有所息7 小时前
记一次 ARM64 + JDK8 上 fastjson2 的诡异数据错乱
java
秋饼7 小时前
MCP 2026-07-28 无状态协议大改版:Spring AI 2.1 多轮往返请求与网关路由落地
java·ai·技术分享·后端开发
SL_staff9 小时前
JVS-BI 实战:业务人员如何用零代码 ELT + 可视化配置构建实时经营分析闭环
java·spring·全栈
SL_staff9 小时前
企业AI落地难?JVS-AI渐进式实践:安全可控、小切口验证、标准化沉淀与规模化复制
java·开源·aigc