How to reduce video size with java/kotlin on Android?

回眸只為那壹抹淺笑 提交于 2021-02-08 17:00:21

问题


I want reduce the video size at Android Studio and for upload to PlayStore needs to be compatible for 64 bits arquitecture, I tried before with ffmpeg and it compress mp4 succefully but take longer time and this solution with 3gp not include the audio. Theres another option or library to compress mp4 and 3gp with audio and video?


回答1:


Here is another library to compress videos. This library can compress videos in low, medium and high quality. Usage example:

VideoCompress.compressVideoMedium("/storage/emulated/0/Movies/source.mp4",
        "/storage/emulated/0/Movies/Compressed/compressed.mp4",
        new VideoCompress.CompressListener() {

    @Override
    public void onStart() {
        // Compression is started.
    }

    @Override
    public void onSuccess() {
        // Compression is successfully finished.
    }

    @Override
    public void onFail() {
        // Compression is failed.
    }

    @Override
    public void onProgress(float percent) {
        // Compression is in progress.
    }
});



回答2:


You can do so using this library: https://github.com/tcking/GiraffeCompressor


GiraffeCompressor.init(context);


//step 4: using compressor

GiraffeCompressor.create() //two implementations: mediacodec and ffmpeg,default is mediacodec
                  .input(inputFile) //set video to be compressed
                  .output(outputFile) //set compressed video output
                  .bitRate(bitRate)//set bitrate 码率
                  .resizeFactor(Float.parseFloat($.id(R.id.et_resize_factor).text()))//set video resize factor 分辨率缩放,默认保持原分辨率
                  .watermark("/sdcard/videoCompressor/watermarker.png")//add watermark(take a long time) 水印图片(需要长时间处理)
                  .ready()
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(new Subscriber<GiraffeCompressor.Result>() {
                      @Override
                      public void onCompleted() {
                          $.id(R.id.btn_start).enabled(true).text("start compress");
                      }

                      @Override
                      public void onError(Throwable e) {
                          e.printStackTrace();
                          $.id(R.id.btn_start).enabled(true).text("start compress");
                          $.id(R.id.tv_console).text("error:"+e.getMessage());

                      }

                      @Override
                      public void onNext(GiraffeCompressor.Result s) {
                          String msg = String.format("compress completed \ntake time:%s \nout put file:%s", s.getCostTime(), s.getOutput());
                          msg = msg + "\ninput file size:"+ Formatter.formatFileSize(getApplication(),inputFile.length());
                          msg = msg + "\nout file size:"+ Formatter.formatFileSize(getApplication(),new File(s.getOutput()).length());
                          System.out.println(msg);
                          $.id(R.id.tv_console).text(msg);
                      }
                  })


来源:https://stackoverflow.com/questions/57983099/how-to-reduce-video-size-with-java-kotlin-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!