I'm capturing video frames as bitmaps using MediaMetadataRetriever.
MediaMetadataRetriever mmr;
double fps;
double duration;
long counter = 0;
long incrementer;
String fpsString;
int width;
int height;
int bitrate;
int rotation;
public FrameCollector(String path, String fpsString) {
try {
mmr = new MediaMetadataRetriever();
mmr.setDataSource(path);
width = Integer.valueOf(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
height = Integer.valueOf(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
bitrate = Integer.valueOf(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE));
String metaRotation = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
rotation = metaRotation == null ? 0 : Integer.parseInt(metaRotation);
Log.i("Rotation ", "rotation:" + rotation);
this.fpsString = fpsString;
fps = Double.valueOf(fpsString);
incrementer = (long) (1000000 / fps);
String stringDuration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
duration = Double.parseDouble(stringDuration) * 1000;
} catch (Exception ex) {
ex.printStackTrace();
}
}
public Bitmap getFirstBitmapFrame() {
try {
Bitmap b = mmr.getFrameAtTime(counter, OPTION_CLOSEST);
return b;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
the problem is when mp4 video has METADATA_KEY_VIDEO_ROTATION == 90 the width and height of frames has been transposed so the video that i made from bitmaps using mediaCodec has wrong width and height.
public void startEncoding(ArrayList<Bitmap> bitmaps, int rotation, int width, int height, int fps, int bitrate, File outputFile) {
if (rotation > 0) {
mWidth = height;
mHeight = width;
} else {
mWidth = width;
mHeight = height;
}
BIT_RATE = bitrate;
FRAME_RATE = fps;
mOutputFile = outputFile;
String outputFileString;
try {
outputFileString = outputFile.getCanonicalPath();
} catch (IOException e) {
Log.e(TAG, "Unable to get path for " + outputFile);
return;
}
MediaCodecInfo codecInfo = selectCodec(MIME_TYPE);
if (codecInfo == null) {
Log.e(TAG, "Unable to find an appropriate codec for " + MIME_TYPE);
return;
}
Log.d(TAG, "found codec: " + codecInfo.getName());
int colorFormat;
try {
colorFormat = MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar;
} catch (Exception e) {
colorFormat = MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar;
}
try {
mediaCodec = MediaCodec.createByCodecName(codecInfo.getName());
} catch (IOException e) {
Log.e(TAG, "Unable to create MediaCodec " + e.getMessage());
return;
}
MediaFormat mediaFormat = MediaFormat.createVideoFormat(MIME_TYPE, mWidth, mHeight);
mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);
mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE);
mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, colorFormat);
mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, I_FRAME_INTERVAL);
mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mediaCodec.start();
try {
mediaMuxer = new MediaMuxer(outputFileString, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
} catch (IOException e) {
Log.e(TAG, "MediaMuxer creation failed. " + e.getMessage());
return;
}
Log.d(TAG, "Initialization complete. Starting encoder...");
encode(bitmaps);
}
As you can see, I managed to make it work by swapping the width and height of mediaCodec
but the output video resolution is changed and it is not acceptable.
here are my questions:
- is there any way to make MediaMetadataRetriever not considering METADATA_KEY_VIDEO_ROTATION value?
- is there a way to remove this metadata from mp4 file?
- is it possible for
mediaCodec
to detect rotation and generate the right video output?
I can't use FFmpeg
library, I already tried using mp4parser
and jcodec
for changing rotation metadata(or removing it) and none of them has worked.
MP4Parser:
Movie inMovie = MovieCreator.build(parameters.getSourceVideoPath());
inMovie.setMatrix(Matrix.ROTATE_0);
BasicContainer out = (BasicContainer) new DefaultMp4Builder().build(inMovie);
WritableByteChannel fc = new RandomAccessFile(
String.format(parameters.getDestinationVideoDirectory() + parameters.get_fileName()), "rw").getChannel();
out.writeContainer(fc);
fc.close();
when I using mp4parser for changing metadata nothing happens and the rotation value of MediaMetadataRetriever is still the same.