Adds handling of external subtitles for anime
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				KnarCraft/FFmpegConvert/pipeline/head This commit looks good
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	KnarCraft/FFmpegConvert/pipeline/head This commit looks good
				
			This commit is contained in:
		@@ -57,8 +57,10 @@ public class AnimeConverter extends Converter {
 | 
			
		||||
        }
 | 
			
		||||
        String newPath = fileCollisionPrevention(folder.getAbsolutePath() + File.separator +
 | 
			
		||||
                stripExtension(file) + ".mp4", "mp4");
 | 
			
		||||
        printl();
 | 
			
		||||
        printl("Preparing to start process...");
 | 
			
		||||
        String[] command = builderCommand(ffmpegPath, file.getName(), streams, newPath);
 | 
			
		||||
        printl("Converting " + file);
 | 
			
		||||
        String[] command = builderCommand(ffmpegPath, file.getName(), streams, newPath, file);
 | 
			
		||||
        ProcessBuilder processBuilder = new ProcessBuilder(command);
 | 
			
		||||
        convertProcess(processBuilder, folder);
 | 
			
		||||
    }
 | 
			
		||||
@@ -71,7 +73,8 @@ public class AnimeConverter extends Converter {
 | 
			
		||||
     * @param outFile <p>The output file.</p>
 | 
			
		||||
     * @return <p>A list of commands</p>
 | 
			
		||||
     */
 | 
			
		||||
    private String[] builderCommand(String executable, String fileName, List<StreamObject> streams, String outFile) {
 | 
			
		||||
    private String[] builderCommand(String executable, String fileName, List<StreamObject> streams, String outFile,
 | 
			
		||||
                                    File file) {
 | 
			
		||||
        List<String> command = ffmpegWebVideo(executable, fileName);
 | 
			
		||||
 | 
			
		||||
        if (this.DEBUG) {
 | 
			
		||||
@@ -100,7 +103,7 @@ public class AnimeConverter extends Converter {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        addAudioStreams(command, audioStream);
 | 
			
		||||
        addSubtitles(command, subtitleStream, videoStream, fileName);
 | 
			
		||||
        addSubtitles(command, subtitleStream, videoStream, fileName, file);
 | 
			
		||||
 | 
			
		||||
        command.add(outFile);
 | 
			
		||||
        return command.toArray(new String[0]);
 | 
			
		||||
@@ -128,9 +131,13 @@ public class AnimeConverter extends Converter {
 | 
			
		||||
     * @param subtitleStream <p>The subtitle stream to be used.</p>
 | 
			
		||||
     * @param videoStream <p>The video stream to be used.</p>
 | 
			
		||||
     * @param fileName <p>The name of the file which is converted.</p>
 | 
			
		||||
     * @param file <p>The file to convert.</p>
 | 
			
		||||
     */
 | 
			
		||||
    private void addSubtitles(List<String> command, SubtitleStream subtitleStream, VideoStream videoStream,
 | 
			
		||||
                              String fileName) {
 | 
			
		||||
                              String fileName, File file) {
 | 
			
		||||
        File folder = file.getParentFile();
 | 
			
		||||
        String externalImageSubtitle = hasExternalImageSubtitle(folder.getAbsolutePath(), fileName);
 | 
			
		||||
        String externalSubtitle = hasExternalSubtitle(folder.getAbsolutePath(), fileName);
 | 
			
		||||
        if (subtitleStream != null && subtitleStream.getIsImageSubtitle()) {
 | 
			
		||||
            command.add("-filter_complex");
 | 
			
		||||
            String filter = String.format("[0:v:%d][0:%d]overlay", videoStream.getAbsoluteIndex(),
 | 
			
		||||
@@ -144,6 +151,20 @@ public class AnimeConverter extends Converter {
 | 
			
		||||
            String subtitleCommand = String.format("subtitles='%s':si=%d", safeFileName,
 | 
			
		||||
                    subtitleStream.getRelativeIndex());
 | 
			
		||||
            command.add(subtitleCommand);
 | 
			
		||||
        } else if (!externalSubtitle.equals("")) {
 | 
			
		||||
            command.add("-map");
 | 
			
		||||
            command.add(String.format("0:%d", videoStream.getAbsoluteIndex()));
 | 
			
		||||
            command.add("-vf");
 | 
			
		||||
            String subtitleCommand = String.format("subtitles='%s'", escapeSpecialCharactersInFileName(externalSubtitle));
 | 
			
		||||
            command.add(subtitleCommand);
 | 
			
		||||
        } else if (!externalImageSubtitle.equals("")) {
 | 
			
		||||
                command.add("-i");
 | 
			
		||||
                command.add(stripExtension(fileName) + externalImageSubtitle);
 | 
			
		||||
                command.add("-filter_complex");
 | 
			
		||||
                command.add(String.format("[1:s]scale=width=1272:height=720,crop=w=1272:h=720:x=0:y=out_h[sub];[%d:v]" +
 | 
			
		||||
                                "[sub]overlay", videoStream.getAbsoluteIndex()));
 | 
			
		||||
                command.add("-profile:v");
 | 
			
		||||
                command.add("baseline");
 | 
			
		||||
        } else {
 | 
			
		||||
            command.add("-map");
 | 
			
		||||
            command.add(String.format("0:%d", videoStream.getAbsoluteIndex()));
 | 
			
		||||
 
 | 
			
		||||
@@ -452,7 +452,7 @@ public abstract class Converter {
 | 
			
		||||
     * @param input <p>The text to print.</p>
 | 
			
		||||
     * @throws IOException <p>If a write is not possible.</p>
 | 
			
		||||
     */
 | 
			
		||||
    static void print(String input) throws IOException {
 | 
			
		||||
    private static void print(String input) throws IOException {
 | 
			
		||||
        if (!input.equals("")) {
 | 
			
		||||
            writer.write(input);
 | 
			
		||||
            writer.flush();
 | 
			
		||||
@@ -479,4 +479,37 @@ public abstract class Converter {
 | 
			
		||||
        writer.newLine();
 | 
			
		||||
        writer.flush();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks whether there exists an external image subtitle with the same filename as the file
 | 
			
		||||
     * @param directory <p>The directory containing the file.</p>
 | 
			
		||||
     * @param file <p>The file to be converted.</p>
 | 
			
		||||
     * @return <p>The extension of the subtitle or empty if no subtitle was found.</p>
 | 
			
		||||
     */
 | 
			
		||||
    String hasExternalImageSubtitle(String directory, String file) {
 | 
			
		||||
        String path = stripExtension(file);
 | 
			
		||||
        for (String subtitleExtension : new String[] {".idx", ".sub"}) {
 | 
			
		||||
            if (new File(directory + File.separator + path + subtitleExtension).exists()) {
 | 
			
		||||
                return path + subtitleExtension;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return "";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks whether there exists an external subtitle with the same filename as the file
 | 
			
		||||
     * @param directory <p>The directory containing the file.</p>
 | 
			
		||||
     * @param file <p>The file to be converted.</p>
 | 
			
		||||
     * @return <p>The extension of the subtitle or empty if no subtitle was found.</p>
 | 
			
		||||
     */
 | 
			
		||||
    String hasExternalSubtitle(String directory, String file) {
 | 
			
		||||
        String path = stripExtension(file);
 | 
			
		||||
        for (String subtitleExtension : new String[] {".srt", ".ass"}) {
 | 
			
		||||
            System.out.println(directory + File.separator + path + subtitleExtension);
 | 
			
		||||
            if (new File(directory + File.separator + path + subtitleExtension).exists()) {
 | 
			
		||||
                return path + subtitleExtension;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return "";
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -95,10 +95,10 @@ public class VideoConverter extends Converter {
 | 
			
		||||
        String externalImageSubtitle = hasExternalImageSubtitle(folder.getAbsolutePath(), fileName);
 | 
			
		||||
        if (!externalSubtitle.equals("")) {
 | 
			
		||||
            command.add("-vf");
 | 
			
		||||
            command.add("subtitles=" + stripExtension(fileName) + externalSubtitle);
 | 
			
		||||
            command.add("subtitles=" + externalSubtitle);
 | 
			
		||||
        } else if (!externalImageSubtitle.equals("")) {
 | 
			
		||||
            command.add("-i");
 | 
			
		||||
            command.add(stripExtension(fileName) + externalImageSubtitle);
 | 
			
		||||
            command.add(externalImageSubtitle);
 | 
			
		||||
            if (this.DEBUG) {
 | 
			
		||||
                addDebug(command, 50, 120);
 | 
			
		||||
            }
 | 
			
		||||
@@ -122,38 +122,6 @@ public class VideoConverter extends Converter {
 | 
			
		||||
        command.add("pan=stereo|FL < 1.0*FL + 0.707*FC + 0.707*BL|FR < 1.0*FR + 0.707*FC + 0.707*BR");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks whether there exists an external image subtitle with the same filename as the file
 | 
			
		||||
     * @param directory <p>The directory containing the file.</p>
 | 
			
		||||
     * @param file <p>The file to be converted.</p>
 | 
			
		||||
     * @return <p>The extension of the subtitle or empty if no subtitle was found.</p>
 | 
			
		||||
     */
 | 
			
		||||
    private String hasExternalImageSubtitle(String directory, String file) {
 | 
			
		||||
        String path = stripExtension(file);
 | 
			
		||||
        for (String subtitleExtension : new String[] {".idx", ".sub"}) {
 | 
			
		||||
            if (new File(directory + File.separator + path + subtitleExtension).exists()) {
 | 
			
		||||
                return subtitleExtension;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return "";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks whether there exists an external subtitle with the same filename as the file
 | 
			
		||||
     * @param directory <p>The directory containing the file.</p>
 | 
			
		||||
     * @param file <p>The file to be converted.</p>
 | 
			
		||||
     * @return <p>The extension of the subtitle or empty if no subtitle was found.</p>
 | 
			
		||||
     */
 | 
			
		||||
    private String hasExternalSubtitle(String directory, String file) {
 | 
			
		||||
        String path = stripExtension(file);
 | 
			
		||||
        for (String subtitleExtension : new String[] {".srt", ".ass"}) {
 | 
			
		||||
            if (new File(directory + File.separator + path + subtitleExtension).exists()) {
 | 
			
		||||
                return subtitleExtension;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return "";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String[] getValidFormats() {
 | 
			
		||||
        return VIDEO_FORMATS;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user