在Java中调用FFmpeg命令可以使用Runtime.getRuntime().exec(command)方法,其中command是要执行的FFmpeg命令。
下面是一个示例代码,演示如何使用Java调用FFmpeg命令来进行视频转码:
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class FFmpegExample {public static void main(String[] args) {String ffmpegCommand = "ffmpeg -i input.mp4 -c:v libx264 -c:a aac output.mp4";try {Process process = Runtime.getRuntime().exec(ffmpegCommand);// 获取进程的输出流BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));String line;while ((line = reader.readLine()) != null) {System.out.println(line);}// 等待FFmpeg命令执行完成int exitCode = process.waitFor();if (exitCode == 0) {System.out.println("视频转码成功!");} else {System.out.println("视频转码失败!");}} catch (IOException | InterruptedException e) {e.printStackTrace();}}}在上面的示例代码中,ffmpegCommand是要执行的FFmpeg命令,input.mp4是要转码的视频文件,output.mp4是转码后的输出文件。你可以根据自己的需求修改这些参数。
注意:在调用exec()方法时,需要处理并读取进程的输出流,否则可能会导致进程阻塞。在示例代码中,我们使用BufferedReader来读取进程的输出,并将其打印到控制台。