use of org.fagu.fmv.utils.file.FileFinder in project fmv by f-agu.
the class MovieScriptConverter method convert.
/**
* @see org.fagu.fmv.mymedia.classify.Converter#convert(org.fagu.fmv.media.Media,
* org.fagu.fmv.utils.file.FileFinder.InfosFile, java.io.File, org.fagu.fmv.mymedia.classify.ConverterListener)
*/
@Override
public void convert(Movie srcMedia, FileFinder<Movie>.InfosFile infosFile, File destFile, ConverterListener<Movie> listener) throws IOException {
openScript();
File srcFile = srcMedia.getFile();
MovieMetadatas infos = srcMedia.getMetadatas();
int audioFrequency = FFMpegUtils.minAudioSampleRate(infos, DEFAULT_AUDIO_SAMPLE_RATE);
FFMPEGExecutorBuilder builder = FFMPEGExecutorBuilder.create();
builder.hideBanner();
builder.addMediaInputFile(srcFile).setMovieMetadatas(infos);
Rotation rotation = rotateMap.get(srcFile.getName());
if (rotation != null) {
if (rotation != Rotation.R_0) {
builder.filter(Rotate.create(rotation));
}
} else {
builder.filter(AutoRotate.create(infos));
}
Size newSize = FFReducer.applyScaleIfNecessary(builder, infos, getMaxSize(), getScaleLogger(), rotation);
writeLabel();
script.println("rem " + (newSize.isLandscape() ? "landscape" : newSize.isPortrait() ? "portrait" : "square"));
builder.filter(ResampleAudio.build().frequency(audioFrequency));
Optional<VolumeDetected> findFirst = infosFile.getInfos().stream().filter(o -> o instanceof VolumeDetected).map(o -> (VolumeDetected) o).findFirst();
if (findFirst.isPresent()) {
VolumeDetected volumeDetected = findFirst.get();
builder.filter(Volume.build().increaseToMax(volumeDetected));
}
File dest = new File(destFile.getParentFile(), FilenameUtils.getBaseName(destFile.getName()) + ".mp4");
OutputProcessor outputProcessor = builder.addMediaOutputFile(dest);
outputProcessor.qualityScale(0);
Transpose.addMetadataRotate(outputProcessor, Rotation.R_0);
outputProcessor.format("mp4");
// outputProcessor.overwrite();
FFExecutor<Object> executor = builder.build();
try {
script.println("if exist \"" + dest.getPath() + "\" goto :movie_" + currentVideo);
script.println("echo.");
script.println("echo Frame: " + infos.getVideoStream().countEstimateFrames().getAsInt());
script.println(executor.getCommandLine());
script.println();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.fagu.fmv.utils.file.FileFinder in project fmv by f-agu.
the class AutoSaveLoadFileFinder method load.
// ***********************************************
/**
*/
private void load() {
if (!saveFile.exists()) {
return;
}
System.out.println("Loading... " + saveFile);
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(saveFile))) {
File currentRootFolder = null;
File currentFile = null;
String line = null;
int number = 0;
while ((line = bufferedReader.readLine()) != null) {
++number;
if (line.charAt(1) != ' ') {
throw new RuntimeException("Unreadable file (line " + number + ")");
}
if (line.startsWith("P ")) {
currentFile = new File(line.substring(2));
continue;
}
if (currentFile != null && currentFile.exists()) {
if (line.startsWith("R ")) {
currentRootFolder = new File(line.substring(2));
if (!currentRootFolder.exists()) {
currentRootFolder = null;
}
} else {
char code = line.charAt(0);
line = line.substring(2);
InfoFile infoFile = infoFileMap.get(code);
if (infoFile == null) {
throw new RuntimeException("InfoFile code not found: " + code);
}
FileFound fileFound = new FileFound(currentRootFolder, currentFile);
Object info = infoFile.parse(currentFile, line);
FileFinder<T>.InfosFile infosFile = get(fileFound);
if (infosFile == null) {
infosFile = new InfosFile();
add(fileFound, infosFile);
}
if (metadatasInfoFile.isMine(info)) {
@SuppressWarnings("unchecked") T t = (T) info;
infosFile.setMain(t);
} else {
infosFile.addInfo(info);
}
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println(count() + " files loaded");
}
use of org.fagu.fmv.utils.file.FileFinder in project fmv by f-agu.
the class ByPatternClassifier method classify.
/**
* @see org.fagu.fmv.mymedia.classify.Classifier#classify(org.fagu.fmv.mymedia.classify.Converter,
* org.fagu.fmv.mymedia.classify.ConverterListener)
*/
@Override
public List<File> classify(Converter<M> converter, ConverterListener<M> listener) throws IOException {
List<File> files = new ArrayList<>(100);
for (FileFinder<M>.InfosFile infosFile : finder.getAll()) {
M media = infosFile.getMain();
String extension = FilenameUtils.getExtension(media.getFile().getName());
Map<String, String> rmap = replacerMap(media, converter.getFormat(extension));
String key = PlaceHolder.format(pattern, Replacers.chain().map(rmap).unresolvableCopy());
map.add(key, infosFile);
replacerMap.analyze(media, this);
}
for (Entry<String, SortedSet<FileFinder<M>.InfosFile>> entry : map.entrySet()) {
for (FileFinder<M>.InfosFile infosFile : entry.getValue()) {
M media = infosFile.getMain();
String extension = FilenameUtils.getExtension(media.getFile().getName());
Map<String, String> map = replacerMap(media, converter.getFormat(extension));
String path = PlaceHolder.format(pattern, Replacers.chain().map(map).unresolvableCopy());
path = PlaceHolder.format(path, replacerMap.getReplacer(media, path, this));
File destFile = new File(destFolder, path);
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
converter.convert(media, infosFile, destFile, listener);
files.add(destFile);
}
}
return files;
}
use of org.fagu.fmv.utils.file.FileFinder in project fmv by f-agu.
the class TimeOffsetImageComparatorTestCase method mockImage.
/**
* @param time
* @param device
* @param model
* @param fileName
* @return
*/
private FileFinder<Image>.InfosFile mockImage(long time, String device, String model, String fileName) {
Image image = mock(Image.class);
ImageMetadatas imageMetadatas = mock(ImageMetadatas.class);
File file = mock(File.class);
doReturn(time).when(image).getTime();
doReturn(imageMetadatas).when(image).getMetadatas();
doReturn(new Date(time)).when(imageMetadatas).getDate();
if (device != null) {
doReturn(device).when(imageMetadatas).getDevice();
}
if (model != null) {
doReturn(model).when(imageMetadatas).getDeviceModel();
}
FileFinder<Image>.InfosFile infosFile = mock(InfosFile.class);
doReturn(image).when(infosFile).getMain();
doReturn(file).when(image).getFile();
doReturn(fileName).when(file).getName();
return infosFile;
}
use of org.fagu.fmv.utils.file.FileFinder in project fmv by f-agu.
the class TimeOffsetImageComparatorTestCase method testSome.
/**
*/
@Test
public void testSome() {
TimeOffsetImageComparator comparator = new TimeOffsetImageComparator();
comparator.addFilter(im -> "a".equals(im.getDevice()), 17);
// 18 = 1 + 17
FileFinder<Image>.InfosFile imageA1 = mockImage(1, "a", "a", "A1");
// 21 = 4 + 17
FileFinder<Image>.InfosFile imageA2 = mockImage(4, "a", "a", "A2");
// 12
FileFinder<Image>.InfosFile imageB1 = mockImage(15, "b", "b", "B1");
// 15
FileFinder<Image>.InfosFile imageB2 = mockImage(12, "b", "b", "B2");
// 17
FileFinder<Image>.InfosFile imageB3 = mockImage(17, "b", "b", "B3");
// 19
FileFinder<Image>.InfosFile imageB4 = mockImage(19, "b", "b", "B4");
TreeSet<FileFinder<Image>.InfosFile> images = new TreeSet<>(comparator);
images.add(imageA1);
images.add(imageA2);
images.add(imageB1);
images.add(imageB2);
images.add(imageB3);
images.add(imageB4);
Iterator<FileFinder<Image>.InfosFile> iterator = images.iterator();
assertSame(imageB2, iterator.next());
assertSame(imageB1, iterator.next());
assertSame(imageB3, iterator.next());
assertSame(imageA1, iterator.next());
assertSame(imageB4, iterator.next());
assertSame(imageA2, iterator.next());
assertFalse(iterator.hasNext());
}
Aggregations