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

相关推荐
代码or搬砖39 分钟前
RBAC(权限认证)小例子
java·数据库·spring boot
daidaidaiyu2 小时前
FFmpeg 关键的结构体
c++·ffmpeg
咸鱼加辣2 小时前
【java面试题】springboot的生命周期
java·开发语言·spring boot
VX:Fegn08953 小时前
计算机毕业设计|基于springboot + vue敬老院管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
好游科技4 小时前
语聊APP新生态!一站式语聊房语音直播APP源码开发搭建
音视频·webrtc·im即时通讯·社交软件·社交语音视频软件
北漂IT民工_程序员_ZG4 小时前
SpringBean生命周期,动态代理
java·spring boot·spring
老华带你飞5 小时前
建筑材料管理|基于springboot 建筑材料管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习·spring
ss2736 小时前
SpringBoot+vue养老院运营管理系统
vue.js·spring boot·后端
summerkissyou19876 小时前
Android-Audio-为啥不移到packages/module
android·音视频