3

My goal is to take in a M4V video file, decode a segment of the video as PNG frames, modify these frames, and re-encode the trimmed video (also to M4V).

The workflow is like so: [Input Video] -> Export Frames -> Modify Frames -> Encode Frames -> [Output Video].

For the decode process, I have been referencing the bigflake examples. Using the ExtractMpegFramesTest example code I was able to generate Bitmap frames from an .m4v file and export frames as PNG files.

Now I am attempting the re-encoding process, using the EncodeAndMuxTest example in attempts to create another set of classes for encoding.

The issue I am running into is, the example code seems to generate raw frames in OpenGL. I have a series of Bitmaps that I want to encode/render to the CodecInputSurface object. Pretty much the reverse of what the decoding process does.

The majority of the example code is just fine, it seems I just need to modify generateSurfaceFrame() to render the Bitmap to the Surface with OpenGL.

Here is the code that I have thus far:

// Member variables (see EncodeAndMuxTest example)
private MediaCodec encoder;
private CodeInputSurface inputSurface;
private MediaMuxer muxer;
private int trackIndex;
private boolean hasMuxerStarted;
private MediaCodec.BufferInfo bufferInfo;

// This is called for each frame to be rendered into the video file
private void encodeFrame(Bitmap bitmap)
{
    int textureId = 0;

    try
    {
        textureId = loadTexture(bitmap);

        // render the texture here?
    }
    finally
    {
        unloadTexture(textureId);
    }
}

// Loads a texture into OpenGL
private int loadTexture(Bitmap bitmap)
{
    final int[] textures = new int[1];
    GLES20.glGenTextures(1, textures, 0);

    int textureWidth = bitmap.getWidth();
    int textureHeight = bitmap.getHeight();

    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
            GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
            GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
            GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
            GLES20.GL_CLAMP_TO_EDGE);

    return textures[0];
}

// Unloads a texture from OpenGL
private void unloadTexture(int textureId)
{
    final int[] textures = new int[1];
    textures[0] = textureId;

    GLES20.glDeleteTextures(1, textures, 0);
}

I feel like I should be able to use the STextureRender from the ExtractMpegFramesTest example to achieve similar, but it's just not clicking for me.

Another thing is performance, which I really am trying to get efficient encoding. I will be encoding 90-450 frames of video (3-15 seconds @ 30fps), so this should only take several seconds hopefully.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Erik
  • 12,730
  • 5
  • 36
  • 42
  • You can convert the Bitmap pixels to a GLES texture with glTexImage(), and then render the texture... which seems to be what you've done so far. What's not working? Some of the code in Grafika (https://github.com/google/grafika) may be of use. What sort of modifications are you performing? If it's something simple you might be able to do it with a GLES fragment shader, which would be much faster (see e.g. "Show + capture" activity in Grafika). – fadden Jun 29 '14 at 04:45
  • Woo @fadden, thanks for the comment! The operations I am performing are trim (select segment from movie), zoom/pan (scale + transform), resize frame to add letter boxing (for text), and rendering text in letterbox areas. Each of these steps requires user interaction such as pinch-to-zoom, drag-to-pan, and entering their text in the respective top/bottom areas. I am doing it all with the `Canvas`/`Paint`/`Bitmap` objects but shaders would be neat. I have a basic understanding of OpenGL but shader language is new to me. – Erik Jun 29 '14 at 20:07
  • Also, I guess it's not so much it not working as much as me having a mental block on what to do. In theory I should just be able to render to it drawing a `GL_TRIANGLE_STRIP` quad, similar to what the `STextureRender` does in the ExtractMpegFramesTest example. I did look at the Grafika example code but I was not sure where to look for the example, I'll take a look more later and see what I can come up with. – Erik Jun 29 '14 at 20:09
  • The "texture from camera" activity does some similar things -- position, zoom, and so on -- using input from the camera preview. Rendering text from GL is kind of a pain; easiest way will be to render the text to a Bitmap and convert the bitmap to a texture. You can see an example in Android Breakout (https://code.google.com/p/android-breakout/source/browse/src/com/faddensoft/breakout/TextResources.java). – fadden Jun 29 '14 at 21:16
  • Thanks! I'll definitely take a look at all this. As much as I am trying to get performance as optimal as possible, it seems that IO load to the SD card is a major bottleneck (~500ms export per frame). The `Bitmap` manipulation does not take very long and I am only doing a 'handful' of frames. I have considered keeping the Bitmaps in memory and recycling the old ones as necessary, but getting into the near-hundred MB of memory usage does make me cringe, even on the Nexus 5 level of flagship devices. – Erik Jun 30 '14 at 01:30
  • @Erik Can you please confirm if you got this to work? I'm stuck on exactly the same problem and I'm not yet able to draw bitmaps as textures on GL. Please help ? – alDiablo Jun 17 '16 at 06:18
  • @alDiablo This was for a project at another firm, so it's been a while now. As I recall, I did get something working but it was not really a performant solution. I ended up exporting the short video clips as a series of PNG images, then using image editing to superimpose text on top, and then re-encoding the video. As you can imagine, this took a few minutes to complete at times. – Erik Jul 03 '16 at 03:17
  • Has anyone ever gotten something like this to work ? I can find tons of questions and fadden seems to be the only one who knows how it works but all he refers to are the testcases from android and not a finished project that does what most people want to do ?! – NikkyD Nov 25 '16 at 15:44

3 Answers3

1

You can try Intel INDE Media Pack, it allows to modify frames, cut segments, join files and much more. The are several sample effects for frames modifications: colors modifications, text overlays an so on, and you can easily modify or add new effects. It has a nice samples set and tutorials how to build and run app: https://software.intel.com/en-us/articles/intel-inde-media-pack-for-android-tutorials-running-samples

Frames modifications are gl shaders based, like this, for example for Sepia:

@Override
protected String getFragmentShader() {
    return "#extension GL_OES_EGL_image_external : require\n" +
            "precision mediump float;\n" +
            "varying vec2 vTextureCoord;\n" +
            "uniform mat3 uWeightsMatrix;\n" +
            "uniform samplerExternalOES sTexture;\n" +
            "void main() {\n" +
            "  vec4 color = texture2D(sTexture, vTextureCoord);\n" +
            "  vec3 color_new = min(uWeightsMatrix * color.rgb, 1.0);\n" +
            "  gl_FragColor = vec4(color_new.rgb, color.a);\n" +
            "}\n";

}

where uWeightsMatrix is set to shader via getAttributeLocation and glUniformMatrix3fv

protected float[] getWeights() {
    return new float[]{
            805.0f / 2048.0f, 715.0f / 2048.0f, 557.0f / 2048.0f,
            1575.0f / 2048.0f, 1405.0f / 2048.0f, 1097.0f / 2048.0f,
            387.0f / 2048.0f, 344.0f / 2048.0f, 268.0f / 2048.0f
    };
}
Marlon
  • 1,473
  • 11
  • 13
1

I was able to render bitmap frames to a surface for encoding. I used MediaCodec + MediaMuxer to encode bitmap frames using the InputSurface and rendering the bitmaps using OpenGL.

Looks like all you are missing is open gl commands to render the texture

To fix this issue I also added some additional open gl commands to render the texture using a vertex shader.

See this project and util class for more details on rendering a texture using open gl https://github.com/rsri/Pic2Fro/blob/b4fe69b44343dab2515c3fd6e769f3370bf31312/app/src/main/java/com/pic2fro/pic2fro/util/Util.java

Calling the renderTexture(..) with the texture handle and appropriate width and height after GLUtils.texImage2D(..) in your snippet will fix all bitmap rendering issues.

See also my related answer here https://stackoverflow.com/a/49331295/7602598

David Knight
  • 43
  • 1
  • 5
0

Note that original STextureRender renders external texture from SurfaceTexture. If you would like it to render your texture (created from bitmap) you have to make the following changes:

  1. change target parameters GLES11Ext.GL_TEXTURE_EXTERNAL_OES to GLES20.GL_TEXTURE_2D

  2. change this line in shader definition "uniform samplerExternalOES sTexture;\n" to “uniform sampler2D sTexture;\n”

  3. remove st.getTransformMatrix(mSTMatrix); from drawFrame

This solution worked for me.

Michal Faber
  • 131
  • 1
  • 1