Search in sources :

Example 1 with PercentPart

use of org.fagu.fmv.textprogressbar.part.PercentPart in project fmv by f-agu.

the class Ripper method rip.

/**
 * @throws IOException
 */
public void rip() throws IOException {
    AppVersion.logMyVersion(logger::log);
    logger.log("===========================================================");
    String msg = "Analyzing DVD on " + dvdDrive + "...";
    logger.log(msg);
    String name = null;
    try (TextProgressBar bar = TextProgressBar.newBar().appendText(msg + "   ").append(new SpinnerPart()).buildAndSchedule()) {
        name = dvdName.nameOf(dvdDrive, logger);
    }
    displayAndLog("Name: " + name);
    msg = "Scanning titles...";
    logger.log(msg);
    MPlayerTitles mPlayerTitles = null;
    try (TextProgressBar bar = TextProgressBar.newBar().appendText(msg + "   ").append(new SpinnerPart()).buildAndSchedule()) {
        mPlayerTitles = titlesExtractor.extract(dvdDrive, logger);
    }
    Collection<MPlayerTitle> titles = selectTitlesPolicy.select(mPlayerTitles.getTitles());
    displayAndLog("Found " + titles.size() + " titles:");
    titles.forEach(t -> displayAndLog("   " + t.getNum() + "/" + t.getLength()));
    displayAndLog("Write to " + tmpDirectory);
    AtomicInteger currentTitle = new AtomicInteger();
    AtomicInteger currentEncoding = new AtomicInteger();
    final int prefixWidth = 30;
    int nbProgresses = titles.size() * 2;
    List<AtomicInteger> progressList = new ArrayList<>(nbProgresses);
    titles.forEach(t -> {
        // dump
        progressList.add(new AtomicInteger());
        // encode
        progressList.add(new AtomicInteger());
    });
    final String finalName = name;
    TextProgressBarBuilder builder = TextProgressBar.newBar().fixWidth(32).withText(StringUtils.abbreviate(finalName, prefixWidth) + " ");
    if (titles.size() == 1) {
        builder.fixWidth(20).with(s -> currentTitle.get() == 1 ? "reading DVD..." : currentEncoding.get() == 1 ? "encoding..." : "");
    } else {
        builder.fixWidth(19).with(s -> currentTitle.get() > 0 ? "reading DVD: " + currentTitle.get() + "/" + titles.size() : "");
        builder.fixWidth(15).with(s -> currentEncoding.get() > 0 ? "encoding: " + currentEncoding.get() + "/" + titles.size() : "");
    }
    textProgressBar = builder.append(ProgressPart.width(31).build()).fixWidth(6).leftPad().with(new PercentPart()).buildAndSchedule(() -> progressList.stream().mapToInt(AtomicInteger::get).sum() / nbProgresses);
    if (!tmpDirectory.exists() && !tmpDirectory.mkdirs()) {
        throw new IOException("Unable to make directory: " + tmpDirectory);
    }
    CountDownLatch encodingLatch = new CountDownLatch(titles.size());
    Iterator<AtomicInteger> progressIterator = progressList.iterator();
    for (MPlayerTitle title : titles) {
        AtomicInteger dumpProgress = progressIterator.next();
        AtomicInteger encodeProgress = progressIterator.next();
        currentTitle.incrementAndGet();
        String baseName = name + "-" + title.getNum();
        File vobFile = File.createTempFile(baseName + '-', ".vob", tmpDirectory);
        logger.log("Dumping title " + currentTitle + "/" + titles.size() + ": " + vobFile.getAbsolutePath());
        MPlayerDump mPlayerDump = mPlayerDumperBuilder.prepare(dvdDrive, logger).progress(dumpProgress::set).dump(title.getNum(), vobFile);
        File mp4File = new File(tmpDirectory, baseName + ".mp4");
        logger.log("Encoding title " + currentTitle + "/" + titles.size() + ": " + mp4File.getAbsolutePath());
        encode(vobFile, mp4File, mPlayerDump, encodeProgress, currentEncoding, encodingLatch);
    }
    currentTitle.set(-1);
    try {
        encodingLatch.await();
    } catch (InterruptedException e) {
        throw new IOException(e);
    }
    logger.log("End");
}
Also used : SpinnerPart(org.fagu.fmv.textprogressbar.part.SpinnerPart) MPlayerDump(org.fagu.fmv.soft.mplayer.MPlayerDump) MPlayerTitle(org.fagu.fmv.soft.mplayer.MPlayerTitle) ArrayList(java.util.ArrayList) PercentPart(org.fagu.fmv.textprogressbar.part.PercentPart) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) TextProgressBarBuilder(org.fagu.fmv.textprogressbar.TextProgressBar.TextProgressBarBuilder) MPlayerTitles(org.fagu.fmv.soft.mplayer.MPlayerTitles) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TextProgressBar(org.fagu.fmv.textprogressbar.TextProgressBar) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 MPlayerDump (org.fagu.fmv.soft.mplayer.MPlayerDump)1 MPlayerTitle (org.fagu.fmv.soft.mplayer.MPlayerTitle)1 MPlayerTitles (org.fagu.fmv.soft.mplayer.MPlayerTitles)1 TextProgressBar (org.fagu.fmv.textprogressbar.TextProgressBar)1 TextProgressBarBuilder (org.fagu.fmv.textprogressbar.TextProgressBar.TextProgressBarBuilder)1 PercentPart (org.fagu.fmv.textprogressbar.part.PercentPart)1 SpinnerPart (org.fagu.fmv.textprogressbar.part.SpinnerPart)1