Coder Social home page Coder Social logo

cmarixtechnolabs / video-trimming-android Goto Github PK

View Code? Open in Web Editor NEW
25.0 3.0 12.0 2.57 MB

Trim the video by adjusting starting point and ending point in Android

License: MIT License

Kotlin 1.04% Java 98.96%
video-trim video-trimmer video-trimming video video-processing video-cutter video-manipulation android android-example android-demo

video-trimming-android's Introduction

Video Trimming - Android

license : MIT license : MIT

Core Features

  • Video can be trimmed/shortened and played on the same screen
  • Video can be trimmed by selecting the starting point and ending point, and it will display the video size and video duration based on the selected position
  • Seekbar moves as per selected video for trimming
  • After trimming, trimmed video can be played automatically and can be shared on social media

How it works

  • User can record video from camera or select video from gallery
  • Set the selected video in the trimming screen
  • Trim the video by dragging starting point and end point
  • View trimmed video on the trimming screen
  • Save video, it will automatically play in next screen
  • Share the video

Purpose of this code

  • Whenever it is required to crop thr video, this code can help you
  • Whenever you are having a limiation of video recording such as allow users to record video for 1 min, this code can help you

Requirements

  • Android 4.1+

When you can use this code

  • When you are developing a Social or standalone video sharing app, this code will help you to provide functinality of trimming video and sharing video with user friendly operations.

Code Snippet

Step 1: Send Video URL To Video Trimming class

if (mVideoTrimmer != null) {
        mVideoTrimmer.setMaxDuration(60);
        mVideoTrimmer.setOnTrimVideoListener(this);
        mVideoTrimmer.setOnK4LVideoListener(this);
        //mVideoTrimmer.setDestinationPath("/storage/emulated/0/DCIM/CameraCustom/");
        mVideoTrimmer.setVideoURI(Uri.parse(path));
        mVideoTrimmer.setVideoInformationVisibility(true);
    }

Step 2: Initializtion of trimming views as we have declared in xml files

LayoutInflater.from(context).inflate(R.layout.view_time_line, this, true);

mHolderTopView = ((SeekBar) findViewById(R.id.handlerTop));
mVideoProgressIndicator = ((ProgressBarView) findViewById(R.id.timeVideoView));
mRangeSeekBarView = ((RangeSeekBarView) findViewById(R.id.timeLineBar));
mLinearVideo = ((RelativeLayout) findViewById(R.id.layout_surface_view));
mVideoView = ((VideoView) findViewById(R.id.video_loader));
mPlayView = ((ImageView) findViewById(R.id.icon_video_play));
mTimeInfoContainer = findViewById(R.id.timeText);
mTextSize = ((TextView) findViewById(R.id.textSize));
mTextTimeFrame = ((TextView) findViewById(R.id.textTimeSelection));
mTextTime = ((TextView) findViewById(R.id.textTime));
mTimeLineView = ((TimeLineView) findViewById(R.id.timeLineView));

setUpListeners();
setUpMargins();

Step 3: To prepare video as it is trimmed by user onVideoPrepared() method is used

private void onVideoPrepared(@NonNull MediaPlayer mp) {
    // Adjust the size of the video
    // so it fits on the screen
    int videoWidth = mp.getVideoWidth();
    int videoHeight = mp.getVideoHeight();
    float videoProportion = (float) videoWidth / (float) videoHeight;
    int screenWidth = mLinearVideo.getWidth();
    int screenHeight = mLinearVideo.getHeight();
    float screenProportion = (float) screenWidth / (float) screenHeight;
    ViewGroup.LayoutParams lp = mVideoView.getLayoutParams();

    if (videoProportion > screenProportion) {
        lp.width = screenWidth;
        lp.height = (int) ((float) screenWidth / videoProportion);
    } else {
        lp.width = (int) (videoProportion * (float) screenHeight);
        lp.height = screenHeight;
    }
    mVideoView.setLayoutParams(lp);

    mPlayView.setVisibility(View.VISIBLE);

    mDuration = mVideoView.getDuration();
    setSeekBarPosition();

    setTimeFrames();
    setTimeVideo(0);

    if (mOnK4LVideoListener != null) {
        mOnK4LVideoListener.onVideoPrepared();
    }
}

Step 4: Give position for seekbar fom start to end

private void setSeekBarPosition() {

    if (mDuration >= mMaxDuration) {
        mStartPosition = mDuration / 2 - mMaxDuration / 2;
        mEndPosition = mDuration / 2 + mMaxDuration / 2;

        mRangeSeekBarView.setThumbValue(0, (mStartPosition * 100) / mDuration);
        mRangeSeekBarView.setThumbValue(1, (mEndPosition * 100) / mDuration);

    } else {
        mStartPosition = 0;
        mEndPosition = mDuration;
    }

    setProgressBarPosition(mStartPosition);
    mVideoView.seekTo(mStartPosition);

    mTimeVideo = mDuration;
    mRangeSeekBarView.initMaxWidth();
}

Step 5: To give permission for seekbar to start moving from start to end, setSeekBarPosition() is used

private void setSeekBarPosition() {

    if (mDuration >= mMaxDuration) {
        mStartPosition = mDuration / 2 - mMaxDuration / 2;
        mEndPosition = mDuration / 2 + mMaxDuration / 2;

        mRangeSeekBarView.setThumbValue(0, (mStartPosition * 100) / mDuration);
        mRangeSeekBarView.setThumbValue(1, (mEndPosition * 100) / mDuration);

    } else {
        mStartPosition = 0;
        mEndPosition = mDuration;
    }
    setProgressBarPosition(mStartPosition);
    mVideoView.seekTo(mStartPosition);
    mTimeVideo = mDuration;
    mRangeSeekBarView.initMaxWidth();
}

Step 6: To synchronize video frames with time setTimeFrames() is used

private void setTimeFrames() {
    String seconds = getContext().getString(R.string.short_seconds);
    mTextTimeFrame.setText(String.format("%s %s - %s %s", stringForTime(mStartPosition), seconds, stringForTime(mEndPosition), seconds));
}

Step 7: Set Start and end point of that video using onSeekThumbs() method

private void onSeekThumbs(int index, float value) {
    switch (index) {
        case Thumb.LEFT: {
            mStartPosition = (int) ((mDuration * value) / 100L);
            mVideoView.seekTo(mStartPosition);
            break;
        }
        case Thumb.RIGHT: {
            mEndPosition = (int) ((mDuration * value) / 100L);
            break;
        }
    }
    setProgressBarPosition(mStartPosition);

    setTimeFrames();
    mTimeVideo = mEndPosition - mStartPosition;
}

Let us know!

We’d be really happy if you sent us links to your projects where you use our component. Just send an email to [email protected] and do let us know if you have any questions or suggestion regarding video trimming in Android.

P.S. We’re going to publish more awesomeness examples on third party libraries, coding standards, plugins etc, in all the technology. Stay tuned!

Stay Socially Connected

Get more familiar with our work by visiting few of our portfolio links.

Portfolio | Facebook | Twitter | Linkedin | Behance | Instagram | Dribbble | Uplabs

Please don’t forget to follow them.

License

MIT License

Copyright © 2019 CMARIX TechnoLabs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

video-trimming-android's People

Contributors

atmanrathod avatar john-cmarix avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

video-trimming-android's Issues

Android 12 and above version crash

E/GlideExecutor: Request threw uncaught throwable
java.lang.IllegalStateException: Only owner is able to interact with pending item content://media/external_primary/video/media/35?requireOriginal=1
at android.os.Parcel.createExceptionOrNull(Parcel.java:2433)
at android.os.Parcel.createException(Parcel.java:2409)
at android.os.Parcel.readException(Parcel.java:2392)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190)
at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:153)
at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:780)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:2027)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1842)
at android.content.ContentResolver.openInputStream(ContentResolver.java:1518)
at com.bumptech.glide.load.data.StreamLocalUriFetcher.loadResourceFromUri(StreamLocalUriFetcher.java:74)
at com.bumptech.glide.load.data.StreamLocalUriFetcher.loadResource(StreamLocalUriFetcher.java:50)
at com.bumptech.glide.load.data.StreamLocalUriFetcher.loadResource(StreamLocalUriFetcher.java:13)
at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:44)
at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:100)
at com.bumptech.glide.load.model.stream.QMediaStoreUriLoader$QMediaStoreUriFetcher.loadData(QMediaStoreUriLoader.java:141)
at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:100)
at com.bumptech.glide.load.engine.SourceGenerator.startNextLoad(SourceGenerator.java:70)
at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:63)
at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:311)
at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:280)
at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:235)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:920)
at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:393)
E/GlideExecutor: Request threw uncaught throwable
java.lang.IllegalStateException: Only owner is able to interact with pending item content://media/external_primary/video/media/35?requireOriginal=1
at android.os.Parcel.createExceptionOrNull(Parcel.java:2433)
at android.os.Parcel.createException(Parcel.java:2409)
at android.os.Parcel.readException(Parcel.java:2392)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190)
at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:153)
at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:780)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:2027)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1842)
at android.content.ContentResolver.openInputStream(ContentResolver.java:1518)
at com.bumptech.glide.load.data.StreamLocalUriFetcher.loadResourceFromUri(StreamLocalUriFetcher.java:74)
at com.bumptech.glide.load.data.StreamLocalUriFetcher.loadResource(StreamLocalUriFetcher.java:50)
at com.bumptech.glide.load.data.StreamLocalUriFetcher.loadResource(StreamLocalUriFetcher.java:13)
at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:44)
at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:100)
at com.bumptech.glide.load.model.stream.QMediaStoreUriLoader$QMediaStoreUriFetcher.loadData(QMediaStoreUriLoader.java:141)
at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:100)
at com.bumptech.glide.load.engine.SourceGenerator.startNextLoad(SourceGenerator.java:70)
at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:63)
at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:311)
at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:280)
at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:235)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:920)
at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:393)

multi trim

I want to do multi trim of a video, the duration of video is variable and I want trim in parts of 10 secs.

I would like to know how use this library for this case?

Thanks.

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.