File collision handling similar to windows
This commit is contained in:
parent
f9f52e4425
commit
9713b3c06e
@ -3,5 +3,7 @@
|
||||
<component name="JavaScriptSettings">
|
||||
<option name="languageLevel" value="ES6" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="1.8" project-jdk-type="JavaSDK" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_10" default="false" project-jdk-name="11" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
@ -15,8 +15,8 @@ import java.util.function.Predicate;
|
||||
* Converts a files or files in a folder to a web playable mp4.
|
||||
*/
|
||||
public class Main {
|
||||
private static final String FFPROBE_PATH = "ffprobe"; //Can be just ffprobe if it's in the path
|
||||
private static final String FFMPEG_PATH = "ffmpeg"; //Can be just ffmpeg if it's in the path
|
||||
private static final String FFPROBE_PATH = "C:\\Users\\Kristian\\Box Sync\\AutoIt\\FFMPEGConvert\\ffmpeg\\bin\\ffprobe"; //Can be just ffprobe if it's in the path
|
||||
private static final String FFMPEG_PATH = "C:\\Users\\Kristian\\Box Sync\\AutoIt\\FFMPEGConvert\\ffmpeg\\bin\\ffmpeg"; //Can be just ffmpeg if it's in the path
|
||||
private static Scanner in = new Scanner(System.in);
|
||||
private static Converter con = null;
|
||||
|
||||
@ -152,7 +152,7 @@ public class Main {
|
||||
* @param list The list to test against
|
||||
* @param predicate A predicate to use on every element in the list
|
||||
* @param <T> Anything which can be stored in a list
|
||||
* @return
|
||||
* @return True if at least one element fulfills the predicate
|
||||
*/
|
||||
private static <T> boolean listContains(T[] list, Predicate<T> predicate) {
|
||||
for (T item : list) {
|
||||
|
@ -43,19 +43,8 @@ public class AnimeConverter extends Converter {
|
||||
if (streams.length == 0) {
|
||||
throw new IllegalArgumentException("The file has no streams");
|
||||
}
|
||||
String noExt = file.getName().substring(0, file.getName().lastIndexOf('.'));
|
||||
String newPath = noExt + ".mp4";
|
||||
boolean isMP4 = newPath.equals(file.getName());
|
||||
if (isMP4) {
|
||||
newPath = noExt + ".tmp.mp4";
|
||||
}
|
||||
String newPath = fileCollisionPrevention(stripExtension(file) + ".mp4", "mp4");
|
||||
convertProcess(new ProcessBuilder(builderCommand(ffmpegPath, file.getName(), streams, newPath)), folder);
|
||||
if (isMP4) {
|
||||
File oldFile = new File(newPath);
|
||||
if (!oldFile.renameTo(new File(noExt + ".mp4"))) {
|
||||
System.out.println("Failed to move converted file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -115,6 +104,9 @@ public class AnimeConverter extends Converter {
|
||||
command.add("subtitles='" + fileName.replace("'", "\'") + "':si=" + subtitleStreams.get(0));
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
command.add("-map");
|
||||
command.add("0:" + listVideo(streams).get(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -46,6 +46,15 @@ public abstract class Converter {
|
||||
return stringBetween(output.toString(), "[STREAM]", "[/STREAM]");
|
||||
}
|
||||
|
||||
static String fileCollisionPrevention(String targetPath, String extension) {
|
||||
File file = new File(targetPath);
|
||||
int i = 1;
|
||||
while (file.exists()) {
|
||||
file = new File(stripExtension(file) + "(" + i + ")" + "." + extension);
|
||||
}
|
||||
return file.toString();
|
||||
}
|
||||
|
||||
static void convertProcess(ProcessBuilder process, File folder) throws IOException {
|
||||
System.out.println(process.command());
|
||||
process.directory(folder);
|
||||
|
@ -25,7 +25,7 @@ public class VideoConverter extends Converter {
|
||||
if (streams.length == 0) {
|
||||
throw new IllegalArgumentException("The file has no streams");
|
||||
}
|
||||
String newPath = stripExtension(file) + Math.random() + "." + newExt;
|
||||
String newPath = fileCollisionPrevention(stripExtension(file) + "." + newExt, newExt);
|
||||
convertProcess(new ProcessBuilder(builderCommand(ffmpegPath, file.getName(), streams, newPath)), folder);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user