linkedin / LiTr

Lightweight hardware accelerated video/audio transcoder for Android.
BSD 2-Clause "Simplified" License
611 stars 85 forks source link

Help with the implementation of listener #175

Open AniruddhaChattopadhyay opened 2 years ago

AniruddhaChattopadhyay commented 2 years ago

Hi, first of all thanks a lot for making LiTr and keeping it opensource. I need a help. I am a bit confused as to how to implement the listener. What I am trying to do is basically take a video, change the width and height of the video keeping the aspect ratio sama and reduce the bitrate of both the video and audio track (Basically trying to reduce the size of the video before uploading to firebase). I have used the TransformationListener but seems like the methods of the interface are not being triggered. I have used debugger to track whether the onStart or onComplete function is being triggered but apparently they are not. I am attaching the code snippet here that I have used. I would really appreciate if someone can guide me as to what mistake I am doing. Also after the video is transcoded how do I access the URI of the transcoded video??

private void compressAndUpload(final Uri fileUri, final String fileName, final StorageReference photoRef, final String uploadPath, final String groupName, final String feedName, FeedPostData feedPostData) {
        File f = new File(getFilesDir().getAbsolutePath() + "/" + getPackageName() + "/media/videos");
        if (f.mkdirs() || f.isDirectory()) {
            //compress and output new video specs
            Log.d(TAG, "inside if to async task");
//            new VideoCompressAsyncTask(this, this, fileUri, fileName, photoRef, uploadPath, groupName, feedName, feedPostData).execute(fileUri.toString(), f.getPath());
            TransformationListener listener;
            listener = new TransformationListener() {
                @Override
                public void onStarted(@NonNull String id) {
                    Log.d(TAG, "onStarted: ");
                }

                @Override
                public void onProgress(@NonNull String id, float progress) {
                    Log.d(TAG, "onStarted: ");
                }

                @Override
                public void onCompleted(@NonNull String id, @Nullable List<TrackTransformationInfo> trackTransformationInfos) {
                    Log.d(TAG, "onStarted: ");
                    uploadFile(Uri.fromFile(new File(f.getPath())), photoRef, groupName, feedName, feedPostData);
                }

                @Override
                public void onCancelled(@NonNull String id, @Nullable List<TrackTransformationInfo> trackTransformationInfos) {

                }

                @Override
                public void onError(@NonNull String id, @Nullable Throwable cause, @Nullable List<TrackTransformationInfo> trackTransformationInfos) {

                }
            };
            String filePath = null;
            try {

                MediaMetadataRetriever retriever = new MediaMetadataRetriever();
                retriever.setDataSource(Uri.parse(fileUri.toString()).getPath());

                MediaExtractor mediaExtractor = new MediaExtractor();
                mediaExtractor.setDataSource(Uri.parse(fileUri.toString()).getPath());
                MediaFormat inputFormat = selectVideoTrack(mediaExtractor);

                MediaFormat mediaFormat = MediaFormat.createVideoFormat(
                        MediaFormat.MIMETYPE_VIDEO_AVC,
                        inputFormat.getInteger(MediaFormat.KEY_WIDTH),
                        inputFormat.getInteger(MediaFormat.KEY_HEIGHT));

                int bitrate = Integer.parseInt(retriever.extractMetadata((MediaMetadataRetriever.METADATA_KEY_BITRATE)));

                Log.d(TAG, "OG bitrate is : " + bitrate);

                int destBitrate = bitrate;
                if (bitrate > 5 * 1024 * 1024 || new File(fileUri.toString()).length() > 10 * 1024 * 1024) {
                    destBitrate = Math.min(bitrate / 2, 5 * 1024 * 1024); // max Bitrate allowed = 5Mbps
                }

                mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, destBitrate );
                MediaTransformer mediaTransformer = new MediaTransformer(getApplicationContext());

                mediaTransformer.transform(UUID.randomUUID().toString(),
                        Uri.parse(fileUri.toString()),
                        Uri.fromFile(new File(f.getPath())).getPath(),
                        mediaFormat,
                        null,
                        listener,
                        null);

            } catch (IOException e) {
                FirebaseCrashlytics.getInstance().recordException(e);
                e.printStackTrace();
            }
        }
    } 
izzytwosheds commented 2 years ago

Hi! Glad to hear you are enjoying LiTr! Overall code looks good. Try adding frame rate to your target format, that should help with getting things to work. Sine you are passing target file path into LiTr, you should have access to it within your app. On later Android versions you should use FilProvider to read from it.

izzytwosheds commented 2 years ago

Also, I just noticed that you are trying to use an original bitrate provided by MediaMetadataRetriever. That is not always available, unfortunately. Often times it produces -1. LiTr will not work with that value, you are expected to provide valid integer value. Can you try to use something like 5000000?