File collision handling similar to windows
This commit is contained in:
		@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user