ZeNyfh / gigavibe-java-edition

Music + Media bot made in java using JDA and Lavaplayer.
GNU General Public License v3.0
9 stars 3 forks source link

transfer new code to videodl #111

Closed ZeNyfh closed 1 year ago

ZeNyfh commented 1 year ago

Here is the code from my desktop utility VideoCompressor.java, it just needs to be implemented into VideoDL

String inputFile = args[0];
String outputFile = args[1];
int crf = 20;
int bitrate = 1024;
int attempt = 0;

try {
    // ffprobe to get res
    String ffprobeCommand = "ffprobe -v error -show_entries stream=width,height -of default=noprint_wrappers=1:nokey=1 " + inputFile;
    Process ffprobe = Runtime.getRuntime().exec(ffprobeCommand);
    BufferedReader ffprobeInput = new BufferedReader(new InputStreamReader(ffprobe.getInputStream()));
    String videoWidth = ffprobeInput.readLine();
    String videoHeight = ffprobeInput.readLine();

    // change res to half
    int scaleWidth = Integer.parseInt(videoWidth);
    int scaleHeight = Integer.parseInt(videoHeight);
    String scale = scaleWidth + ":" + scaleHeight;
    // compression
    int numThreads = Runtime.getRuntime().availableProcessors();
    String command = "";
    long time = System.currentTimeMillis();
    Process p;

    // duplicate file
    FileInputStream inputStream = new FileInputStream(inputFile);
    FileOutputStream outputStream = new FileOutputStream(outputFile);
    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }
    inputStream.close();
    outputStream.close();
    System.out.println(new File(outputFile).length());

    // check filesize
    File output = new File(outputFile);
    while (output.length() > 25600000) {
        attempt++;
        crf += 4;
        bitrate -= 128;
        command = "ffmpeg -nostdin -loglevel error -y -i " + inputFile + " -c:v libx264 -crf " + crf + " -b:a 55k -c:a aac -b:v " + bitrate + "k -vf scale=" + scale + " -threads " + numThreads + " " + outputFile;
        p = Runtime.getRuntime().exec(command);
        // cancel current process if output file size exceeds 25600000 bytes
        while (output.length() > 25600000 || attempt == 1) {
            p.destroy(); // cancel current process
            output.delete(); // delete current output file
            System.out.println("process cancelled and output file deleted");

            // retry with new settings
            attempt++;
            crf += 4;
            bitrate -= 128;
            command = "ffmpeg -nostdin -loglevel error -y -i " + inputFile + " -c:v libx264 -crf " + crf + " -b:a 55k -c:a aac -b:v " + bitrate + "k -vf scale=" + scale + " -threads " + numThreads + " " + outputFile;
            p = Runtime.getRuntime().exec(command);
            output = new File(outputFile);
            System.out.println("made new output");
            System.out.println(output.length());
        }

        p.waitFor();
        output = new File(outputFile);
        System.out.println("output size: " + output.length());
    }
    System.out.println((System.currentTimeMillis()-time)/1000 + " seconds");
    System.out.println("done");
9382 commented 1 year ago

See e9967fa