Commit a57f74da authored by yanzg's avatar yanzg

压缩视频

parent ae22eb78
......@@ -37,6 +37,12 @@
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.11</version>
</dependency>
<dependency>
<groupId>com.yanzuoguang</groupId>
<artifactId>yzg-util-base</artifactId>
......
......@@ -2,6 +2,7 @@ package com.yanzuoguang.util;
import com.yanzuoguang.util.log.Log;
import it.sauronsoftware.jave.*;
import net.coobird.thumbnailator.Thumbnails;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
......@@ -11,6 +12,7 @@ import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
......@@ -30,6 +32,7 @@ public class MediaHelper {
public static final int FIRST_FRAME = 5;
public static final int DEFAULT_BIT_RATE = 372 * 1000;
public static final float DEFAULT_SIZE = 1f;
public static final float DEFAULT_QUALITY = 1f;
public static final String FORMAT_EMPTY = "";
public static final String FORMAT_FLV = "flv";
......@@ -357,13 +360,32 @@ public class MediaHelper {
*
* @param srcFilePath 来源路径
* @param descFilePath  目标路径
* @param quality 压缩程度,参数quality是取值0~1范围内
* @param quality 质量程度,取值0~1范围内
* @param size 大小程度,取值0~1范围内
* @throws IOException
*/
public static void compressPic(String srcFilePath, String descFilePath, float quality) throws IOException {
public static void compressPicBySystem(String srcFilePath, String descFilePath,
float quality, float size) throws IOException {
// 设置默认值
if (size == 0) {
size = 1;
}
if (quality == 0) {
quality = 1;
}
File input = new File(srcFilePath);
BufferedImage image = ImageIO.read(input);
// 压缩图片大小
if (size < 1) {
int toWidth = (int) (image.getWidth() * size);
int toHeight = (int) (image.getHeight() * size);
BufferedImage buffImage = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
buffImage.getGraphics().drawImage(image.getScaledInstance(toWidth, toHeight, Image.SCALE_SMOOTH),
0, 0, null);
image = buffImage;
}
// 先指定Output,才能调用writer.write方法
File output = new File(descFilePath);
......@@ -382,6 +404,7 @@ public class MediaHelper {
if (param.canWriteCompressed()) {
// 指定压缩方式为MODE_EXPLICIT
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
// param.set
// 压缩程度,参数quality是取值0~1范围内
param.setCompressionQuality(quality);
}
......@@ -399,4 +422,49 @@ public class MediaHelper {
}
}
}
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath  目标路径
* @throws IOException
*/
public static void compressPic(String srcFilePath, String descFilePath) throws IOException {
compressPic(srcFilePath, descFilePath, DEFAULT_QUALITY, DEFAULT_SIZE);
}
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath  目标路径
* @param quality 压缩程度,参数quality是取值0~1范围内
* @throws IOException
*/
public static void compressPic(String srcFilePath, String descFilePath, float quality) throws IOException {
compressPic(srcFilePath, descFilePath, quality, DEFAULT_SIZE);
}
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath  目标路径
* @param quality 质量程度,取值0~1范围内
* @param size 大小程度,取值0~1范围内
* @throws IOException
*/
public static void compressPic(String srcFilePath, String descFilePath, float quality, float size) throws IOException {
Thumbnails
// 来源目录
.of(srcFilePath)
// 按比例缩小
.scale(size)
// 质量压缩
.outputQuality(quality)
// 目标文件
.toFile(descFilePath);
}
}
......@@ -39,7 +39,8 @@ public class TestMediaHelper {
return;
}
String file = getFile();
MediaHelper.getVideoFirstImage(file, file + ".jpg");
String targetFile = getTargetFile();
MediaHelper.getVideoFirstImage(file, targetFile + ".jpg");
isFirstImage = true;
}
......@@ -47,10 +48,40 @@ public class TestMediaHelper {
public void testImageZip() throws IOException {
// 生成测试图片
testVideoFirstImage();
String file = getFile();
MediaHelper.compressPic(file + ".jpg", file + ".zip.jpg", 0.25f);
List<Runnable> list = new ArrayList<>();
for (int i = 0; i < sizes.length; i++) {
list.add(testImageZipThread(sizes[i], quotes[i]));
}
RunnableListAuto.run(list);
}
private Runnable testImageZipThread(float size, float quote) {
return new Runnable() {
@Override
public void run() {
try {
testImageZip(size, quote);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
}
private void testImageZip(float size, float quote) throws IOException {
String file = getTargetFile() + ".jpg";
String targetFile = getTargetFile();
String toName = String.format("%s.size_%d.quot_%d.jpg",
targetFile, (int) (size * 100), (int) (quote * 100));
String toNameSystem = String.format("%s.size_%d.quot_%d.system.jpg",
targetFile, (int) (size * 100), (int) (quote * 100));
MediaHelper.compressPic(file, toName, quote, size);
MediaHelper.compressPicBySystem(file, toNameSystem, quote, size);
}
@Test
public void testVideoZipFlv() {
List<Runnable> list = new ArrayList<>();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment