wert für progress auf 100% setzen

NilsSteffens

Neues Mitglied
Hallo, ich habe einen Prozess der einen gewissen Zeitraum dauert (video converter ffmpeg) ich habe eine art Progressbar in meiner Konsole (ohne gui) und möchte den Endwert als 100% festlegen. Da dieser Wert immer individuell ist, (je nach video also andere progressdauer) habe ich das problem dass ich nicht weis wie genau ich diesen individuellen Endwert festlegen soll

hier die "Progressbar"

Java:
import java.util.Collections;
import java.util.concurrent.TimeUnit;

public class progress {
    public static void main(String[] args) {
        //dauer des Prozess
        long total = 3357;
        long startTime = System.currentTimeMillis();

        for (int i = 1; i <= total; i = i + 3) {
       
            try {
                Thread.sleep(50);
                printProgress(startTime, total, i);
            } catch (InterruptedException e) {
            }
        }
    }


    private static void printProgress(long startTime, long total, long current) {
        long eta = current == 0 ? 0 :
            (total - current) * (System.currentTimeMillis() - startTime) / current;

        String etaHms = current == 0 ? "N/A" :
                String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(eta),
                        TimeUnit.MILLISECONDS.toMinutes(eta) % TimeUnit.HOURS.toMinutes(1),
                        TimeUnit.MILLISECONDS.toSeconds(eta) % TimeUnit.MINUTES.toSeconds(1));

        StringBuilder string = new StringBuilder(140);  
        int percent = (int) (current * 100 / total);
        string
            .append('\r')
            .append(String.join("", Collections.nCopies(percent == 0 ? 2 : 2 - (int) (Math.log10(percent)), " ")))
            .append(String.format(" %d%% [", percent))
            .append(String.join("", Collections.nCopies(percent, "=")))
            .append('>')
            .append(String.join("", Collections.nCopies(100 - percent, " ")))
            .append(']')
            .append(String.join("", Collections.nCopies((int) (Math.log10(total)) - (int) (Math.log10(current)), " ")))
            .append(String.format(" ETA: %s",  etaHms));
        System.out.print(string);
    }
}
 
Du kannst nichts abschätzen, wenn du nicht weißt wie lange ein Prozess dauert. Vielleicht schaust du nach, ob wer für ffmpeg eine Java API gebaut hat und der dir solche Werte liefern kann.
 
Was mir noch einfallen würde, was aber nicht ganz deiner Vorstellung entspricht, ist dass die Arbeitsschritte zählst.

Schritt (1/4) - Lade Video
Schritt (2/4) - Bearbeite Video
Schritt (3/4) - mach irgendwas anderes
Schritt (4/4) - mach fertig

Die Anzahl dieser müsstest du genau wissen und kannst du super in Prozent darstellen.

Wenn du es ganz Wacklig machen möchtest, kannst du auch die Zeit Stoppen, die er für 1% braucht und dann auf 100% hoch rechnen und wenn er bei 2% ist das gleiche wieder. Hat dann aber sehr starke Zeitsprünge - Kannst du gut am File Explorer von Windows sehen, wenn du Daten überträgst. Laut ihm dauerern auch manche übertragungen ewig obwohl er dann doch nur eine Minute braucht (Ich meine zumindest, dass er so funktioniert)

LG
 
Habe deinen Code mal rüber kopiert, du hast ein zwei kleine Formatierrungsfehler drinnen
Einmal du kommst mit diesem "Progressbar" nicht immer auf 100% bei Fertigstellung und immer wenn du eine Stelle weiter gehst mit der Prozentzahl (1,10,100) verrruscht dein ETA.

Hier mal das Ergebnis bei Progress -> main(String[]) -> total = 50

Code:
   2% [==>                                                                                                  ]  ETA: 00:00:02
   8% [========>                                                                                            ]  ETA: 00:00:01
  14% [==============>                                                                                      ]  ETA: 00:00:01
  20% [====================>                                                                                ] ETA: 00:00:00
  26% [==========================>                                                                          ] ETA: 00:00:00
  32% [================================>                                                                    ] ETA: 00:00:00
  38% [======================================>                                                              ] ETA: 00:00:00
  44% [============================================>                                                        ] ETA: 00:00:00
  50% [==================================================>                                                  ] ETA: 00:00:00
  56% [========================================================>                                            ] ETA: 00:00:00
  62% [==============================================================>                                      ] ETA: 00:00:00
  68% [====================================================================>                                ] ETA: 00:00:00
  74% [==========================================================================>                          ] ETA: 00:00:00
  80% [================================================================================>                    ] ETA: 00:00:00
  86% [======================================================================================>              ] ETA: 00:00:00
  92% [============================================================================================>        ] ETA: 00:00:00
  98% [==================================================================================================>  ] ETA: 00:00:00
 
erst einmal danke für die schnellen antworten 🙂

nur leider hilft mir das alles nicht wirklich 🙁
ich bin vor drei tagen erst in java eingestiegen (Praktikum bei einem IT unternehmen)

um meine jetzige situation noch einmal zu schildern:
ich habe den progress soweit berechnet und muss anhand der frames die bereits berechnet worden sind die Prozentzahl angeben und den passenden timer dazu.
progress läuft soweit (das mit den 99% übrigens auch)

hier die runtime,

Java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;




public class Runtime {

    public void execute(String[] command) throws IOException {
       
       

        ProcessBuilder pb = new ProcessBuilder(command);
        Process process = pb.start();

        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(process.getErrorStream(), Charset.forName("UTF-8")));
        String line;
        int seconds = -1;
        double fps = -1;
        double durationInFrames = -1;
        while ((line = bufferedReader.readLine()) != null) {

            try {
                String current;
                if (durationInFrames == -1) {
                    if ((current = line.trim()).startsWith("Duration:")) {
                        String durationS = current.substring(10, current.indexOf(","));
                        System.err.println(durationS);
                        String[] arr = durationS.split(":");
                        seconds = (Integer.parseInt(arr[0]) * 3600) + (Integer.parseInt(arr[1]) * 60)
                                + Integer.parseInt(arr[2].substring(0, 2));
                        System.out.println(seconds);
                    } else if (current.contains("Video:") && current.contains("fps,")) {
                        if (seconds == -1)
                            throw new Exception("No duration found when parsing fps");
                        int startBoundery = current.indexOf("fps,") - 8;
                        System.out.println(current.substring(startBoundery));
                        int endBoundery = current.indexOf(",", startBoundery);
                        int endString = current.indexOf("fps,");
                        String fpsS = current.substring(endBoundery + 1, endString).trim();
                        fps = Double.parseDouble(fpsS);
                        durationInFrames = seconds * fps;
                       
                       

                       
                        double d = durationInFrames;
                        long duration = Math.round(d);
                        System.out.println("durationtime:" + duration);
                       
                        progress progress= new progress();
                        progress.progressbar(duration);
                       
                   
                    }
                }else{
                   
                   
                }

            } catch (Exception e) {
                e.printStackTrace();
                process.destroyForcibly();
            }

        }

    }

    }


Code:
import java.util.Collections;
import java.util.concurrent.TimeUnit;

public class progress {
   
    public void progressbar(long progress){   
        //dauer des Prozess
       
        long total = progress;
        long startTime = System.currentTimeMillis();
       
        for (int i = 1; i <= total; i = i + 3) {
       
            try {
                Thread.sleep(50);
                printProgress(startTime, total, i);
            } catch (InterruptedException e) {
            }
        }
    }

        //zeit
   
    private static void printProgress(long startTime, long total, long current) {
        long eta = current == 0 ? 0 :
            (total - current) * (System.currentTimeMillis() - startTime) / current;

        String etaHms = current == 0 ? "N/A" :
                String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(eta),
                        TimeUnit.MILLISECONDS.toMinutes(eta) % TimeUnit.HOURS.toMinutes(1),
                        TimeUnit.MILLISECONDS.toSeconds(eta) % TimeUnit.MINUTES.toSeconds(1));

        StringBuilder string = new StringBuilder(140);  
        int percent = (int) (current * 101 / total);
        string
            .append('\r')
            .append(String.join("", Collections.nCopies(percent == 0 ? 2 : 2 - (int) (Math.log10(percent)), " ")))
            .append(String.format(" %d%% [", percent))
            .append(String.join("", Collections.nCopies(percent, "=")))
            .append('>')
            .append(String.join("", Collections.nCopies(100 - percent, " ")))
            .append(']')
            .append(String.join("", Collections.nCopies((int) (Math.log10(total)) - (int) (Math.log10(current)), " ")))
            .append(String.format(" ETA: %s",  etaHms));
        System.out.print(string);
    }
}
 
Jedes Frame in dem ffmpeg-Video format hat binäre Startsequenzen du kannst diese zählen oder du schaust, ob du eine FFMPEG fähige libary findest und kannst quasi getTotalFrames() aufrufen und bekommst diese dann.

Was willst du denn mit dem process.getErrorStream() machen? Liefert er dir irgendwas wichtiges?

Ich meine JCodec kann das bin mir aber unsicher. http://jcodec.org/
Aber so wie ich die Sache sehe bleibt dir nichts anderes übrig, als zu schauen wie viel Zeit du für ein Frame brauchst und rechnest dann hoch. Da du ja nicht weißt wie lange ein Frame braucht kannst du auch nicht genau bestimmen wann es fertig sein wird.

LG
 
Zuletzt bearbeitet:

Zurück
Oben