use of org.fagu.fmv.ffmpeg.metadatas.VideoStream in project fmv by f-agu.
the class AutoRotateTestCase method test.
@Test
public void test() {
MovieMetadatas movieMetadatas = mock(MovieMetadatas.class);
VideoStream videoStream = mock(VideoStream.class);
doReturn(videoStream).when(movieMetadatas).getVideoStream();
doReturn(Optional.of("90")).when(videoStream).tag("rotate");
AutoRotate.create(movieMetadatas);
}
use of org.fagu.fmv.ffmpeg.metadatas.VideoStream in project fmv by f-agu.
the class Movie method getDevice.
/**
* @see org.fagu.fmv.media.Media#getDevice()
*/
@Override
public String getDevice() {
List<String> keys = new ArrayList<>();
VideoStream videoStream = videoMetadatas.getVideoStream();
if (videoStream != null) {
String handlerName = videoStream.handlerName().orElse(null);
if (StringUtils.isNotEmpty(handlerName)) {
keys.add(handlerName);
}
Size size = videoStream.size();
if (size != null) {
keys.add(size.toString());
}
}
AudioStream audioStream = videoMetadatas.getAudioStream();
if (audioStream != null) {
String handlerName = audioStream.handlerName().orElse(null);
if (StringUtils.isNotEmpty(handlerName)) {
keys.add(handlerName);
}
OptionalInt sampleRate = audioStream.sampleRate();
if (sampleRate.isPresent()) {
keys.add(sampleRate.getAsInt() + "Hz");
}
}
return keys.stream().collect(Collectors.joining(" "));
}
use of org.fagu.fmv.ffmpeg.metadatas.VideoStream in project fmv by f-agu.
the class FFReducer method isVideo.
/**
* @param metadatas
* @param logger
* @return
*/
private boolean isVideo(MovieMetadatas metadatas, Logger logger) {
if (!metadatas.contains(Type.VIDEO)) {
logger.log("Is audio: not contains video stream");
return false;
}
VideoStream videoStream = metadatas.getVideoStream();
OptionalInt numberOfFrames = videoStream.numberOfFrames();
if (numberOfFrames.isPresent() && numberOfFrames.getAsInt() == 1) {
logger.log("Is audio: number of frames unavailable: " + numberOfFrames);
return false;
}
if (Decoders.MJPEG.getName().equals(videoStream.codecName().get())) {
FrameRate frameRate = videoStream.frameRate().orElse(null);
if (frameRate != null && frameRate.floatValue() < 100) {
return true;
}
logger.log("Is audio: frameRate is null or too high: " + frameRate);
return false;
}
return true;
}
use of org.fagu.fmv.ffmpeg.metadatas.VideoStream 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.metadatas.VideoStream in project fmv by f-agu.
the class FFReducer method needToReduceAudio.
/**
* @param metadatas
* @param srcFile
* @return
*/
private boolean needToReduceAudio(MovieMetadatas metadatas, File srcFile) {
AudioStream audioStream = metadatas.getAudioStream();
String extension = FilenameUtils.getExtension(srcFile.getName());
if (!audioFormat.equalsIgnoreCase(extension) || audioStream.bitRate().getAsInt() > 128000) {
return true;
}
VideoStream videoStream = metadatas.getVideoStream();
if (videoStream != null && Decoders.MJPEG.getName().equals(videoStream.codecName().get())) {
// has cover
return true;
}
return false;
}
Aggregations