Commit b3326fdb authored by yanzg's avatar yanzg

压缩视频

parent 7ed5cf69
......@@ -43,11 +43,10 @@ public class MediaHelper {
*
* @param fromFile 视频文件
* @param toFile 第一帧图片
* @return
* @throws IOException
* @throws EncoderException
*/
public static void zipVideo(String fromFile, String toFile) throws EncoderException {
zipVide(fromFile, toFile, FORMAT_EMPTY, FORMAT_EMPTY, DEFAULT_SIZE, DEFAULT_BIT_RATE);
zipVideo(fromFile, toFile, FORMAT_EMPTY, FORMAT_EMPTY, DEFAULT_SIZE, DEFAULT_BIT_RATE);
}
/**
......@@ -56,11 +55,10 @@ public class MediaHelper {
* @param fromFile 视频文件
* @param toFile 第一帧图片
* @param size 宽度和高度是否压缩
* @return
* @throws IOException
* @throws EncoderException
*/
public static void zipVideo(String fromFile, String toFile, float size) throws EncoderException {
zipVide(fromFile, toFile, FORMAT_EMPTY, FORMAT_EMPTY, size, DEFAULT_BIT_RATE);
zipVideo(fromFile, toFile, FORMAT_EMPTY, FORMAT_EMPTY, size, DEFAULT_BIT_RATE);
}
/**
......@@ -71,11 +69,10 @@ public class MediaHelper {
* @param size 宽度和高度是否压缩
* @param bitRate 它为新的重新编码的视频流设置比特率值。如果未设置比特率值,编码器将选择默认值。
* 该值应以位/秒表示。在这个例子中,如果你想要一个360kb/s的比特率,你应该调用setBitRate(新整数(360000))。
* @return
* @throws IOException
* @throws EncoderException
*/
public static void zipVideo(String fromFile, String toFile, float size, int bitRate) throws EncoderException {
zipVide(fromFile, toFile, FORMAT_EMPTY, FORMAT_EMPTY, size, bitRate);
zipVideo(fromFile, toFile, FORMAT_EMPTY, FORMAT_EMPTY, size, bitRate);
}
......@@ -84,11 +81,10 @@ public class MediaHelper {
*
* @param fromFile 视频文件
* @param toFile 第一帧图片
* @return
* @throws IOException
* @throws EncoderException
*/
public static void zipVideo3pg(String fromFile, String toFile) throws EncoderException {
zipVide(fromFile, toFile, FORMAT_3PG, FORMAT_3PG_CODEC, DEFAULT_SIZE, DEFAULT_BIT_RATE);
zipVideo(fromFile, toFile, FORMAT_3PG, FORMAT_3PG_CODEC, DEFAULT_SIZE, DEFAULT_BIT_RATE);
}
/**
......@@ -97,11 +93,10 @@ public class MediaHelper {
* @param fromFile 视频文件
* @param toFile 第一帧图片
* @param size 宽度和高度是否压缩
* @return
* @throws IOException
* @throws EncoderException
*/
public static void zipVideo3pg(String fromFile, String toFile, float size) throws EncoderException {
zipVide(fromFile, toFile, FORMAT_3PG, FORMAT_3PG_CODEC, size, DEFAULT_BIT_RATE);
zipVideo(fromFile, toFile, FORMAT_3PG, FORMAT_3PG_CODEC, size, DEFAULT_BIT_RATE);
}
/**
......@@ -111,12 +106,11 @@ public class MediaHelper {
* @param toFile 第一帧图片
* @param size 宽度和高度是否压缩
* @param bitRate 它为新的重新编码的视频流设置比特率值。如果未设置比特率值,编码器将选择默认值。
* 该值应以位/秒表示。在这个例子中,如果你想要一个360kb/s的比特率,你应该调用setBitRate(新整数(360000))。
* @return
* @throws IOException
* 该值应以位/秒表示。在这个例子中,如果你想要一个360kb/s的比特率,你应该调用setBitRate(新整数(360000))
* @throws EncoderException
*/
public static void zipVideo3pg(String fromFile, String toFile, float size, int bitRate) throws EncoderException {
zipVide(fromFile, toFile, FORMAT_3PG, FORMAT_3PG_CODEC, size, bitRate);
zipVideo(fromFile, toFile, FORMAT_3PG, FORMAT_3PG_CODEC, size, bitRate);
}
/**
......@@ -124,11 +118,10 @@ public class MediaHelper {
*
* @param fromFile 视频文件
* @param toFile 第一帧图片
* @return
* @throws IOException
* @throws EncoderException
*/
public static void zipVideoMpeg(String fromFile, String toFile) throws EncoderException {
zipVide(fromFile, toFile, FORMAT_MPEG, FORMAT_MPEG_CODEC, DEFAULT_SIZE, DEFAULT_BIT_RATE);
zipVideo(fromFile, toFile, FORMAT_MPEG, FORMAT_MPEG_CODEC, DEFAULT_SIZE, DEFAULT_BIT_RATE);
}
/**
......@@ -137,11 +130,10 @@ public class MediaHelper {
* @param fromFile 视频文件
* @param toFile 第一帧图片
* @param size 宽度和高度是否压缩
* @return
* @throws IOException
* @throws EncoderException
*/
public static void zipVideoMpeg(String fromFile, String toFile, float size) throws EncoderException {
zipVide(fromFile, toFile, FORMAT_MPEG, FORMAT_MPEG_CODEC, size, DEFAULT_BIT_RATE);
zipVideo(fromFile, toFile, FORMAT_MPEG, FORMAT_MPEG_CODEC, size, DEFAULT_BIT_RATE);
}
/**
......@@ -152,11 +144,10 @@ public class MediaHelper {
* @param size 宽度和高度是否压缩
* @param bitRate 它为新的重新编码的视频流设置比特率值。如果未设置比特率值,编码器将选择默认值。
* 该值应以位/秒表示。在这个例子中,如果你想要一个360kb/s的比特率,你应该调用setBitRate(新整数(360000))。
* @return
* @throws IOException
* @throws EncoderException
*/
public static void zipVideoMpeg(String fromFile, String toFile, float size, int bitRate) throws EncoderException {
zipVide(fromFile, toFile, FORMAT_MPEG, FORMAT_MPEG_CODEC, size, bitRate);
zipVideo(fromFile, toFile, FORMAT_MPEG, FORMAT_MPEG_CODEC, size, bitRate);
}
......@@ -165,11 +156,10 @@ public class MediaHelper {
*
* @param fromFile 视频文件
* @param toFile 第一帧图片
* @return
* @throws IOException
* @throws EncoderException
*/
public static void zipVideoFlv(String fromFile, String toFile) throws EncoderException {
zipVide(fromFile, toFile, FORMAT_FLV, FORMAT_FLV, DEFAULT_SIZE, DEFAULT_BIT_RATE);
zipVideo(fromFile, toFile, FORMAT_FLV, FORMAT_FLV, DEFAULT_SIZE, DEFAULT_BIT_RATE);
}
/**
......@@ -178,11 +168,10 @@ public class MediaHelper {
* @param fromFile 视频文件
* @param toFile 第一帧图片
* @param size 宽度和高度是否压缩
* @return
* @throws IOException
* @throws EncoderException
*/
public static void zipVideoFlv(String fromFile, String toFile, float size) throws EncoderException {
zipVide(fromFile, toFile, FORMAT_FLV, FORMAT_FLV, size, DEFAULT_BIT_RATE);
zipVideo(fromFile, toFile, FORMAT_FLV, FORMAT_FLV, size, DEFAULT_BIT_RATE);
}
/**
......@@ -193,11 +182,40 @@ public class MediaHelper {
* @param size 宽度和高度是否压缩
* @param bitRate 它为新的重新编码的视频流设置比特率值。如果未设置比特率值,编码器将选择默认值。
* 该值应以位/秒表示。在这个例子中,如果你想要一个360kb/s的比特率,你应该调用setBitRate(新整数(360000))。
* @return
* @throws IOException
* @throws EncoderException
*/
public static void zipVideoFlv(String fromFile, String toFile, float size, int bitRate) throws EncoderException {
zipVide(fromFile, toFile, FORMAT_FLV, FORMAT_FLV, size, bitRate);
zipVideo(fromFile, toFile, FORMAT_FLV, FORMAT_FLV, size, bitRate);
}
/**
* 从视频文件中获取第一张图片
*
* @param fromFile 视频文件
* @param toFile 第一帧图片
* @param format 要支持的格式
* @param codeC 视频的格式
* @throws EncoderException
*/
public static void zipVideo(String fromFile, String toFile,
String format, String codeC) throws EncoderException {
zipVideo(fromFile, toFile, format, codeC, DEFAULT_SIZE, DEFAULT_BIT_RATE);
}
/**
* 从视频文件中获取第一张图片
*
* @param fromFile 视频文件
* @param toFile 第一帧图片
* @param format 要支持的格式
* @param codeC 视频的格式
* @param size 宽度和高度是否压缩
* @throws EncoderException
*/
public static void zipVideo(String fromFile, String toFile,
String format, String codeC, float size) throws EncoderException {
zipVideo(fromFile, toFile, format, codeC, size, DEFAULT_BIT_RATE);
}
/**
......@@ -210,12 +228,11 @@ public class MediaHelper {
* @param size 宽度和高度是否压缩
* @param bitRate 它为新的重新编码的视频流设置比特率值。如果未设置比特率值,编码器将选择默认值。
* 该值应以位/秒表示。在这个例子中,如果你想要一个360kb/s的比特率,你应该调用setBitRate(新整数(360000))。
* @return
* @throws IOException
* @throws EncoderException
*/
public static void zipVide(String fromFile, String toFile,
String format, String codeC,
float size, int bitRate) throws EncoderException {
public static void zipVideo(String fromFile, String toFile,
String format, String codeC,
float size, int bitRate) throws EncoderException {
if (size > 1) {
throw new RuntimeException("size请在0~1之间");
}
......@@ -295,7 +312,6 @@ public class MediaHelper {
*
* @param fromFile 视频文件
* @param toFile 第一帧图片
* @return
* @throws IOException
*/
public static void getVideoFirstImage(String fromFile, String toFile) throws IOException {
......
......@@ -2,9 +2,7 @@ package helper;
import com.yanzuoguang.util.MediaHelper;
import com.yanzuoguang.util.thread.RunnableListAuto;
import com.yanzuoguang.util.thread.ThreadHelper;
import it.sauronsoftware.jave.EncoderException;
import jdk.management.resource.internal.inst.ThreadRMHooks;
import org.junit.Test;
import java.io.File;
......@@ -106,4 +104,33 @@ public class TestMediaHelper {
file, (int) (size * 100), (int) (quote * 100));
MediaHelper.zipVideoMpeg(file, name, size, (int) (MediaHelper.DEFAULT_BIT_RATE * quote));
}
@Test
public void testVideoZip3pg() throws EncoderException {
List<Runnable> list = new ArrayList<>();
for (int i = 0; i < sizes.length; i++) {
list.add(testVideoZip3pgThread(sizes[i], quotes[i]));
}
RunnableListAuto.run(list);
}
private Runnable testVideoZip3pgThread(float size, float quote) {
return new Runnable() {
@Override
public void run() {
try {
testVideoZip3pg(size, quote);
} catch (EncoderException e) {
throw new RuntimeException(e);
}
}
};
}
private void testVideoZip3pg(float size, float quote) throws EncoderException {
String file = getFile();
String name = String.format("%s.size_%d.quot_%d.3pg",
file, (int) (size * 100), (int) (quote * 100));
MediaHelper.zipVideo3pg(file, name, size, (int) (MediaHelper.DEFAULT_BIT_RATE * quote));
}
}
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