java实现h265视频转h264视频
时间: 2023-10-09 14:05:09 浏览: 167
要将H.265视频转换为H.264视频,可以使用FFmpeg库。以下是使用Java调用FFmpeg库实现H.265到H.264转换的示例代码:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class H265ToH264Converter {
public static void main(String[] args) {
String inputFilePath = "input.hevc";
String outputFilePath = "output.mp4";
convert(inputFilePath, outputFilePath);
}
public static void convert(String inputFilePath, String outputFilePath) {
List<String> command = new ArrayList<>();
command.add("ffmpeg");
command.add("-i");
command.add(inputFilePath);
command.add("-c:v");
command.add("libx264");
command.add(outputFilePath);
try {
ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
int exitCode = process.waitFor();
if (exitCode == 0) {
System.out.println("Conversion completed successfully");
} else {
System.out.println("Conversion failed");
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
```
这个示例代码使用Java的ProcessBuilder类启动一个FFmpeg进程,并将H.265视频转换为H.264视频。输入文件路径和输出文件路径作为参数传递给convert()方法。在调用Process.waitFor()方法时,程序会等待FFmpeg进程完成转换操作。转换完成后,程序会输出一条相关消息。
阅读全文