Fixes some bugs and adds unfinished command parser
This commit is contained in:
		@@ -15,12 +15,16 @@ 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 = "C:\\Users\\Kristian\\Nextcloud\\Programming\\AutoIt\\FFMPEGConvert\\ffmpeg\\bin\\ffprobe"; //Can be just ffprobe if it's in the path
 | 
			
		||||
    private static final String FFMPEG_PATH = "C:\\Users\\Kristian\\Nextcloud\\Programming\\AutoIt\\FFMPEGConvert\\ffmpeg\\bin\\ffmpeg"; //Can be just ffmpeg if it's in the path
 | 
			
		||||
    private static final String FFPROBE_PATH = "C:\\Users\\Kristian\\Downloads\\ffmpeg-20190427-8019395-win64-static\\bin\\ffprobe.exe"; //Can be just ffprobe if it's in the path
 | 
			
		||||
    private static final String FFMPEG_PATH = "C:\\Users\\Kristian\\Downloads\\ffmpeg-20190427-8019395-win64-static\\bin\\ffmpeg.exe"; //Can be just ffmpeg if it's in the path
 | 
			
		||||
    private static Scanner in = new Scanner(System.in);
 | 
			
		||||
    private static Converter con = null;
 | 
			
		||||
 | 
			
		||||
    public static void main(String[] args) throws IOException {
 | 
			
		||||
        //System.out.println(tokenizer("AnimeConverter -audiolang jap,eng -sublang eng,nor,* \"C:\\Users\\Kristian\\Downloads\\Anime\\[Kametsu] ERASED (BD 1080p Hi10 FLAC)\""));
 | 
			
		||||
        //parser(tokenizer("AnimeConverter \"C:\\Users\\Kristian\\Downloads\\Anime\\[Kametsu] ERASED (BD 1080p Hi10 FLAC)\""));
 | 
			
		||||
        //System.exit(1);
 | 
			
		||||
 | 
			
		||||
        int choice = getChoice("Which converter do you want do use?\n1. Anime to web mp4\n2. Audio converter\n3. Video converter", 1, 3);
 | 
			
		||||
 | 
			
		||||
        System.out.println("Input for this converter:");
 | 
			
		||||
@@ -69,6 +73,111 @@ public class Main {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private enum converterArgumentValueType {
 | 
			
		||||
        BOOLEAN,
 | 
			
		||||
        COMMA_SEPARATED_LIST,
 | 
			
		||||
        SINGLE_VALUE,
 | 
			
		||||
        INT
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static class converterArgument {
 | 
			
		||||
        private String name;
 | 
			
		||||
        private boolean valueRequired;
 | 
			
		||||
        private converterArgumentValueType valueType;
 | 
			
		||||
        private converterArgument(String name, boolean valueRequired, converterArgumentValueType valueType) {
 | 
			
		||||
            this.name = name;
 | 
			
		||||
            this.valueRequired = valueRequired;
 | 
			
		||||
            this.valueType = valueType;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private boolean testArgumentValue(String value) {
 | 
			
		||||
            if (value.length() == 0) {
 | 
			
		||||
                return !valueRequired;
 | 
			
		||||
            }
 | 
			
		||||
            if (valueRequired && value.startsWith("-")) {
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
            switch (valueType) {
 | 
			
		||||
                case BOOLEAN:
 | 
			
		||||
                    String lower = value.toLowerCase();
 | 
			
		||||
                    return lower.equals("true") || lower.equals("false");
 | 
			
		||||
                case COMMA_SEPARATED_LIST:
 | 
			
		||||
                    return !value.contains(" ");
 | 
			
		||||
                case SINGLE_VALUE:
 | 
			
		||||
                    return !value.contains(" ");
 | 
			
		||||
                case INT:
 | 
			
		||||
                    Integer.parseInt(value);
 | 
			
		||||
                    return true;
 | 
			
		||||
            }
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static void parser(List<String> tokens) {
 | 
			
		||||
        String[] types = {"animeconverter", "audioconverter", "videoconverter"};
 | 
			
		||||
        converterArgument[] commonArgs = {
 | 
			
		||||
                new converterArgument("-recursions", true, converterArgumentValueType.INT)
 | 
			
		||||
        };
 | 
			
		||||
        converterArgument[] animeArgs = {
 | 
			
		||||
 | 
			
		||||
        };
 | 
			
		||||
        converterArgument[] audioArgs = {
 | 
			
		||||
                new converterArgument("-outext", true, converterArgumentValueType.SINGLE_VALUE)
 | 
			
		||||
        };
 | 
			
		||||
        converterArgument[] videoArgs = {
 | 
			
		||||
                new converterArgument("-outext", true, converterArgumentValueType.SINGLE_VALUE)
 | 
			
		||||
        };
 | 
			
		||||
        String type = tokens.get(0).toLowerCase();
 | 
			
		||||
        if (!listContains(types, s -> s.equals(type))) {
 | 
			
		||||
            throw new IllegalArgumentException("Unknown converter type chosen.");
 | 
			
		||||
        }
 | 
			
		||||
        if (tokens.size() < 2) {
 | 
			
		||||
            throw new IllegalArgumentException("No file/folder path in argument.");
 | 
			
		||||
        }
 | 
			
		||||
        for (int i = 1; i < tokens.size() - 1; i++) {
 | 
			
		||||
            //TODO: Find the type of argument and check the value
 | 
			
		||||
            //TODO: Find an executable way to represent the chain of commands parsed
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static List<String> tokenizer(String input) {
 | 
			
		||||
        List<String> tokens = new ArrayList<>();
 | 
			
		||||
        Boolean startedQuote = false;
 | 
			
		||||
        StringBuilder currentToken = new StringBuilder();
 | 
			
		||||
        for (int i = 0; i < input.length(); i++) {
 | 
			
		||||
            char c = input.charAt(i);
 | 
			
		||||
            if (c == ' ') {
 | 
			
		||||
                if (!startedQuote) {
 | 
			
		||||
                    if (!currentToken.toString().trim().equals("")) {
 | 
			
		||||
                        tokens.add(currentToken.toString());
 | 
			
		||||
                        currentToken = new StringBuilder();
 | 
			
		||||
                    } else {
 | 
			
		||||
                        currentToken = new StringBuilder();
 | 
			
		||||
                    }
 | 
			
		||||
                } else {
 | 
			
		||||
                    currentToken.append(c);
 | 
			
		||||
                }
 | 
			
		||||
            } else if (c == '"') {
 | 
			
		||||
                if (startedQuote) {
 | 
			
		||||
                    if (!currentToken.toString().trim().equals("")) {
 | 
			
		||||
                        tokens.add(currentToken.toString());
 | 
			
		||||
                        currentToken = new StringBuilder();
 | 
			
		||||
                    }
 | 
			
		||||
                    startedQuote = false;
 | 
			
		||||
                } else {
 | 
			
		||||
                    startedQuote = true;
 | 
			
		||||
                    currentToken = new StringBuilder();
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                currentToken.append(c);
 | 
			
		||||
                if (i == input.length() - 1) {
 | 
			
		||||
                    tokens.add(currentToken.toString());
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return tokens;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static void animeConverter() {
 | 
			
		||||
        System.out.println("[Audio languages jap,eng,ger,fre] [Subtitle languages eng,ger,fre] [Convert to stereo if necessary true/false] [Prevent signs&songs subtitles true/false]\nYour input: ");
 | 
			
		||||
        List<String> input = readInput(4);
 | 
			
		||||
 
 | 
			
		||||
@@ -9,6 +9,7 @@ public class AnimeConverter extends Converter {
 | 
			
		||||
    private String[] subtitleLang;
 | 
			
		||||
    private boolean toStereo;
 | 
			
		||||
    private boolean preventSignsAndSongs;
 | 
			
		||||
    private boolean debug = false;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param ffprobePath           Path/command to ffprobe
 | 
			
		||||
@@ -58,6 +59,11 @@ public class AnimeConverter extends Converter {
 | 
			
		||||
     */
 | 
			
		||||
    private String[] builderCommand(String executable, String fileName, String[] streams, String outFile) {
 | 
			
		||||
        List<String> command = ffmpegWebVideo(executable, fileName);
 | 
			
		||||
 | 
			
		||||
        if (this.debug) {
 | 
			
		||||
            addDebug(command, 50, 120);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        for (String lang : audioLang) {
 | 
			
		||||
            List<Integer> audioStreams = listAudio(streams, lang);
 | 
			
		||||
            if (audioStreams.size() > 0) {
 | 
			
		||||
@@ -84,21 +90,28 @@ public class AnimeConverter extends Converter {
 | 
			
		||||
 | 
			
		||||
    private void addSubtitles(String[] streams, List<Integer> videoStreams, List<String> command, String fileName) {
 | 
			
		||||
        for (String lang : subtitleLang) {
 | 
			
		||||
            List<Integer> subtitleStreams = listSubtitles(streams, lang);
 | 
			
		||||
            if (subtitleStreams.size() > 0 && videoStreams.size() > 0 && isImageSub(streams, subtitleStreams.get(0))) {
 | 
			
		||||
            List<Integer> subtitleStreams;
 | 
			
		||||
            List<Integer> subtitleStreamsAbsolute;
 | 
			
		||||
            if (lang.equals("*")) {
 | 
			
		||||
                subtitleStreams = listSubtitlesRelative(streams);
 | 
			
		||||
                subtitleStreamsAbsolute = listSubtitles(streams);
 | 
			
		||||
            } else if (preventSignsAndSongs) {
 | 
			
		||||
                subtitleStreams = listSubtitlesRelative(streams, lang, new String[]{"title=Signs"});
 | 
			
		||||
                subtitleStreamsAbsolute = listSubtitles(streams, lang, new String[]{"title=Signs"});
 | 
			
		||||
            } else {
 | 
			
		||||
                subtitleStreams = listSubtitlesRelative(streams, lang);
 | 
			
		||||
                subtitleStreamsAbsolute = listSubtitles(streams, lang);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (subtitleStreams.size() > 0 && videoStreams.size() > 0 && isImageSub(streams, subtitleStreamsAbsolute.get(0))) {
 | 
			
		||||
                command.add("-filter_complex");
 | 
			
		||||
                command.add("[0:v:" + listVideo(streams).get(0) + "][0:" + subtitleStreams.get(0) + "]overlay");
 | 
			
		||||
                command.add("[0:v:" + listVideo(streams).get(0) + "][0:" + subtitleStreamsAbsolute.get(0) + "]overlay");
 | 
			
		||||
                break;
 | 
			
		||||
            } else if (subtitleStreams.size() > 0) {
 | 
			
		||||
                if (videoStreams.size() > 0) {
 | 
			
		||||
                    command.add("-map");
 | 
			
		||||
                    command.add("0:" + listVideo(streams).get(0));
 | 
			
		||||
                }
 | 
			
		||||
                if (preventSignsAndSongs) {
 | 
			
		||||
                    subtitleStreams = listSubtitlesRelative(streams, lang, new String[]{"title=Signs"});
 | 
			
		||||
                } else {
 | 
			
		||||
                    subtitleStreams = listSubtitlesRelative(streams, lang);
 | 
			
		||||
                }
 | 
			
		||||
                if (subtitleStreams.size() > 0) {
 | 
			
		||||
                    command.add("-vf");
 | 
			
		||||
                    command.add("subtitles='" + fileName.replace("'", "\'") + "':si=" + subtitleStreams.get(0));
 | 
			
		||||
 
 | 
			
		||||
@@ -162,6 +162,31 @@ public abstract class Converter {
 | 
			
		||||
        return listIndexes(list, (string) -> string.contains("codec_type=audio") && string.contains("language=" + lang));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Lists all subtitle streams of a certain language relatively to the number of subtitle streams.
 | 
			
		||||
     * 0-based.
 | 
			
		||||
     * Filters out all indexes containing anything in illegal
 | 
			
		||||
     *
 | 
			
		||||
     * @param list      A list of ffprobe indexes
 | 
			
		||||
     * @param lang      The wanted language
 | 
			
		||||
     * @param illegal   A list of strings not to allow (Songs & Signs for example)
 | 
			
		||||
     * @return          An integer list containing just the wanted indexes
 | 
			
		||||
     */
 | 
			
		||||
    static List<Integer> listSubtitles(String[] list, String lang, String[] illegal) {
 | 
			
		||||
        List<Integer> subtitles = listIndexes(list, (string) -> string.contains("codec_type=subtitle")
 | 
			
		||||
                && string.contains("language=" + lang));
 | 
			
		||||
        List<Integer> notWanted = listIndexes(list, (string) -> {
 | 
			
		||||
            for (String s : illegal) {
 | 
			
		||||
                if (string.contains(s)) {
 | 
			
		||||
                    return true;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            return false;
 | 
			
		||||
        });
 | 
			
		||||
        subtitles.removeAll(notWanted);
 | 
			
		||||
        return subtitles;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Lists all subtitle streams of a certain language.
 | 
			
		||||
     *
 | 
			
		||||
@@ -227,6 +252,22 @@ public abstract class Converter {
 | 
			
		||||
        return wanted;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Lists all subtitle streams relatively to the number of subtitle streams.
 | 
			
		||||
     * 0-based.
 | 
			
		||||
     *
 | 
			
		||||
     * @param list  A list of ffprobe indexes
 | 
			
		||||
     * @return      An integer list containing just the wanted indexes
 | 
			
		||||
     */
 | 
			
		||||
    static List<Integer> listSubtitlesRelative(String[] list) {
 | 
			
		||||
        list = subList(list, (string) -> string.contains("codec_type=subtitle"));
 | 
			
		||||
        List<Integer> wanted = new ArrayList<>();
 | 
			
		||||
        for (int i = 0; i < list.length; i++) {
 | 
			
		||||
                wanted.add(i);
 | 
			
		||||
        }
 | 
			
		||||
        return wanted;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Lists all indexes fulfilling a predicate.
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user