Commit 6e170c8b authored by yanzg's avatar yanzg

压缩视频

parent a57f74da
...@@ -36,8 +36,9 @@ public class MediaHelper { ...@@ -36,8 +36,9 @@ public class MediaHelper {
public static final String FORMAT_EMPTY = ""; public static final String FORMAT_EMPTY = "";
public static final String FORMAT_FLV = "flv"; public static final String FORMAT_FLV = "flv";
public static final String FORMAT_MPEG = "mpegvideo"; public static final String FORMAT_MPEG = "mp4";
public static final String FORMAT_MPEG_CODEC = "mpeg4"; public static final String FORMAT_MPEG_CODEC = "mpeg4";
// public static final String FORMAT_MPEG_CODEC = "mpeg";
public static final String FORMAT_3PG = "3gp"; public static final String FORMAT_3PG = "3gp";
public static final String FORMAT_3PG_CODEC = "mpeg4"; public static final String FORMAT_3PG_CODEC = "mpeg4";
...@@ -274,8 +275,9 @@ public class MediaHelper { ...@@ -274,8 +275,9 @@ public class MediaHelper {
// 视频编码设置 // 视频编码设置
VideoAttributes video = new VideoAttributes(); VideoAttributes video = new VideoAttributes();
// video.setCodec(VideoAttributes.DIRECT_STREAM_COPY);
video.setCodec(codeC); video.setCodec(codeC);
int fromFrameRate = (int) (fromVideo.getFrameRate()); // video.setTag("DIVX");
int toFrameRate = 15; int toFrameRate = 15;
video.setFrameRate(toFrameRate); video.setFrameRate(toFrameRate);
// 压缩比特率 // 压缩比特率
...@@ -283,7 +285,9 @@ public class MediaHelper { ...@@ -283,7 +285,9 @@ public class MediaHelper {
// 压缩宽高 // 压缩宽高
VideoSize fromSize = fromVideo.getSize(); VideoSize fromSize = fromVideo.getSize();
VideoSize toSize = new VideoSize((int) (fromSize.getWidth() * size), (int) (fromSize.getHeight() * size)); int toWidth = (int) (fromSize.getWidth() * size);
int toHeight = (int) (fromSize.getHeight() * size);
VideoSize toSize = new VideoSize(toWidth, toHeight);
video.setSize(toSize); video.setSize(toSize);
Log.info(MediaHelper.class, "%s-%s " + Log.info(MediaHelper.class, "%s-%s " +
......
...@@ -2,6 +2,7 @@ package helper; ...@@ -2,6 +2,7 @@ package helper;
import com.yanzuoguang.util.MediaHelper; import com.yanzuoguang.util.MediaHelper;
import com.yanzuoguang.util.thread.RunnableListAuto; import com.yanzuoguang.util.thread.RunnableListAuto;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException; import it.sauronsoftware.jave.EncoderException;
import org.junit.Test; import org.junit.Test;
...@@ -16,6 +17,50 @@ public class TestMediaHelper { ...@@ -16,6 +17,50 @@ public class TestMediaHelper {
public float[] sizes = new float[]{1f, 0.5f, 1f, 1f, 0.5f, 0.5f}; public float[] sizes = new float[]{1f, 0.5f, 1f, 1f, 0.5f, 0.5f};
public float[] quotes = new float[]{1f, 1f, 0.5f, 0.25f, 0.5f, 0.25f}; public float[] quotes = new float[]{1f, 1f, 0.5f, 0.25f, 0.5f, 0.25f};
@Test
public void printEncoding() throws EncoderException {
Encoder encoder = new Encoder();
String[] audioEncoders = encoder.getVideoEncoders();
for (String item : audioEncoders) {
System.out.println(item);
}
}
@Test
public void testConvert() throws EncoderException {
Encoder encoder = new Encoder();
String[] audioEncoders = encoder.getVideoEncoders();
List<Runnable> list = new ArrayList<>();
for (String item : audioEncoders) {
list.add(testConvertThread(item, 1f, 1f));
}
RunnableListAuto.run(list);
}
private Runnable testConvertThread(String item, float size, float quote) {
return new Runnable() {
@Override
public void run() {
try {
testConvert(item, size, quote);
} catch (EncoderException e) {
throw new RuntimeException(e);
}
}
};
}
private void testConvert(String item, float size, float quote) throws EncoderException {
String file = getFile();
String targetFile = getTargetFile();
String name = String.format("%s.size_%d.quot_%d.%s.mp4",
targetFile, (int) (size * 100), (int) (quote * 100), item);
MediaHelper.zipVideo(file, name, "mp4", item, size, (int) (MediaHelper.DEFAULT_BIT_RATE * quote));
}
private String getFile() { private String getFile() {
// 注意,路径应为文件在工程中的相对路径 // 注意,路径应为文件在工程中的相对路径
File f = new File("src/test/java/helper/100M.mp4"); File f = new File("src/test/java/helper/100M.mp4");
...@@ -114,6 +159,11 @@ public class TestMediaHelper { ...@@ -114,6 +159,11 @@ public class TestMediaHelper {
} }
@Test
public void testVideoZipMpeg_50_50() throws EncoderException {
testVideoZipMpeg(0.5f, 0.5f);
}
@Test @Test
public void testVideoZipMpeg() throws EncoderException { public void testVideoZipMpeg() throws EncoderException {
List<Runnable> list = new ArrayList<>(); List<Runnable> list = new ArrayList<>();
...@@ -174,4 +224,41 @@ public class TestMediaHelper { ...@@ -174,4 +224,41 @@ public class TestMediaHelper {
targetFile, (int) (size * 100), (int) (quote * 100)); targetFile, (int) (size * 100), (int) (quote * 100));
MediaHelper.zipVideo3pg(file, name, size, (int) (MediaHelper.DEFAULT_BIT_RATE * quote)); MediaHelper.zipVideo3pg(file, name, size, (int) (MediaHelper.DEFAULT_BIT_RATE * quote));
} }
@Test
public void testVideoZip_50_50() throws EncoderException {
testVideoZip(0.5f, 0.5f);
}
@Test
public void testVideoZip() throws EncoderException {
List<Runnable> list = new ArrayList<>();
for (int i = 0; i < sizes.length; i++) {
list.add(testVideoZipThread(sizes[i], quotes[i]));
}
RunnableListAuto.run(list);
}
private Runnable testVideoZipThread(float size, float quote) {
return new Runnable() {
@Override
public void run() {
try {
testVideoZip(size, quote);
} catch (EncoderException e) {
throw new RuntimeException(e);
}
}
};
}
private void testVideoZip(float size, float quote) throws EncoderException {
String file = getFile();
String targetFile = getTargetFile();
String name = String.format("%s.size_%d.quot_%d.mp4",
targetFile, (int) (size * 100), (int) (quote * 100));
MediaHelper.zipVideo(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