use of org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder in project fmv by f-agu.
the class StreamTestCase method testInputToFile.
@Test
@Ignore
public void testInputToFile() throws Exception {
File outFile = new File("d:\\tmp\\out.ts");
FFMPEGExecutorBuilder builder = FFMPEGExecutorBuilder.create();
builder.addMediaInput(new PipeMediaInput()).add("-analyzeduration", Integer.toString(Integer.MAX_VALUE)).add("-probesize", Integer.toString(Integer.MAX_VALUE)).pixelFormat(PixelFormat.YUV420P);
builder.filter(Rotate.create(Rotation.R_90));
builder.addMediaOutputFile(outFile).bitStream(Type.VIDEO, BitStreamFilter.H264_MP4TOANNEXB).overwrite();
FFExecutor<Object> executor = builder.build();
System.out.println(executor.getCommandLine());
executor.input(() -> ResourceUtils.open("mp4.mp4"));
executor.execute();
}
use of org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder in project fmv by f-agu.
the class Reduce method doIt.
/**
* @param sourceFile
* @throws IOException
*/
private static void doIt(File sourceFile) throws IOException {
String extension = FilenameUtils.getExtension(sourceFile.getName()).toLowerCase();
if (!"mkv".equals(extension)) {
extension = "mp4";
}
File destinationFile = new File(sourceFile.getParentFile(), FilenameUtils.getBaseName(sourceFile.getName()) + "-new." + extension);
FFMPEGExecutorBuilder builder = FFMPEGExecutorBuilder.create();
builder.hideBanner();
InputProcessor inputProcessor = builder.addMediaInputFile(sourceFile);
MovieMetadatas videoMetadatas = inputProcessor.getMovieMetadatas();
boolean doVideo = !videoMetadatas.getVideoStream().isTreatedByFMV();
boolean doAudio = doVideo;
Collection<AudioStream> audioStreams = StreamOrder.sort(videoMetadatas.getAudioStreams());
for (AudioStream audioStream : audioStreams) {
if ("vorbis".equals(audioStream.codecName().get())) {
doAudio |= true;
break;
}
if ("aac".equals(audioStream.codecName().get())) {
doAudio = false;
break;
}
}
if (!doVideo && !doAudio) {
return;
}
OutputProcessor outputProcessor = builder.addMediaOutputFile(destinationFile);
outputProcessor.qualityScale(0);
// video
for (Stream stream : videoMetadatas.getVideoStreams()) {
outputProcessor.map().streams(stream).input(inputProcessor);
}
// audio
for (Stream stream : audioStreams) {
outputProcessor.map().streams(stream).input(inputProcessor);
}
// subtitle
Collection<SubtitleStream> subtitleStreams = StreamOrder.sort(videoMetadatas.getSubtitleStreams());
for (Stream stream : subtitleStreams) {
outputProcessor.map().streams(stream).input(inputProcessor);
}
// other stream
for (Stream stream : videoMetadatas.getStreams()) {
Type type = stream.type();
if (type != Type.AUDIO && type != Type.VIDEO && type != Type.SUBTITLE) {
outputProcessor.map().streams(stream).input(inputProcessor);
}
}
// ------------------------ disposition default ------------------------
//
int count = 0;
for (Stream stream : audioStreams) {
boolean beDefault = count == 1;
if (stream.isDefaultStream() != beDefault) {
outputProcessor.metadataStream(Type.AUDIO, count, "disposition:default", beDefault ? "1" : "0");
}
++count;
}
count = 0;
for (Stream stream : subtitleStreams) {
boolean beDefault = count == 1;
if (stream.isDefaultStream() != beDefault) {
outputProcessor.metadataStream(Type.SUBTITLE, count, "disposition:default", beDefault ? "1" : "0");
}
++count;
}
// video
if (doVideo) {
outputProcessor.codec(H264.findRecommanded().strict(Strict.EXPERIMENTAL).quality(23));
} else {
outputProcessor.codecCopy(Type.VIDEO);
}
// audio
if (doAudio) {
outputProcessor.codecAutoSelectAAC();
} else {
outputProcessor.codecCopy(Type.AUDIO);
}
// subtitle
if (videoMetadatas.contains(Type.SUBTITLE)) {
outputProcessor.codecCopy(Type.SUBTITLE);
}
// outputProcessor.overwrite();
FFExecutor<Object> executor = builder.build();
System.out.println(executor.getCommandLine());
}
use of org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder 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.ffmpeg.executor.FFMPEGExecutorBuilder in project fmv by f-agu.
the class FFReducer method reduceVideo.
/**
* @param metadatas
* @param srcFile
* @param movieMetadatas
* @param destFile
* @param consolePrefixMessage
* @param logger
* @throws IOException
*/
private boolean reduceVideo(MovieMetadatas metadatas, File srcFile, File destFile, String consolePrefixMessage, Logger logger) throws IOException {
AudioStream audioStream = metadatas.getAudioStream();
boolean audioCodecCopy = audioStream.isCodec(Formats.AC3);
FFMPEGExecutorBuilder builder = FFMPEGExecutorBuilder.create();
builder.hideBanner();
InputProcessor inputProcessor = builder.addMediaInputFile(srcFile);
builder.filter(AutoRotate.create(metadatas));
applyScaleIfNecessary(builder, metadatas, getMaxSize(), logger);
VolumeDetect volumeDetect = null;
if (!audioCodecCopy) {
volumeDetect = VolumeDetect.build();
builder.filter(volumeDetect);
}
CropDetect cropDetect = CropDetect.build();
builder.filter(cropDetect);
MovieMetadatas videoMetadatas = inputProcessor.getMovieMetadatas();
Collection<AudioStream> audioStreams = StreamOrder.sort(videoMetadatas.getAudioStreams());
OutputProcessor outputProcessor = builder.addMediaOutputFile(destFile);
outputProcessor.qualityScale(0);
// video
for (Stream stream : videoMetadatas.getVideoStreams()) {
logger.log("map[" + stream.index() + "] video: " + stream);
outputProcessor.map().streams(stream).input(inputProcessor);
}
// audio
for (Stream stream : audioStreams) {
logger.log("map[" + stream.index() + "] audio: " + stream);
outputProcessor.map().streams(stream).input(inputProcessor);
}
// subtitle
Collection<SubtitleStream> subtitleStreams = StreamOrder.sort(videoMetadatas.getSubtitleStreams());
for (Stream stream : subtitleStreams) {
logger.log("map[" + stream.index() + "] subtitle: " + stream);
outputProcessor.map().streams(stream).input(inputProcessor);
}
// other stream (Apple... again bullshit)
// for (Stream stream : videoMetadatas.getStreams()) {
// Type type = stream.type();
// if (type != Type.AUDIO && type != Type.VIDEO && type != Type.SUBTITLE) {
// logger.log("map other stream: " + stream);
// outputProcessor.map().streams(stream).input(inputProcessor);
// }
// }
// -------------------------- codec -------------------------
outputProcessor.codec(H264.findRecommanded().strict(Strict.EXPERIMENTAL).quality(crf));
// audio
if (audioCodecCopy) {
logger.log("Audio: AC3, copy");
outputProcessor.codecCopy(Type.AUDIO);
} else {
logger.log("Audio: force AAC");
outputProcessor.codecAutoSelectAAC();
}
// subtitle
if (videoMetadatas.contains(Type.SUBTITLE)) {
outputProcessor.codecCopy(Type.SUBTITLE);
}
outputProcessor.overwrite();
FFExecutor<Object> executor = builder.build();
executor.addListener(createLogFFExecListener(logger));
executor.addListener(createCropDetectFFExecListener(logger, cropDetect, videoMetadatas));
if (!audioCodecCopy) {
executor.addListener(createVolumeDetectFFExecListener(logger, volumeDetect));
}
VideoStream videoStream = metadatas.getVideoStream();
OptionalInt countEstimateFrames = videoStream.countEstimateFrames();
Progress progress = executor.getProgress();
if (countEstimateFrames.isPresent() && progress != null) {
textProgressBar = FFMpegProgressBar.with(progress).byFrame(countEstimateFrames.getAsInt()).fileSize(srcFile.length()).build().makeBar(consolePrefixMessage);
} else {
StringJoiner joiner = new StringJoiner(", ");
if (progress == null) {
joiner.add("progress not found");
}
if (!countEstimateFrames.isPresent()) {
joiner.add("nb frames nout found");
}
logger.log("No progress bar: " + joiner.toString());
}
executor.execute();
Optional<String> codecName = videoStream.codecName();
if (codecName.isPresent() && codecName.get().equalsIgnoreCase(Formats.HEVC.getName())) {
// h265
return true;
}
return false;
}
use of org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder in project fmv by f-agu.
the class Reducer method reduce.
public void reduce(File srcFile, File destFile, Logo logo) throws IOException {
int crf = (int) (getCRF(srcFile).orElse(26));
FFMPEGExecutorBuilder builder = FFMPEGExecutorBuilder.create();
builder.hideBanner();
builder.addMediaInputFile(srcFile);
if (logo != null) {
Delogo delogo = logo.generateFilter().show(true);
builder.filter(delogo);
}
OutputProcessor outputProcessor = builder.addMediaOutputFile(destFile);
outputProcessor.qualityScale(0);
outputProcessor.duration(Duration.valueOf(60));
outputProcessor.codec(H264.findRecommanded().strict(Strict.EXPERIMENTAL).quality(crf));
outputProcessor.overwrite();
FFExecutor<Object> executor = builder.build();
executor.execute();
}
Aggregations