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容器等方式来运行它。

相关推荐
aqi0018 小时前
FFmpeg开发笔记(八十六)基于Node.js的国产视频编辑开源框架OpenCut
ffmpeg·音视频·直播·流媒体
摇滚侠19 小时前
Spring Boot3零基础教程,函数式 Web 新特性,笔记51
java·spring boot·笔记
执笔论英雄19 小时前
【大模型推理】ScheduleBatch 学习
java·spring boot·学习
Slow菜鸟21 小时前
SpringBoot集成Elasticsearch | Elasticsearch 7.x专属HLRC(High Level Rest Client)
spring boot·elasticsearch·jenkins
Slow菜鸟1 天前
SpringBoot教程(安装篇):Elasticsearch及可视化工具安装(Windows环境)
spring boot·elasticsearch
忧郁的橙子.1 天前
IntelliJ IDEA 2023中为 Spring Boot 项目添加注释模板
java·spring boot·intellij-idea
聆风吟º1 天前
【Spring Boot 报错已解决】别让端口配置卡壳!Spring Boot “Binding to target failed” 报错解决思路
android·java·spring boot
遥远_1 天前
Spring Boot微服务健康检测:保障系统稳定性的关键实践
spring boot·微服务·1024程序员节·健康检测
苹果醋31 天前
学习札记-Java8系列-1-Java8新特性简介&为什么要学习Java8
java·运维·spring boot·mysql·nginx
兜兜风d'1 天前
RabbitMQ 发送方确认机制详解
spring boot·分布式·rabbitmq·java-rabbitmq·1024程序员节