VideoProcessor

Introduction: process video(cut & scale & increase & decrease & reverse) with mediacodec
More: Author   ReportBugs   
Tags:
视频处理-

VideoProcessor使用 Android 原生的 MediaCodec 实现视频压缩、剪辑、混音、快慢放及倒流的功能(快慢放及倒流支持音频同步变化),在支持 MediaCodec 的手机上优于使用 FFmpeg 的方案

  • 体积小 :编译后的 aar 只有 262K,ffmpeg 一个 so 就 7、8M,精简之后也差不多还有一半大小
  • 速度快 :在 huaweiP9 上压缩(1080P 20s 20000k -> 720p 2000k)
lib 耗时
VideoProcessor 13.3s
ffmpeg 172s
ffmpeg(ultrafast) 74s

Gradle

在根目录下的 build.gradle 里添加 maven 仓库

allprojects {
        repositories {
            ...
            maven { url 'https://www.jitpack.io' }
        }
    }

添加依赖 要求 minSdkVersion 21


    dependencies {
            implementation 'com.github.yellowcath:VideoProcessor:2.4.2'
    }

Changelog

2.4.2

  • 解决 arm64 的机器上 crash 的问题
  • 解决某些情况下进度计算错误的问题

2.4.0

  • 兼容 Android Q
  • 去掉对 support lib 的依赖

使用

基本用法如下

    VideoProcessor.processor(context)
            .input(inputVideoPath)
            .output(outputVideoPath)
            .outWidth(outWidth)
            .outHeight(outHeight)
            .process();

VideoProcessor 支持多种参数同时处理,完整参数如下

VideoProcessor.processor(context)
       .input(inputVideoPath) // .input(inputVideoUri)
       .output(outputVideoPath)
       //以下参数全部为可选
       .outWidth(width)
       .outHeight(height)
       .startTimeMs(startTimeMs)//用于剪辑视频
       .endTimeMs(endTimeMs)    //用于剪辑视频
       .speed(speed)            //改变视频速率,用于快慢放
       .changeAudioSpeed(changeAudioSpeed) //改变视频速率时,音频是否同步变化
       .bitrate(bitrate)       //输出视频比特率
       .frameRate(frameRate)   //帧率
       .iFrameInterval(iFrameInterval)  //关键帧距,为 0 时可输出全关键帧视频(部分机器上需为 -1)
       .progressListener(listener)      //可输出视频处理进度
       .process();

用户使用时可自行根据需要调用,例如

    //视频两倍速
    VideoProcessor.processor(context)
            .input(inputVideoPath)
            .output(outputVideoPath)
            .speed(2.0f)
            .process();

此外,其它功能

//视频逆序
VideoProcessor.reverseVideo(context, inputVideoPath, outputVideoPath,reverseAudio,listener);
//混音,支持渐入渐出
VideoProcessor.mixAudioTrack(context, inputVideoPath, aacAudioPath, outputVideoPath, startMs, endMs, videoVolume, audioVolume,fadeInSec, fadeOutSec);

Demo

enter image description here

Apps
About Me
GitHub: Trinea
Facebook: Dev Tools