Skip to content

Commit

Permalink
#202 Customise formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
yermak committed Jan 29, 2021
1 parent ae274ba commit a70b03f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 20 deletions.
14 changes: 6 additions & 8 deletions src/main/java/uk/yermak/audiobookconverter/Chapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.apache.commons.lang3.StringUtils;
import uk.yermak.audiobookconverter.fx.ConverterApplication;

import java.time.Duration;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand All @@ -21,9 +21,9 @@ public Chapter(Part part, List<MediaInfo> media) {
this.media.addListener(part.getBook());
media.forEach(mediaInfo -> mediaInfo.setChapter(this));
this.media.addAll(media);
renderMap.put("CHAPTER_NUMBER", Chapter::getNumberString);
renderMap.put("CHAPTER_NUMBER", Chapter::getNumber);
renderMap.put("CHAPTER_TEXT", c -> "Chapter");
renderMap.put("DURATION", Chapter::getDurationString);
renderMap.put("DURATION", c->Duration.ofMillis(c.getDuration()));
}

public void replaceMediaWithTracks(MediaInfo mediaInfo, List<Track> tracks) {
Expand All @@ -33,10 +33,6 @@ public void replaceMediaWithTracks(MediaInfo mediaInfo, List<Track> tracks) {
this.getMedia().addAll(position, adaptors);
}

public String getNumberString() {
return StringUtils.leftPad(String.valueOf(getNumber()), 3, "0");
}

public Chapter(MediaInfo mediaInfo) {
this(null, Collections.singletonList(mediaInfo));
}
Expand Down Expand Up @@ -65,12 +61,14 @@ public String getDetails() {

@Override
public long getDuration() {
return (long) (media.stream().mapToLong(MediaInfo::getDuration).sum() / ConverterApplication.getContext().getSpeed());
return (long) (media.stream().mapToLong(Organisable::getDuration).sum() / ConverterApplication.getContext().getSpeed());
}

/*
public String getDurationString() {
return Utils.formatTime(getDuration());
}
*/

@Override
public boolean split() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ static void searchForPosters(List<MediaInfo> media) {

//adding artificial limit of image count to address issue #153.
if (!pictures.isEmpty()) {
for (int i = 0; i < 10 || i < pictures.size(); i++) {
for (int i = 0; i < 10 && i < pictures.size(); i++) {
context.addPosterIfMissingWithDelay(new ArtWorkBean(Utils.tempCopy(pictures.get(i).getPath())));
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/uk/yermak/audiobookconverter/Part.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.apache.commons.lang3.StringUtils;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -27,8 +27,8 @@ public Part(Book book) {
renderMap.put("WRITER", part -> part.getBook().getBookInfo().writer().trimToNull());
renderMap.put("NARRATOR", part -> part.getBook().getBookInfo().narrator().trimToNull());
renderMap.put("YEAR", part -> part.getBook().getBookInfo().year().trimToNull());
renderMap.put("PART", Part::getNumberString);
renderMap.put("DURATION", Part::getDurationString);
renderMap.put("PART", Part::getNumber);
renderMap.put("DURATION", p -> Duration.ofMillis(p.getDuration()));

}

Expand All @@ -40,7 +40,7 @@ public void replaceMediaChapterByTracksChapters(MediaInfo mediaInfo, List<Track>
this.getChapters().addAll(position, chapters);
}

public void construct(ObservableList<Chapter> chapters){
public void construct(ObservableList<Chapter> chapters) {
chapters.forEach(c -> c.setPart(this));
this.chapters.addAll(chapters);
}
Expand All @@ -50,17 +50,21 @@ private String getBookNumberString() {
return String.valueOf(getBook().getBookInfo().bookNumber());
}

/*
private String getNumberString() {
if (getBook().getParts().size() > 1) {
return StringUtils.leftPad(String.valueOf(getNumber()), 2, '0');
} else {
return null;
}
}
*/

/*
private String getDurationString() {
return Utils.formatTimeForFilename(getDuration());
}
*/

public String getTitle() {
return "Part " + getNumber();
Expand Down
34 changes: 28 additions & 6 deletions src/main/java/uk/yermak/audiobookconverter/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.*;

import java.io.File;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.text.DecimalFormat;
import java.time.Duration;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
Expand Down Expand Up @@ -64,13 +66,17 @@ public static String renderChapter(Chapter chapter, Map<String, Function<Chapter
chapterFormat = "<if(BOOK_NUMBER)><BOOK_NUMBER>. <endif>" +
"<if(BOOK_TITLE)><BOOK_TITLE>. <endif>" +
"<if(CHAPTER_TEXT)><CHAPTER_TEXT> <endif>" +
"<if(CHAPTER_NUMBER)><CHAPTER_NUMBER> <endif>" +
"<if(CHAPTER_NUMBER)><CHAPTER_NUMBER; format=\"%,03d\"> <endif>" +
"<if(TAG)><TAG> <endif>" +
"<if(CUSTOM_TITLE)><CUSTOM_TITLE> <endif>" +
"<if(DURATION)> - <DURATION><endif>";
"<if(DURATION)> - <DURATION; format=\"%02d:%02d:%02d\"><endif>";
AppProperties.setProperty("chapter_format", chapterFormat);
}
ST chapterTemplate = new ST(chapterFormat);
STGroup g = new STGroupString("");
g.registerRenderer(Number.class, new NumberRenderer());
g.registerRenderer(Duration.class, new DurationRender());
ST chapterTemplate = new ST(g, chapterFormat);

context.forEach((key, value) -> {
if (key.contains("TAG")) {
chapterTemplate.add("TAG", value.apply(chapter));
Expand Down Expand Up @@ -173,11 +179,15 @@ public static String renderPart(Part part, Map<String, Function<Part, Object>> c
"<if(TITLE)><TITLE><endif>" +
"<if(NARRATOR)> (<NARRATOR>)<endif>" +
"<if(YEAR)>-<YEAR><endif>" +
"<if(PART)>, Part <PART><endif>";
"<if(PART)>, Part <PART; format=\"%,03d\"><endif>";
AppProperties.setProperty("part_format", partFormat);
}

ST partTemplate = new ST(partFormat);
STGroup g = new STGroupString("");
g.registerRenderer(Number.class, new NumberRenderer());
ST partTemplate = new ST(g, partFormat);


context.forEach((key, value) -> {
partTemplate.add(key, value.apply(part));
});
Expand Down Expand Up @@ -228,4 +238,16 @@ private static String getFFMpegPath() {
public static final String FFPROBE = isSupported() ? new File(getFFMpegPath() + "/ffprobe" + (isWindows() ? ".exe" : "")).getAbsolutePath() : "ffprobe";


private static class DurationRender implements AttributeRenderer<Duration> {

@Override
public String toString(Duration duration, String format, Locale locale) {
if (format == null) {
format = "%02d:%02d:%02d";
}
return String.format(format, duration.toHoursPart(),
duration.toMinutesPart(),
duration.toSecondsPart());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void editChapter() {
chapterNo.setSelected(context.containsKey("CHAPTER_NUMBER"));
chapterNo.setOnAction(event -> {
if (chapterNo.isSelected()) {
context.put("CHAPTER_NUMBER", Chapter::getNumberString);
context.put("CHAPTER_NUMBER", Chapter::getNumber);
} else {
context.remove("CHAPTER_NUMBER");
}
Expand Down

0 comments on commit a70b03f

Please sign in to comment.