要使用Android的MP4Parser库调整视频分辨率,请按照以下步骤操作:
dependencies {
implementation 'com.googlecode.mp4parser:mp4parser:1.5.0'
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
import android.media.MediaMetadata;
import android.media.MediaCodec;
import android.media.MediaFormat;
import com.googlecode.mp4parser.AbstractContainer;
import com.googlecode.mp4parser.Container;
import com.googlecode.mp4parser.FileChannelWrapper;
import com.googlecode.mp4parser.Movie;
import com.googlecode.mp4parser.MutableInteger;
import com.googlecode.mp4parser.Track;
import com.googlecode.mp4parser.VideoCodecCore;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
public void resizeVideo(String inputPath, String outputPath, int newWidth, int newHeight) throws IOException {
File inputFile = new File(inputPath);
Container container = new Container(new FileChannelWrapper(new FileInputStream(inputFile)));
Movie movie = new Movie();
for (Track track : container.getTracks()) {
if (track.getHandler().toString().contains("Video")) {
MediaFormat format = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, newWidth, newHeight);
VideoCodecCore codec = new VideoCodecCore();
MediaCodec mediaCodec = MediaCodec.createByCodecName("avc1");
mediaCodec.configure(format, null, null, 0);
mediaCodec.start();
MutableInteger bufferSize = new MutableInteger(0);
ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers();
int trackIndex = container.getTracks().indexOf(track);
long startTime = 0;
long endTime = 0;
for (int i = 0; i < movie.getTracks().size(); i++) {
if (movie.getTracks().get(i).equals(track)) {
startTime = movie.getTracks().get(i).getSampleDescriptionBox().getSampleEntryTime();
endTime = movie.getTracks().get(i).getLastSampleEntryTime();
break;
}
}
long sampleTime = startTime;
while (sampleTime < endTime) {
int inputBufferIndex = mediaCodec.dequeueInputBuffer(-1);
if (inputBufferIndex >= 0) {
ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
int sampleSize = container.readSampleData(inputBuffer, 0);
if (sampleSize < 0) {
break;
}
inputBuffer.limit(sampleSize);
mediaCodec.queueInputBuffer(inputBufferIndex, 0, sampleSize, sampleTime, 0);
sampleTime += sampleSize;
}
}
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
while (mediaCodec.dequeueOutputBuffer(bufferInfo, 0) == MediaCodec.BUFFER_FLAG_END_OF_STREAM) {
ByteBuffer outputBuffer = mediaCodec.getOutputBuffer(bufferInfo.outputBufferIndex);
outputBuffer.position(bufferInfo.offset);
outputBuffer.limit(bufferInfo.offset + bufferInfo.size);
FileOutputStream fos = new FileOutputStream(outputPath);
FileChannelWrapper outputChannel = new FileChannelWrapper(fos);
outputChannel.write(outputBuffer);
outputChannel.close();
mediaCodec.releaseOutputBuffer(bufferInfo.outputBufferIndex, false);
}
mediaCodec.stop();
mediaCodec.release();
}
movie.addTrack(track);
}
movie.writeContainer(new FileOutputStream(outputPath));
}
resizeVideo
方法,传入输入视频路径、输出视频路径以及新的宽度和高度:String inputPath = "path/to/input/video.mp4";
String outputPath = "path/to/output/video.mp4";
int newWidth = 1280;
int newHeight = 720;
try {
resizeVideo(inputPath, outputPath, newWidth, newHeight);
} catch (IOException e) {
e.printStackTrace();
}
这将使用MP4Parser库将输入视频调整为指定的新分辨率,并将调整后的视频保存到输出路径。