liusuyi
2026-06-01 3496700a5ba18be8ca0590a79e21a867782d1ea9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.ard.work.utils;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
 
public class FFmpegUtils {
 
    public static void transcodeToMP4(String inputFilePath, String outputFilePath) {
        try {
            File outputFile = new File(outputFilePath);
            if (outputFile.exists()) {
                outputFile.delete();
            }
 
            ProcessBuilder processBuilder = new ProcessBuilder(
                    "ffmpeg", "-i", inputFilePath,
                    "-c:v", "libx264", "-profile:v", "baseline",
                    "-level", "3.0", "-c:a", "aac", "-strict", "experimental",
                    "-movflags", "faststart", outputFilePath
            );
            processBuilder.redirectErrorStream(true);
 
            Process process = processBuilder.start();
            // 捕获并打印 FFmpeg 的输出日志
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                String line;
                while ((line = reader.readLine()) != null) {
                //    System.out.println(line); // 打印 FFmpeg 输出
                }
            }
            if (process.waitFor() != 0) {
                throw new RuntimeException("ffmpeg 转码失败");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}