biliob/src/main/java/com/yutou/bilibili/Tools/FFmpegUtils.java
2024-10-28 18:27:16 +08:00

77 lines
2.6 KiB
Java

package com.yutou.bilibili.Tools;
import com.yutou.common.utils.AppTools;
import com.yutou.common.utils.Log;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class FFmpegUtils {
private static final List<File> nowFFmpegList = new ArrayList<>();
public static int add(String ffmpeg, File file, File out) {
if (nowFFmpegList.contains(file)) {
return 2;
}
if (file.getName().contains("ffmpeg")) {
return 1;
} else {
startFFmpeg(ffmpeg, file, out);
}
return 0;
}
private synchronized static void startFFmpeg(String ffmpeg, File file, File out) {
if (nowFFmpegList.contains(file)) {
return;
}
nowFFmpegList.add(file);
new Thread(new Runnable() {
@Override
public void run() {
try {
if (!out.exists()) {
out.mkdirs();
}
String exec = String.format(
"%s -i \"%s\" -c:v copy -y \"%s\" ",
ffmpeg,
file.getAbsolutePath(),
out.getAbsolutePath() + File.separator + file.getName());
Log.i(exec);
Process process= ProcessUtils.exec(exec);
InputStream inputStream = process.getErrorStream();
byte[] bytes = new byte[1024];
while (inputStream.read(bytes) > -1) {
}
inputStream.close();
AppTools.copyFileToName(out.getAbsolutePath()+File.separator+file.getName(), file.getParent() + File.separator, file.getName().replace(".mp4", "_ffmpeg.mp4"), true);
file.delete();
new File(out.getAbsolutePath()+File.separator+file.getName()).delete();
} catch (Exception e) {
Log.e(e);
}
nowFFmpegList.remove(file);
}
}).start();
}
public static void main(String[] args) {
File file = new File("D:\\ieda\\bilibili\\live\\2021-03-20\\[2021-03-20 003537]1064046.mp4");
int i = -1;
while (i != 1) {
i = add("D:\\ffmpeg-4.3.1-2020-11-19-full_build\\bin\\ffmpeg.exe", file.getAbsoluteFile(), new File("ffmpeg_out"));
Log.i(i);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
Log.e(e);
}
}
Log.i("转码完成");
}
}