VideoEditor-For-Android Documentation

repository·master·Indexed 23 days ago

https://github.com/qqchenjian318/videoeditor-for-android

A hardware-accelerated video editing project for Android (Android 4.3+) utilizing OpenGL for visual effects and MediaCodec for efficient video encoding, decoding, and audio processing. It provides core capabilities for video capture, recording, cutting, and adding background music, featuring specialized classes like ICamera for hardware control, InputSurface for encoder input, OutputSurface for decoder output, and TextureRender for GPU-filtered frame processing.

Tokens
1.8K
Snippets
0
Records
12
Agent score
79%

What's inside VideoEditor-For-Android

  1. Overview of VideoEditor-For-Android

    master

    VideoEditor-For-Android is an Android-based video editor that provides core video processing capabilities. It is designed to work with Android's hardware-accelerated APIs and is not compatible with systems below Android 4.3.

    Key features include:

    • Video Capture: Uses Android APIs for video acquisition.
    • Video Processing: Utilizes OpenGL for frame-level processing, including whitening, applying filters, and adding watermarks.
    • Encoding/Decoding: Uses Android's MediaCodec for hardware-accelerated video encoding and audio/video separation.
    • Audio Handling: Uses MediaCodec for audio mixing and processing.
    • Editing Functions: Supports video recording, cutting, and adding background music (BGM).
  2. Use OutputSurface to receive MediaCodec decoder output

    master

    The OutputSurface class manages a Surface that can be passed to MediaCodec.configure() to receive decoded video frames. It uses a SurfaceTexture to latch incoming frames and provides methods to render them using OpenGL ES.

    Workflow

    1. Initialize: Create an OutputSurface instance by passing a VideoInfo object. This prepares the GL environment and creates the underlying Surface.
    2. Configure MediaCodec: Call getSurface() to retrieve the Surface and pass it to your MediaCodec configuration.
    3. Process Frames:
      • Call awaitNewImage() to wait for and latch the next available frame from the SurfaceTexture.
      • Call drawImage() to render the latched texture onto the current EGL surface.
    4. Cleanup: Call release() to discard EGL contexts and surface resources.
  3. Use InputSurface to provide input to MediaCodec

    master

    The InputSurface class manages the EGL state required to feed video frames into a MediaCodec encoder via an OpenGL ES 2.0 context.

    To use it, you must first obtain a Surface from MediaCodec.createInputSurface(). The InputSurface then wraps this surface into an EGL window surface.

    Workflow:

    1. Initialize: Create an InputSurface instance by passing the Surface from your encoder.
    2. Prepare Rendering: Call makeCurrent() to set the EGL context and surface for the current thread.
    3. Render: Perform your OpenGL ES drawing commands.
    4. Timestamp: Call setPresentationTime(long nsecs) to set the frame's timestamp in nanoseconds.
    5. Publish: Call swapBuffers() to send the rendered frame to the video encoder.
    6. Cleanup: Call release() to destroy the EGL context, surface, and the underlying Surface.
  4. InputSurface API Reference

    master

    The InputSurface class provides the following public methods for managing the encoder input surface:

    • InputSurface(Surface surface): Constructor. Takes a Surface obtained from MediaCodec.createInputSurface(). Throws NullPointerException if the surface is null.
    • void makeCurrent(): Makes the EGL context and surface current on the calling thread. Throws RuntimeException if eglMakeCurrent fails.
    • boolean swapBuffers(): Calls eglSwapBuffers to publish the current frame to the encoder.
    • Surface getSurface(): Returns the underlying Surface that the MediaCodec receives buffers from.
    • void setPresentationTime(long nsecs): Sends the presentation time stamp to EGL in nanoseconds using EGLExt.eglPresentationTimeANDROID.
  5. Use TextureRender for video frame processing

    master

    TextureRender is an OpenGL ES 2.0 engine used to render video frames onto a surface. It supports applying GPU filters, handling video rotation, and managing different display modes like 'clip mode' (for cropping/aspect ratio adjustment) and 'zoom mode'.

    Lifecycle and Usage

    1. Initialization: Instantiate with a VideoInfo object containing video dimensions and rotation.
    2. Setup: Call surfaceCreated() after the EGL surface is ready to initialize shaders, textures, and framebuffers.
    3. Frame Preparation: Call preDraw(SurfaceTexture st) to clear the buffer and prepare the base texture.
    4. Rendering: Call drawFrame(SurfaceTexture st) to execute the actual draw call. The engine automatically switches between zoomDraw and clipDraw based on the isClipMode state.
    5. Filter Application: Use addGpuFilter(GPUImageFilter filter) to attach custom GPU effects to the rendering pipeline.
  6. OutputSurface API Reference

    master

    The OutputSurface class provides the following public methods for managing decoder output surfaces and rendering:

    MethodDescription
    OutputSurface(VideoInfo info)Constructor that prepares GL, creates a SurfaceTexture, and a Surface.
    OutputSurface(VideoInfo info, int clipMode)Constructor for clip mode (implementation details vary).
    void release()Discards all held resources, including the EGL context and the Surface.
    void makeCurrent()Makes the internal EGL context and surface current.
    Surface getSurface()Returns the Surface used for MediaCodec output.
    void awaitNewImage()Blocks until a new frame is available, then latches it via updateTexImage().
    void drawImage()Renders the current texture onto the current EGL surface.
    void addGpuFilter(GPUImageFilter filter)Adds a GPU filter to the rendering pipeline.
    void onVideoSizeChanged(VideoInfo info)Updates the internal texture renderer with new video dimensions.
  7. Release InputSurface resources

    master

    The release() method must be called to prevent resource leaks. It performs the following actions:

    1. If the current EGL context matches the InputSurface context, it clears the current context and surface using eglMakeCurrent with EGL_NO_SURFACE and EGL_NO_CONTEXT.
    2. Destroys the EGL surface via eglDestroySurface.
    3. Destroys the EGL context via eglDestroyContext.
    4. Releases the underlying Surface via mSurface.release().
    5. Nullifies all internal EGL and Surface references.
  8. Configure ICamera settings via Config class

    master

    The ICamera.Config class allows you to define hardware constraints and aspect ratios for the camera session.

    Available fields:

    • float rate: The aspect ratio (width/height). Default is 1.778f.
    • int minPreviewWidth: The minimum required width for the camera preview.
    • int minPictureWidth: The minimum required width for captured pictures.