Search code examples
androidcompression

Silicompressor no sound after video is compressed


I have been compressing Images without any problems. Recently my app required video compress functionality to be added. Again, Silicompressor does the job but for some reason there is no audio in output file.

I am using code inside Runnable

String videoUriStr; // Passed as a variable

String folderPath = activity.getExternalFilesDir("/").getAbsolutePath() + "/My Folder";
String videoPath = SiliCompressor.with(context).compressVideo(videoUriStr, folderPath);

I am able to play compressed video without sound (though on uploading to firebase storage, url shows black screen but right length of video. But I will figure that out). My main concern is the sound.

I have googled a lot and found 2 solutions out of which second seems to be promising but looking way to implement it.

First is to downgrade implementation 'com.googlecode.mp4parser:isoparser:1.1.22' to implementation 'com.googlecode.mp4parser:isoparser:1.0.6', which doesn't work.

Second, which I found here, says "issue seems to be with audio encoder, I changed audio encoder to HE_AAC it works now". Can please anyone help me with this?


Solution

  • So answering my own question. Didn't get much of help on this, hence decided to switch library to FFmpeg.

    Gradle

    implementation 'com.writingminds:FFmpegAndroid:0.3.2'
    

    Load Library:

    FFmpeg fFmpeg;
    
    private void LoadFfmpegLibrary(){
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy_hh-mm", Locale.CANADA);
        Date now = new Date();
        String videoFileName = "Video-" + formatter.format(now) + ".mp4";
        if(fFmpeg == null){
            fFmpeg = FFmpeg.getInstance(context);
            try {
                fFmpeg.loadBinary(new LoadBinaryResponseHandler() {
                    @Override
                    public void onStart() {
                    }
    
                    @Override
                    public void onFailure() {
                    }
    
                    @Override
                    public void onSuccess() {
                    }
    
                    @Override
                    public void onFinish() {
                        Compress(videoFileName);
                    }
                });
            } catch (FFmpegNotSupportedException e) {
                e.printStackTrace();
            }
        } else {
            Compress(videoFileName);
        }
    }
    

    Then Compress videos

    private void Compress(String fileName){
        String outputPath = getExternalFilesDir("/").getAbsolutePath() + "/My Folder/" + fileName;
        String[] commandArray;
        commandArray = new String[]{"-y", "-i", originalFileUriStr, "-s", "720x480", "-r", "25", "-vcodec", "mpeg4", "-b:v", "300k", "-b:a", "48000", "-ac", "2", "-ar", "22050", outputPath};
        final ProgressDialog dialog = new ProgressDialog(activity);
        try {
            fFmpeg.execute(commandArray, new ExecuteBinaryResponseHandler() {
                @Override
                public void onStart() {
                    dialog.setMessage("Compressing... please wait");
                }
                @Override
                public void onProgress(String message) {
                    Log.e("FFmpeg onProgress? ", message);
                }
                @Override
                public void onFailure(String message) {
                    Log.e("FFmpeg onFailure? ", message);
                }
                @Override
                public void onSuccess(String message) {
                    Log.e("FFmpeg onSuccess? ", message);
    
                }
                @Override
                public void onFinish() {
                    Uri compressedOutputUri = Uri.fromFile(new File(outputPath));
                }
            });
        } catch (FFmpegCommandAlreadyRunningException e) {
            e.printStackTrace();
        }
    }
    

    And now sound works perfect.