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

相关推荐
FinelyYang19 分钟前
uniapp+unipush2.0+WebRTC实现h5一对一视频通话
uni-app·音视频·webrtc
s***353028 分钟前
Spring Boot3.x集成Flowable7.x(一)Spring Boot集成与设计、部署、发起、完成简单流程
java·spring boot·后端
3***161029 分钟前
【监控】Spring Boot+Prometheus+Grafana实现可视化监控
spring boot·grafana·prometheus
s***45331 分钟前
SpringBoot返回文件让前端下载的几种方式
前端·spring boot·后端
q***04052 小时前
从MySQL5.7平滑升级到MySQL8.0的最佳实践分享
ffmpeg
一叶飘零_sweeeet2 小时前
FFmpeg 实战全解析:从底层原理到企业级应用落地
ffmpeg
4***R2402 小时前
C++在音视频处理中的库
开发语言·c++·音视频
i***27953 小时前
Spring boot 3.3.1 官方文档 中文
java·数据库·spring boot
allbs3 小时前
spring boot项目excel导出功能封装——4.导入
spring boot·后端·excel
l***77524 小时前
总结:Spring Boot 之spring.factories
java·spring boot·spring