1 year ago

#366972

test-img

Ali Has

Saving some (not all) decoded H.264 video stream frames with original quality and not screen resolution

I have an H.264 video stream coming form server to Android device. I want user to be able to get single frames of this stream with original quality and not with screen resolution, and simultaneously be able to watch the video. I know that for getting such image with screen resolution I can decodeframes via MediaCodec or ExoPlayer, render them onto a TextureView and then use textureView.getBitmap(); but it just gives me the frame with screen resolution (am I right?). Alternatively, I could user OpenGL ES and get image by glReadPixels mentioned in google resources examples but I do not know if it gives the same quality or not (image size (resolution) is obviously the same):

/**
 * Saves the current frame to disk as a PNG image.  Frame starts from (0,0).
 * <p>
 * Useful for debugging.
 */
public static void saveFrame(String filename, int width, int height) {
    
    ByteBuffer buf = ByteBuffer.allocateDirect(width * height * 4);
    buf.order(ByteOrder.LITTLE_ENDIAN);
    GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
    buf.rewind();
    int pixelCount = width * height;
    int[] colors = new int[pixelCount];
    buf.asIntBuffer().get(colors);
    for (int i = 0; i < pixelCount; i++) {
        int c = colors[i];
        colors[i] = (c & 0xff00ff00) | ((c & 0x00ff0000) >> 16) | ((c & 0x000000ff) << 16);
    }
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(filename);
        Bitmap bmp = Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
        bmp.recycle();
    } catch (IOException ioe) {
        throw new RuntimeException("Failed to write file " + filename, ioe);
    } finally {
        try {
            if (fos != null) fos.close();
        } catch (IOException ioe2) {
            throw new RuntimeException("Failed to close file " + filename, ioe2);
        }
    }
    Log.d(TAG, "Saved " + width + "x" + height + " frame as '" + filename + "'");
}

android

opengl-es

h.264

android-mediacodec

textureview

0 Answers

Your Answer

Accepted video resources