Search in sources :

Example 1 with FileFinder

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);
    }
}
Also used : FFReducer(org.fagu.fmv.mymedia.reduce.FFReducer) HashMap(java.util.HashMap) Size(org.fagu.fmv.utils.media.Size) AutoRotate(org.fagu.fmv.ffmpeg.filter.impl.AutoRotate) ConverterListener(org.fagu.fmv.mymedia.classify.ConverterListener) Map(java.util.Map) VolumeDetected(org.fagu.fmv.ffmpeg.filter.impl.VolumeDetected) MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) FFMPEGExecutorBuilder(org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder) PrintStream(java.io.PrintStream) Rotation(org.fagu.fmv.utils.media.Rotation) Rotate(org.fagu.fmv.ffmpeg.filter.impl.Rotate) FileFinder(org.fagu.fmv.utils.file.FileFinder) FFMpegUtils(org.fagu.fmv.ffmpeg.FFMpegUtils) FFExecutor(org.fagu.fmv.ffmpeg.executor.FFExecutor) OutputProcessor(org.fagu.fmv.ffmpeg.operation.OutputProcessor) IOException(java.io.IOException) File(java.io.File) Volume(org.fagu.fmv.ffmpeg.filter.impl.Volume) Transpose(org.fagu.fmv.ffmpeg.filter.impl.Transpose) Logger(org.fagu.fmv.mymedia.logger.Logger) Optional(java.util.Optional) ResampleAudio(org.fagu.fmv.ffmpeg.filter.impl.ResampleAudio) Converter(org.fagu.fmv.mymedia.classify.Converter) FilenameUtils(org.apache.commons.io.FilenameUtils) FFMPEGExecutorBuilder(org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder) Size(org.fagu.fmv.utils.media.Size) IOException(java.io.IOException) Rotation(org.fagu.fmv.utils.media.Rotation) MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) OutputProcessor(org.fagu.fmv.ffmpeg.operation.OutputProcessor) File(java.io.File) VolumeDetected(org.fagu.fmv.ffmpeg.filter.impl.VolumeDetected)

Example 2 with FileFinder

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");
}
Also used : FileFinder(org.fagu.fmv.utils.file.FileFinder) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Example 3 with FileFinder

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;
}
Also used : ArrayList(java.util.ArrayList) FileFinder(org.fagu.fmv.utils.file.FileFinder) File(java.io.File) SortedSet(java.util.SortedSet) MapSortedSet(org.fagu.fmv.utils.collection.MapSortedSet)

Example 4 with FileFinder

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;
}
Also used : ImageMetadatas(org.fagu.fmv.im.ImageMetadatas) FileFinder(org.fagu.fmv.utils.file.FileFinder) Image(org.fagu.fmv.im.Image) File(java.io.File) InfosFile(org.fagu.fmv.utils.file.FileFinder.InfosFile) Date(java.util.Date)

Example 5 with FileFinder

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());
}
Also used : InfosFile(org.fagu.fmv.utils.file.FileFinder.InfosFile) TreeSet(java.util.TreeSet) TimeOffsetImageComparator(org.fagu.fmv.mymedia.classify.image.TimeOffsetImageComparator) FileFinder(org.fagu.fmv.utils.file.FileFinder) Image(org.fagu.fmv.im.Image) Test(org.junit.Test)

Aggregations

FileFinder (org.fagu.fmv.utils.file.FileFinder)10 File (java.io.File)8 ArrayList (java.util.ArrayList)3 SortedSet (java.util.SortedSet)3 Image (org.fagu.fmv.im.Image)3 MapSortedSet (org.fagu.fmv.utils.collection.MapSortedSet)3 IOException (java.io.IOException)2 Date (java.util.Date)2 MovieMetadatas (org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas)2 TimeOffsetImageComparator (org.fagu.fmv.mymedia.classify.image.TimeOffsetImageComparator)2 InfosFile (org.fagu.fmv.utils.file.FileFinder.InfosFile)2 Rotation (org.fagu.fmv.utils.media.Rotation)2 Test (org.junit.Test)2 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 PrintStream (java.io.PrintStream)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1