ypresto / qtfaststart-java

qt-faststart (make mp4 file streamable) in Java, MIT licensed.
MIT License
72 stars 16 forks source link

Android : How to use https://github.com/ypresto/android-transcoder with this utility? #4

Open PankajChunchun opened 6 years ago

PankajChunchun commented 6 years ago

Hi Yuya Tanaka,

I am working on an Android project, where requirement is to transcode a video. So I am using https://github.com/ypresto/android-transcoder, but this utility does not place moov atom at beginning of file.

So I went through this utility and wanted to use it.

I did not find any documentation regarding how can I use it with https://github.com/ypresto/android-transcoder. Can you please add some documentation? Steps would also be fine, later I will add documentation for this utility and will share pull request.

Thank you.

PankajChunchun commented 6 years ago

I saw your comment in a closed issue https://github.com/ypresto/qtfaststart-java/issues/2

So what I understood is

Step1 : Transcode the video Step2: Use utility to add "moov" in transcoded video

And my implementation looks like

MediaTranscoder.Listener listener = new MediaTranscoder.Listener() {
                        @Override
                        public void onTranscodeProgress(double progress) {
                        }

                        @Override
                        public void onTranscodeCompleted() {
                            Log.d(TAG, "transcoding took " + (SystemClock.uptimeMillis() - startTime) + "ms");
                            onTranscodeFinished(true, "transcoded file placed on " + file, parcelFileDescriptor);

                            Log.d(TAG, "Adding MOOV at beging of this file...");

                            File videoToPlay = null;

                            try {
                                if (QtFastStart.fastStart(file, moovFile)) {
                                    Log.d(TAG, "Added MOOV at beging of this file...");
                                    // Use output file path to play video
                                    videoToPlay = moovFile;
                                } else {
                                    Log.d(TAG, "MOOV present in this FILE");
                                    // Use input file path, as it has moov already so output file will not exists.
                                    videoToPlay = file;
                                }

                            } catch (QtFastStart.MalformedFileException m) {
                                Log.e(TAG, m.toString());
                            } catch (QtFastStart.UnsupportedFileException q) {
                                Log.e(TAG, q.toString());
                            } catch (QtFastStart.QtFastStartException e) { // catches QtFastStartException other than MalformedFileException and UnsupportedFileException
                                Log.e(TAG, e.toString());
                            } catch (IOException e) {
                                Log.e(TAG, e.toString());
                            }

                            Log.d(TAG, "Playing video " + videoToPlay.getAbsolutePath());

                            Uri uri = FileProvider.getUriForFile(MainActivity.this, FILE_PROVIDER_AUTHORITY, videoToPlay);
                            startActivity(new Intent(Intent.ACTION_VIEW)
                                    .setDataAndType(uri, "video/mp4")
                                    .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION));
                        }

                        @Override
                        public void onTranscodeCanceled() {
                            onTranscodeFinished(false, "Transcoder canceled.", parcelFileDescriptor);
                        }

                        @Override
                        public void onTranscodeFailed(Exception exception) {
                            onTranscodeFinished(false, "Transcoder error occurred.", parcelFileDescriptor);
                        }
                    };
                    Log.d(TAG, "transcoding into " + file);
                    mFuture = MediaTranscoder.getInstance().transcodeVideo(fileDescriptor, file.getAbsolutePath(),
                            MediaFormatStrategyPresets.createAndroid720pStrategy(8000 * 1000, 128 * 1000, 1), listener);

Can you please confirm If I am doing it in correct way?

masterwok commented 6 years ago

@PankajChunchun does one have to wait for the transcoding to complete? In my use case, I want to start playing the stream immediately. This solution will not work for me.