Spring Boot 3整合FFmpeg进行图片和MP3转换为视频

Spring Boot 3整合FFmpeg进行图片和MP3转换为视频的示例代码如下:

添加FFmpeg依赖到pom.xml:

bash 复制代码
<dependency>
    <groupId>com.github.kokorin.jaffree</groupId>
    <artifactId>jaffree</artifactId>
    <version>0.1.2</version>
</dependency>

创建一个服务类来执行转换:

bash 复制代码
import com.github.kokorin.jaffree.ffmpeg.FFmpeg;
import com.github.kokorin.jaffree.ffmpeg.FFmpegResult;
import org.springframework.stereotype.Service;
 
import java.nio.file.Paths;
 
@Service
public class VideoConversionService {
 
    public void convertImageAndAudioToVideo(String imagePath, String audioPath, String outputPath) throws Exception {
        FFmpegResult result = FFmpeg.atPath()
                .addInput(FFmpeg.input(imagePath))
                .addInput(FFmpeg.input(audioPath))
                .overrideOutputFiles(true)
                .addOutput(FFmpeg.output(outputPath)
                        .videoCodec("copy")
                        .audioCodec("copy"))
                .execute();
 
        if (result.getState() == FFmpegResult.State.SUCCESS) {
            System.out.println("转换成功");
        } else {
            System.out.println("转换失败");
        }
    }
}

在Spring Boot应用的主类或者任意一个配置类中,调用转换服务:

bash 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
 
@Component
public class VideoConversionRunner implements CommandLineRunner {
 
    @Autowired
    private VideoConversionService videoConversionService;
 
    @Override
    public void run(String... args) throws Exception {
        String imagePath = "path/to/image.jpg";
        String audioPath = "path/to/audio.mp3";
        String outputPath = "path/to/output.mp4";
 
        videoConversionService.convertImageAndAudioToVideo(imagePath, audioPath, outputPath);
    }
}

确保你的图片和音频文件路径是正确的,并且FFmpeg可执行文件在你的系统PATH中或者通过FFmpeg.atPath()指定。

以上代码假设你已经有了一个运行中的Spring Boot 3应用,并且FFmpeg已经安装在你的系统上。如果FFmpeg没有安装,你需要先下载并安装FFmpeg,或者使用Docker容器等方式来运行它。

相关推荐
码出财富10 分钟前
SpringBoot 内置的 20 个高效工具类
java·spring boot·spring cloud·java-ee
座山雕~2 小时前
Springboot
android·spring boot·后端
韩立学长4 小时前
基于Springboot流浪动物救助系统o8g44kwc(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
qq_318121594 小时前
互联网大厂Java面试故事:在线教育微服务架构、缓存优化与AI智能教学全流程解析
java·spring boot·redis·微服务·kafka·spring security·在线教育
进击的CJR4 小时前
redis哨兵实现主从自动切换
mysql·ffmpeg·dba
大猫和小黄6 小时前
Java异常处理:从基础到SpringBoot实战解析
java·开发语言·spring boot·异常
hero.fei6 小时前
kaptcha 验证码生成工具在springboot中集成
java·spring boot·后端
TEL189246224776 小时前
IT6225B:USB-C(DP Alt 模式)及电源传输控制器 内置双通道 DP 1.4 转 HDMI 2.0 转换器
音视频·实时音视频·视频编解码
w***76556 小时前
存储技术全景:从基础原理到未来趋势
spring boot·后端·mybatis
huahualaly8 小时前
重建oracle测试库步骤
数据库·oracle·ffmpeg