114 lines
3.3 KiB
Plaintext
114 lines
3.3 KiB
Plaintext
package Sdf1_login;
|
|
|
|
import java.io.*;
|
|
import java.nio.file.*;
|
|
import java.util.logging.Logger;
|
|
|
|
public class RadioConverter {
|
|
|
|
private final Logger logger;
|
|
private final boolean ffmpegAvailable;
|
|
|
|
public RadioConverter(Logger logger) {
|
|
this.logger = logger;
|
|
this.ffmpegAvailable = checkFfmpeg();
|
|
}
|
|
|
|
/**
|
|
* 转换音频文件为 ogg 格式
|
|
* 支持: mp3, wav, flac, ogg
|
|
*/
|
|
public boolean convert(File input,
|
|
File output) {
|
|
String name =
|
|
input.getName().toLowerCase();
|
|
|
|
// ogg 文件直接复制
|
|
if (name.endsWith(".ogg")) {
|
|
return copyFile(input, output);
|
|
}
|
|
|
|
// 有 ffmpeg 则转换
|
|
if (ffmpegAvailable) {
|
|
return convertFfmpeg(input, output);
|
|
}
|
|
|
|
// 无 ffmpeg 无法转换
|
|
logger.warning(
|
|
"[Radio] 无法转换: "
|
|
+ input.getName()
|
|
+ " (需要安装ffmpeg)");
|
|
return false;
|
|
}
|
|
|
|
private boolean copyFile(File src,
|
|
File dest) {
|
|
try {
|
|
dest.getParentFile().mkdirs();
|
|
Files.copy(src.toPath(),
|
|
dest.toPath(),
|
|
StandardCopyOption
|
|
.REPLACE_EXISTING);
|
|
return true;
|
|
} catch (Exception e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private boolean convertFfmpeg(
|
|
File input, File output) {
|
|
try {
|
|
output.getParentFile().mkdirs();
|
|
ProcessBuilder pb =
|
|
new ProcessBuilder(
|
|
"ffmpeg", "-y",
|
|
"-i",
|
|
input
|
|
.getAbsolutePath(),
|
|
"-c:a",
|
|
"libvorbis",
|
|
"-q:a", "5",
|
|
output
|
|
.getAbsolutePath());
|
|
pb.redirectErrorStream(true);
|
|
Process p = pb.start();
|
|
p.waitFor();
|
|
return output.exists();
|
|
} catch (Exception e) {
|
|
logger.warning(
|
|
"[Radio] ffmpeg转换失败: "
|
|
+ input.getName());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private boolean checkFfmpeg() {
|
|
try {
|
|
Process p =
|
|
new ProcessBuilder(
|
|
"ffmpeg",
|
|
"-version")
|
|
.start();
|
|
boolean ok = p.waitFor() == 0;
|
|
if (ok) {
|
|
logger.info(
|
|
"[Radio] ffmpeg可用");
|
|
} else {
|
|
logger.info(
|
|
"[Radio] ffmpeg不可用"
|
|
+ " (仅支持ogg文件)");
|
|
}
|
|
return ok;
|
|
} catch (Exception e) {
|
|
logger.info(
|
|
"[Radio] ffmpeg未安装"
|
|
+ " (仅支持ogg文件)");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public boolean isFfmpegAvailable() {
|
|
return ffmpegAvailable;
|
|
}
|
|
}
|