Giter Site home page Giter Site logo

nanyangtaiji / simplevideoeditor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fahimfarhan/simplevideoeditor

0.0 1.0 0.0 1.43 MB

A very simple free and open-source android video editor made with Mp4Composer that allows you to crop, trim and apply filters :)

License: MIT License

Java 94.83% CMake 0.22% C 0.72% Kotlin 4.23%

simplevideoeditor's Introduction

SimpleVideoEditor

Acknowledgement

Special thanks to:

  1. MasayukiSuda/Mp4Composer-android
  2. cats-oss/android-gpuimage

๐ŸŽ‰ The library is now available via jitpack.io ๐Ÿš€

Warning Please note that I should change some file names, variables in strings.xml etc. Otherwise they may conflict with user's app resulting unprecedented problems. Sorry for inconvenience.

To import the library: Add it in your root build.gradle at the end of repositories:

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72"    // <-- ADD THIS LINE
    }
}
allprojects {
		repositories {
			// ...
			maven { url 'https://www.jitpack.io' }  // <-- ADD THIS LINE
		}
	}

Next, open your app's build.gradle file, and add this at the top:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

And in the android block, add this:

android{
    // other configs
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }
        // other configs
}

Finally, add the dependency

dependencies {
        implementation 'com.github.fahimfarhan:SimpleVideoEditor:v1.0'
}

That's it!

todo:

  1. study some photo editors like burhanrashid52/PhotoEditor, and implement text, stickers etc support

Crop Explanation

The crop mechanism was not well documented in the Mp4Composer-android library, so I had to go through a lot of trials and errors to understand what the library was actually doing. I am quite sure this is what the library input means. For simplicity, assume that our video frame does not change its shape, but our crop area changes shape / moves when the user drags / moves / does something.

crop math

Here as you can see, the video frame has height H pixel, width W pixel and so its center is (W/2, H/2).

So, AspectRatio, r = cropQuadWidth : cropQuadHeight = W:H

Suppose initially the crop quad has exactly the same dimensions as the video frame, that is, it has same height, width and center (W/2, H/2). Now the user uses pinch gesture to resize the crop quad by saving the aspect ratio r and keeping the center where it is. Now suppose the user uses a drag gesture to move the crop quad so that its new center is (x0,y0).

Therefore, the translation is

    dx = (x0 - W/2)  pixel,  
    dy = (y0 - H/2)  pixel

We can represent the crop quad with its 4 corner points: p0, p1, p2, p3 as shown in the picture. Since our aspect ratio is fixed, our crop quad also has aspect ratio r.

Our crop quads newWidth = (p3.x - p0.x) pixel newHeight = newWidth/r = (p3.y - p0.y) pixel Now for some reason you need to divide dx with newWidth, dy with newHeight and multiply them with 2. Don't ask me why. The library input wants this :/ So

    translationX = dx * 2 / newWidth;
    translationY = dy * 2 / newHeight;

Therefore, on pinch gesture, the crop quad has a scaling factor, scalingFactor = initialWidthOfCropQuad / finalWidthOfCropQuad => scalingFactor = W / newWidth = W / (p3.x - p0.x) = H / newHeight = H / (p3.y - p0.y) Just use a formula that you like. If you implement a rotation in your video editor, make sure to save it in a variable rotation = theta. I didnot use this feature, so I don't remember details (degree or radian, clockwise, anticlockwise etc) at the time of writing. Since I am not rotating, my rotation is zero. So rotation = 0

Now that we have all the input parameters ready, we can create a FillModeCustomItem object like this:

        FillModeCustomItem fillModeCustomItem = new FillModeCustomItem(
                scalingFactor,
                rotation,
                translateX,
                translateY,
                W ,         // the video Width = W pixel
                H           // the video Height = H pixel
        );

And finally pass this fillModeCustomItem into mp4composer to get the desired crop area. You need to specify the output videos height and width. Let outputVideoWidthPx = 720 pixel and outputVideoHeightPx = outputWidth/r which is the crop quad's width and height. It's not entirely clear to my how / why this works. The original developers gave a sample where the crop area was fixed and the video frame resized itself. So perhaps internally, the library takes a quad of outputVideoWidthPx, outputVideoHeightPx centered at (x0, y0), then zooms the video frame (using that scalingFactor) and translates translationX, translationY amount to track the actual crop area.

Finally, you do this in java:

        mp4Composer = new Mp4Composer(selectedVideoUri, destPath, activity, mylogger);
        mp4Composer
                .size(outputVideoWidthPx, outputVideoHeightPx) // fake it till you make it
                .fillMode(FillMode.CUSTOM)
                .customFillMode(fillModeCustomItem);
        // todo: other inputs according to your requirements
        mp4Composer.start();                

Example:

Please check out VideoEditorPresenter.java#L83, it shows my implementation.

You can also download my project, run it and try to have a better understanding. If you have any questions, feel free to ask me and I'll try my utmost to answer them. Thank you.

simplevideoeditor's People

Contributors

fahimfarhan avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.