Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.techgnious</groupId>
<artifactId>IVCompressor</artifactId>
<version>2.0.2</version>
<version>2.0.2.2</version>
<packaging>jar</packaging>

<name>ImageVideoCompressor</name>
Expand All @@ -30,10 +30,10 @@
</licenses>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<schild.version>3.0.1</schild.version>
<schild.version>3.2.0</schild.version>
</properties>

<distributionManagement>
Expand Down
80 changes: 74 additions & 6 deletions src/main/java/io/github/techgnious/IVCompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.imageio.ImageIO;
Expand All @@ -46,7 +48,10 @@
import ws.schild.jave.encode.EncodingAttributes;
import ws.schild.jave.encode.VideoAttributes;
import ws.schild.jave.encode.enums.X264_PROFILE;
import ws.schild.jave.filters.VideoFilter;
import ws.schild.jave.info.MultimediaInfo;
import ws.schild.jave.info.VideoSize;
import ws.schild.jave.progress.EncoderProgressListener;

/**
* Base Class to handle compression or conversion of Image/Video Files.
Expand Down Expand Up @@ -458,7 +463,36 @@ public byte[] convertVideoFormat(byte[] data, VideoFormats inputFormat, VideoFor
encodingAttributes.setVideoAttributes(videoAttr);
return encodeVideo(data, filetype);
}


/**
* This method is used to convert the video from existing format to another
* format without compressing the data
*
* @param data - data that is to be converted
* @param inputFormat - video format the data is to be converted
* @param outputFormat - video format the data is to be converted
* @param audioAttribute to customize audio encoding
* @param videoAttribute to customize video encoding
* @return - returns byte array as response
* @throws VideoException - throws exception if there is an issue with video
* processing
*/
public byte[] convertVideoFormatWithAttributes(byte[] data, VideoFormats inputFormat, VideoFormats outputFormat,
IVAudioAttributes audioAttribute, IVVideoAttributes videoAttribute)
throws VideoException {
String fileFormat = inputFormat.getType();

// reset default audio/video attributes
audioAttributes = new AudioAttributes();
videoAttributes = new VideoAttributes();

setAudioAndVideoAttributes(fileFormat, audioAttribute, videoAttribute);

encodingAttributes.setInputFormat(inputFormat.getType());
encodingAttributes.setOutputFormat(outputFormat.getType());

return encodeVideo(data, fileFormat);
}
/**
* Method to set the user defined Audio and Video Attributes for encoding
*
Expand All @@ -470,14 +504,29 @@ private void setAudioAndVideoAttributes(String fileFormat, IVAudioAttributes aud
IVVideoAttributes videoAttribute) {
if (videoAttribute != null) {
videoAttributes.setCodec(IVConstants.VIDEO_CODEC);
videoAttributes.setX264Profile(X264_PROFILE.BASELINE);

if (videoAttribute.getX264Profile() != null) {
videoAttributes.setX264Profile(videoAttribute.getX264Profile());
} else {
videoAttributes.setX264Profile(X264_PROFILE.BASELINE);
}

if (videoAttribute.getPixelFormat() != null) {
videoAttributes.setPixelFormat(videoAttribute.getPixelFormat().getType());
}

if (videoAttribute.getBitRate() != null)
videoAttributes.setBitRate(videoAttribute.getBitRate());
if (videoAttribute.getFrameRate() != null)
videoAttributes.setFrameRate(videoAttribute.getFrameRate());
if (videoAttribute.getSize() != null)
videoAttributes.setSize(
new VideoSize(videoAttribute.getSize().getWidth(), videoAttribute.getSize().getHeight()));

for (VideoFilter vf: videoAttribute.getVideoFilters()) {
videoAttributes.addFilter(vf);
}

encodingAttributes.setVideoAttributes(videoAttributes);
}
if (audioAttribute != null) {
Expand Down Expand Up @@ -507,15 +556,34 @@ private void setAudioAndVideoAttributes(String fileFormat, IVAudioAttributes aud
private byte[] encodeVideo(byte[] data, String fileFormat) throws VideoException {
File target = null;
File file = null;
try {
EncoderProgressListener listener = new EncoderProgressListener() {
MultimediaInfo info;
List<String> messages = new ArrayList<>();
@Override
public void sourceInfo(MultimediaInfo info) {
this.info = info;
}

@Override
public void progress(int permil) {
}

@Override
public void message(String message) {
messages.add(message);
}
};

try {
target = File.createTempFile(IVConstants.TARGET_FILENAME, fileFormat);
file = File.createTempFile(IVConstants.SOURCE_FILENAME, fileFormat);
FileUtils.writeByteArrayToFile(file, data);
MultimediaObject source = new MultimediaObject(file);
encoder.encode(source, target, encodingAttributes);
encoder.encode(source, target, encodingAttributes, listener);
return FileUtils.readFileToByteArray(target);
} catch (Exception e) {
throw new VideoException("Error Occurred while resizing the video");
List<String> unhandledMessages = encoder.getUnhandledMessages();
throw new VideoException("Error Occurred while resizing the video", unhandledMessages, e);
} finally {
try {
if (file != null)
Expand Down Expand Up @@ -551,7 +619,7 @@ private byte[] rescaleImage(byte[] data, int width, int height, String contentTy
writeImageToOutputstream(width, height, contentType, originalImage, outputStream, resizedImage);
return outputStream.toByteArray();
} catch (Exception e) {
throw new ImageException("Byte Array doesn't contain valid Image");
throw new ImageException("Byte Array doesn't contain valid Image", e);
}
}

Expand Down
39 changes: 38 additions & 1 deletion src/main/java/io/github/techgnious/dto/IVVideoAttributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
package io.github.techgnious.dto;

import java.util.ArrayList;

import ws.schild.jave.encode.enums.X264_PROFILE;
import ws.schild.jave.filters.VideoFilter;

/**
* Class to define video encoding attributes for enhancing the video compression
*
Expand All @@ -38,8 +43,16 @@ public class IVVideoAttributes {
* video size will not be modified.
*/
private IVSize size = null;


private X264_PROFILE x264Profile = null;

private PixelFormats pixelFormat = null;

private final ArrayList<VideoFilter> videoFilters = new ArrayList<>();

/**

/**
* @return the bitRate
*/
public Integer getBitRate() {
Expand Down Expand Up @@ -81,4 +94,28 @@ public void setSize(IVSize size) {
this.size = size;
}

public X264_PROFILE getX264Profile() {
return x264Profile;
}

public void setX264Profile(X264_PROFILE x264Profile) {
this.x264Profile = x264Profile;
}


public PixelFormats getPixelFormat() {
return pixelFormat;
}

public void setPixelFormat(PixelFormats pixelFormat) {
this.pixelFormat = pixelFormat;
}

public ArrayList<VideoFilter> getVideoFilters() {
return videoFilters;
}

public void addVideoFilter(VideoFilter vf) {
videoFilters.add(vf);
}
}
41 changes: 41 additions & 0 deletions src/main/java/io/github/techgnious/dto/PixelFormats.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2020 Srikanth Reddy Anreddy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.techgnious.dto;

/**
* Enum Class that defines the type of pixel formats allowed
*
*/
public enum PixelFormats {

YUV420P("yuv420p");

/**
* Extension type of the video
*/
private String type;

PixelFormats(String type) {
this.type = type;
}

/**
* @return the pixel format (ffmpeg values)
*/
public String getType() {
return type;
}
}
22 changes: 21 additions & 1 deletion src/main/java/io/github/techgnious/exception/VideoException.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package io.github.techgnious.exception;

import java.util.ArrayList;
import java.util.List;

/**
* Exception class to handle errors thrown during video compression
*
Expand All @@ -27,12 +30,25 @@ public class VideoException extends Exception {
*
*/
private static final long serialVersionUID = 378604187699000920L;

List<String> unhandledMessages = null;

public VideoException() {
super();
}

/**
/**
* Throws Video Exception error with custom message and casue
*
* @param message - description of the error
* @param cause - root cause of the issue
*/
public VideoException(String message, List<String> unhandledMessages, Throwable cause) {
super(message, cause);
this.unhandledMessages = unhandledMessages;
}

/**
* Throws Video Exception error with custom message and casue
*
* @param message - description of the error
Expand All @@ -57,5 +73,9 @@ public VideoException(String message) {
public VideoException(Throwable cause) {
super(cause);
}

public List<String> getUnhandledMessages() {
return unhandledMessages;
}

}