Commit 6cbc6257 authored by yanzg's avatar yanzg

视频转换

parent 7533cb00
......@@ -25,6 +25,7 @@ public class MediaHelper extends ImageHelper {
* 默认截取视频的中间帧为封面
*/
public static final int FRAME_INDEX = 3;
public static boolean Frame = true;
/**
* 从视频文件中获取第一张图片
......@@ -80,6 +81,9 @@ public class MediaHelper extends ImageHelper {
if (!target.getParentFile().exists()) {
target.getParentFile().mkdirs();
}
if (target.exists()) {
target.delete();
}
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(fromFile);
try {
......@@ -94,25 +98,34 @@ public class MediaHelper extends ImageHelper {
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(
toFile,
parameter.getVideoWidthFinally(),
parameter.getVideoHeightFinally(),
grabber.getAudioChannels()
parameter.getVideoHeightFinally()
);
parameter.init(grabber, recorder);
try {
// 开始转换
recorder.start(grabber.getFormatContext());
// recorder.start(grabber.getFormatContext());
recorder.start();
// 转码没有图像
if (Frame) {
Frame frame;
while ((frame = grabber.grabFrame(true, true, true, false)) != null) {
// if (grabber.grabPacket() == null) {
// continue;
// }
recorder.record(frame);
}
} else {
avcodec.AVPacket packet;
long dts = 0;
long t1 = System.currentTimeMillis();
while ((packet = grabber.grabPacket()) != null) {
long currentDts = packet.dts();
if (currentDts >= dts) {
recorder.recordPacket(packet);
}
dts = currentDts;
}
} finally {
recorder.stop();
recorder.release();
recorder.close();
}
Log.info(MediaHelper.class, "转换完成...源文件:%s 目标文件:%s 旋转参数:%s ",
......
package com.yanzuoguang.util;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.vo.BaseVo;
import io.swagger.annotations.ApiModelProperty;
import org.bytedeco.javacpp.avcodec;
......@@ -194,6 +195,7 @@ public class MediaParameter extends BaseVo {
// 读取音频属性
this.audioBitRate = StringHelper.getFirst(this.audioBitRate, grabber.getAudioBitrate());
this.audioCodec = StringHelper.getFirstRun(AV_CODEC_ID_NONE, AV_CODEC_ID_NONE, this.audioCodec, grabber.getAudioCodec());
Log.info(MediaParameter.class, "getAudioCodecName:%s", grabber.getAudioCodecName());
this.audioFrameRate = StringHelper.getFirstRun((double) AV_SAMPLE_FMT_NONE, (double) AV_SAMPLE_FMT_NONE, this.audioFrameRate, grabber.getAudioFrameRate());
// 视频宽度高度
......@@ -201,7 +203,8 @@ public class MediaParameter extends BaseVo {
this.videoHeight = StringHelper.getFirst(this.videoHeight, grabber.getImageHeight());
// 读取视频格式
this.videoFormat = StringHelper.getFirst(this.videoFormat, grabber.getFormat());
this.videoCodeC = StringHelper.getFirst(this.videoCodeC, grabber.getVideoCodec());
this.videoCodeC = StringHelper.getFirstRun(AV_CODEC_ID_NONE, AV_CODEC_ID_NONE, this.videoCodeC, grabber.getVideoCodec());
Log.info(MediaParameter.class, "getVideoCodecName:%s", grabber.getVideoCodecName());
this.videoPixelFormat = StringHelper.getFirstRun(AV_PIX_FMT_NONE, AV_PIX_FMT_NONE, this.videoPixelFormat, grabber.getPixelFormat());
// 读取视频属性
this.videoRotate = StringHelper.toDouble(grabber.getVideoMetadata(ROTATE));
......@@ -217,16 +220,19 @@ public class MediaParameter extends BaseVo {
public void init(FFmpegFrameGrabber grabber, FFmpegFrameRecorder recorder) {
// 设置音频比特率
recorder.setAudioBitrate(this.getAudioBitRateFinally());
recorder.setAudioCodec(this.audioCodec);
// recorder.setAudioCodec(this.audioCodec);
// 设置声道
recorder.setAudioChannels(grabber.getAudioChannels());
// 设置音频属性
// recorder.setSampleFormat(grabber.getSampleFormat());
// recorder.setSampleRate(grabber.getSampleRate());
// 视频格式
recorder.setFormat(this.videoFormat);
// 设置转码视频格式
recorder.setVideoCodec(this.videoCodeC);
// 设置像素格式
recorder.setPixelFormat(this.videoPixelFormat);
// recorder.setPixelFormat(this.videoPixelFormat);
// 设置转码后的视频角度
recorder.setMetadata(ROTATE, String.valueOf(this.getVideoRotateFinally()));
......@@ -234,6 +240,8 @@ public class MediaParameter extends BaseVo {
recorder.setVideoBitrate(StringHelper.getFirst(this.getVideoBitRateFinally(), this.getAudioBitRateFinally()));
// 设置帧速率
recorder.setFrameRate(grabber.getFrameRate());
// 帧长度
recorder.setTimestamp(grabber.getTimestamp());
}
public void initZipMp4() {
......
......@@ -2,19 +2,17 @@ package helper;
import com.yanzuoguang.util.MediaHelper;
import com.yanzuoguang.util.MediaParameter;
import com.yanzuoguang.util.thread.RunnableListAuto;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TestMediaHelper {
private boolean isFirstImage = false;
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 String[] fileNames = new String[]{"100M.mp4", "z001594372388232a3017ad69c82a342.MOV", "xuziming.mp4"};
private String getFile(String file) {
......@@ -35,36 +33,33 @@ public class TestMediaHelper {
}
@Test
public void testConvert() {
List<Runnable> list = new ArrayList<>();
public void testConvert() throws IOException {
for (String fileName : fileNames) {
for (int i = 0; i < sizes.length; i++) {
list.add(testConvertThread(sizes[i], quotes[i]));
testConvert(fileName, sizes[i], quotes[i]);
}
RunnableListAuto.run(list);
}
private Runnable testConvertThread(float size, float quote) {
return new Runnable() {
@Override
public void run() {
try {
testConvert(size, quote);
} catch (IOException e) {
e.printStackTrace();
}
}
};
@Test
public void testConvertSingle() throws IOException {
String fileName = "100M.mp4";
// String fileName = "z001594372388232a3017ad69c82a342.MOV";
testConvert(fileName, 1, 1);
}
private void testConvert(float size, float quote) throws IOException {
String file = getFile("100M.mp4");
String targetFile = getTargetFile("target/100M.mp4");
private void testConvert(String fileName, float size, float quote) throws IOException {
String file = getFile(fileName);
String targetFile = getTargetFile("target/" + fileName);
String toFile = String.format("%s.size_%d.quot_%d.mp4", targetFile, (int) (size * 100), (int) (quote * 100));
MediaParameter parameter = new MediaParameter();
parameter.setVideoSizeZip(size);
parameter.setVideoBitRateZip(quote);
// parameter.setVideoFrameRate(15);
MediaHelper.convertVideoMp4(file, toFile, parameter);
MediaHelper.convertVideo(file, toFile, parameter);
}
}
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