OpenTikTok Video Editing Project

repository·master·Indexed 43 days ago

https://github.com/galis/opentiktok

An open-source video editing project for Android designed to provide a CapCut-like experience. Version 1.0 includes features such as frame-by-frame preview, text and sticker effects, video composition, and transitions. The project includes a C++ core with the ARContext class for AR engine lifecycle management, Filter3D for 3D object overlays, and FilterBase for implementing custom video filters.

Tokens
2.4K
Snippets
8
Records
13
Agent score
56%

What's inside OpenTikTok

  1. Overview of OpenTikTok

    master
    OpenTikTok is an open-source video editing project designed to mimic the functionality of editors like CapCut (剪映). It provides core video editing capabilities including frame-by-frame preview, text and sticker effects, video composition, and transitions. The project is currently in the v0.99/v1.0 phase, focusing on stability and feature expansion.
  2. Testing OpenTikTok with sample files

    master

    To test the project's functionality, you can use the testfile.mp4 located in the raw/ directory. For Android-based testing, push this file to the following cache directory:

    /data/data/com.galix.opentiktok/cache

    push testfile.mp4 to /data/data/com.galix.opentiktok/cache
  3. Planned features for v2.0

    master

    The upcoming v2.0 release is planned to include:

    • 变速播放 (Variable speed playback)
    • 混音 (Audio mixing)
    • 滤镜特效 (Filter effects)
    • 录屏 (Screen recording)
    • 调节 (Adjustments/Color correction)
  4. Available features in v1.0

    master

    The following features are implemented and available in the v1.0 release:

    • 逐帧预览 (Frame-by-frame preview)
    • 文字特效 (Text effects)
    • 贴纸特效 (Sticker effects)
    • 多段预览 (Multi-segment preview)
    • 比例 (Aspect ratio adjustment)
    • 背景 (Background settings)
    • 视频合成 (Video composition/merging)
    • 裁剪 (Cropping)
    • 转场 (Transitions)
  5. Manage AR engine lifecycle with ARContext

    master

    The ARContext class is the primary interface for managing the Augmented Reality (AR) engine lifecycle. It handles the creation, rendering, and destruction of the AR environment.

    Key lifecycle methods:

    • create(): Initializes the AR engine.
    • draw(int textureId): Renders the AR effects onto the provided OpenGL texture ID.
    • destroy(): Cleans up resources and shuts down the engine.
    • onSurfaceChanged(int width, int height): Updates the internal viewport dimensions when the rendering surface changes.
    slar::ARContext context;
    context.create();
    // ... in render loop ...
    context.draw(textureId);
    // ... on resize ...
    context.onSurfaceChanged(newWidth, newHeight);
    // ... cleanup ...
    context.destroy();
  6. Use Filter3D for 3D object overlay

    master

    The Filter3D class is a specialized filter used to overlay 3D models onto video frames. It extends FilterBase and manages 3D transformations (Model-View-Projection) to render objects using .obj files.

    To use it, you must load a 3D model file, set the spatial transformation data (position, scale, and rotation), and handle the drawing lifecycle via onDraw.

    // Example usage pattern for Filter3D
    slfilter::Filter3D filter3d;
    
    // 1. Load the 3D model
    filter3d.loadFile("path/to/model.obj");
    
    // 2. Set transformation data
    // center: Point2f (x, y)
    // scale: float
    // pitch, yaw, roll: rotation angles
    cv::Point2f center(0.5f, 0.5f);
    filter3d.setData(center, 1.0f, 0.0f, 0.0f, 0.0f);
    
    // 3. In the rendering loop
    filter3d.onDraw(textureId);
  7. Implement a custom video filter using FilterBase

    master

    To create a custom video filter, inherit from the slfilter::FilterBase class. You must manage the filter lifecycle by calling init() during initialization and destroy() when the filter is no longer needed.

    Key lifecycle hooks to override:

    • onInit(): Called during the init() process.
    • onInitialized(): Called after initialization is complete.
    • onDraw(GLuint textureId): The primary method for rendering logic. It receives the textureId of the input frame.
    • onDestroy(): Called during the destroy() process.
    • onOutputChanged(int width, int height): Called when the output dimensions change.

    Note: FilterBase can also serve as a default filter if no specific logic is provided.

    class MyCustomFilter : public slfilter::FilterBase {
    public:
        MyCustomFilter(const std::string& vShader, const std::string& fShader) 
            : FilterBase(vShader, fShader) {}
    
        void onDraw(GLuint textureId) override {
            // Custom rendering logic using textureId
        }
    };
    
    // Usage:
    MyCustomFilter filter("vertex_shader_code", "fragment_shader_code");
    filter.init();
    GLuint resultTexture = filter.draw(inputTextureId);
    filter.destroy();
  8. Configure FilterBase rendering modes and sizes

    master

    You can control how the filter outputs its data and the resolution of its internal buffers.

    Render Modes Use setRenderMode(RenderMode mode) to choose between:

    • OUTPUT: Standard output mode.
    • FRAMEBUFFER: Renders into an internal framebuffer.

    Buffer Management

    • setFrameBufferSize(const SLSize &size): Sets the resolution of the internal framebuffer.
    • getFrameBuffer(): Returns the GL texture ID of the framebuffer.
    • getFrameBufferColorTexture(): Returns the color texture ID of the framebuffer.
    • getOutputSize(): Returns the current output dimensions.
    • getFrameBufferSize(): Returns the current framebuffer dimensions.
  9. Use FilterOES for hardware-accelerated texture filtering

    master

    The FilterOES class is a hardware-accelerated implementation of FilterBase designed for OES texture filtering. It allows for efficient rendering of textures using OpenGL ES hardware acceleration.

    Key methods for managing the filter include:

    • onDraw(GLuint textureId): Performs the actual drawing operation using the provided texture ID.
    • setTextureInfo(SLSize size): Configures the dimensions of the texture being processed.
    • setBgColor(int color): Sets the background color for the filter operation.

    Note: This class is part of the slfilter namespace.

    #include "FilterOES.h"
    
    // Example usage pattern
    slfilter::FilterOES filter;
    filter.setTextureInfo({width, height});
    filter.setBgColor(0xFF000000);
    filter.onDraw(textureId);
  10. Bind attributes and uniforms to a FilterBase

    master

    Once a filter is initialized, you can pass data to its shaders using binding methods. These methods map values to shader uniforms or attributes.

    Attribute Binding

    • bindAttr(string &&key, float values[], int lineWidth = 2): Binds an array of floats as an attribute.

    Uniform/Data Binding

    • bindInt(string &&key, int value): Binds an integer.
    • bindFloat(string &&key, float value): Binds a float.
    • bindFloatArray(string &&key, float values[], int length): Binds a float array.
    • bindFloatVec3(string &&key, float *floatValue): Binds a 3-component float vector.
    • bindTexture(string &&key, GLuint texture): Binds a GL texture ID.
    • bindTexture(string &&key, SLMat &mat): Binds a matrix as a texture.
    • bindRect(string &&key, SLRect &rect): Binds a rectangle.
    • bindPoint(string &&key, SLRect &point): Binds a point.
    • bindTMat(string &&key, SLMat &mat): Binds a transformation matrix (specifically for OpenCV integration).

    All keys must match the names defined in your GLSL shader code.

    filter.bindFloat("u_opacity", 0.5f);
    filter.bindInt("u_frame_count", 120);
    filter.bindTexture("u_sampler", textureId);
  11. Reference ARConfig and CONFIG_TYPE

    master

    The ARConfig struct and CONFIG_TYPE enum define the configuration parameters for AR effects. While CONFIG_TYPE defines categories of effects, ARConfig is used to pass specific data like lipstick types and textures.

    enum CONFIG_TYPE {
        EYELINE,
        EYELASH,
        EYESHADOW,
        LIPSTICK,
        SURFACE,
    };
    
    struct ARConfig {
        string lipstickType;
        Mat lipstick;
        bool lipstickChange;
    };
  12. Filter3D API Reference

    master

    The Filter3D class provides the following public methods for managing 3D model rendering:

    • loadFile(string obj): Loads a 3D model from an OBJ file path. Returns an integer status code.
    • setData(Point2f &center, float scale, float pitch, float yaw, float roll): Configures the 3D object's position and orientation.
      • center: A cv::Point2f representing the anchor point.
      • scale: A float for scaling the object.
      • pitch, yaw, roll: Euler angles for rotation.
    • onDraw(GLuint textureId): The primary rendering call. It draws the 3D object over the provided textureId (the video frame).
    • onOutputChanged(int width, int height): Updates the internal projection matrix when the video output dimensions change.