Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compression always gives empty bytes #27

Open
architpoddar opened this issue May 8, 2020 · 5 comments
Open

Compression always gives empty bytes #27

architpoddar opened this issue May 8, 2020 · 5 comments

Comments

@architpoddar
Copy link

architpoddar commented May 8, 2020

Package` id="Xamarin.Android.MP4Transcoder" version="1.0.47"

Hi, I'm trying to use this plugin for compressing videos to 720p.

But for some phones, the resultant file is of 0 bytes, ie, it doesn't get created. It works fine for some phones too.

Here is the code that I'm using:

public async Task CompressVideo(FileDto file, bool deleteOldFile = false)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
{
Java.IO.File oldVideo = new Java.IO.File(file.FilePath);

            var path = Application.Context.GetExternalFilesDir(null).AbsolutePath;

            Java.IO.File videoFolder = new Java.IO.File(path, "Movies");

            if (!videoFolder.Exists())
            {
                videoFolder.Mkdirs();
            }

            var videoFilePath = Path.Combine(videoFolder.AbsolutePath, file.FileName);

            //Java.IO.File newVideo = new Java.IO.File(videoFilePath);
            Java.IO.File newVideo = new Java.IO.File(oldVideo.CanonicalPath.Replace(".mp4", "_720p.mp4"));

            await Xamarin.MP4Transcoder.Transcoder.For960x540Format().ConvertAsync(oldVideo, newVideo);

            FileStream fileStream = new FileStream(newVideo.Path, FileMode.Open, FileAccess.ReadWrite);

            var compressedFile = new FileDto();
            compressedFile.Bytes = fileStream.ToByteArray();
            compressedFile.Extension = file.Extension;
            compressedFile.FilePath = videoFilePath;
            compressedFile.FileName = DateTime.Now.ToString("yyyyMMddHHmmss") + file.Extension;
            return compressedFile;
        }
        else
        {
            return file;
        }
    }

Could you please help me out?

@ackava
Copy link
Contributor

ackava commented Jul 7, 2020

Can you catch exception and print it? This feature depends upon MP4 transcoder that is part of Android itself, very old devices and some devices had buggy implementation so it might be the case.

@pratikarya
Copy link

@ackava I have used the same code as shown above by @architpoddar and the video bytes is 0.
Device: Redmi Note 8.
Version: Android 9.
Nuget Version: 1.0.47

Following is the exception logged in the output window:

11-12 15:41:46.825 I/Choreographer(25148): Skipped 225 frames! The application may be doing too much work on its main thread.
11-12 15:41:46.843 I/OpenGLRenderer(25148): Davey! duration=3770ms; Flags=0, IntendedVsync=111646393231745, Vsync=111650143231595, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=111650150563441, AnimationStart=111650151442451, PerformTraversalsStart=111650151451253, DrawStart=111650156702816, SyncQueued=111650157814639, SyncStart=111650158223284, IssueDrawCommandsStart=111650159509014, SwapBuffers=111650162789639, FrameCompleted=111650164139430, DequeueBufferDuration=343000, QueueBufferDuration=603000,
11-12 15:41:48.795 D/Mono (25148): Requesting loading reference 2 (of 4) of /storage/emulated/0/Android/data/com.sampleapp/files/.override/Xamarin.MP4Transcoder.dll
11-12 15:41:48.795 D/Mono (25148): Loading reference 2 of /storage/emulated/0/Android/data/com.sampleapp/files/.override/Xamarin.MP4Transcoder.dll asmctx DEFAULT, looking for Java.Interop, Version=0.1.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065
11-12 15:41:48.795 D/Mono (25148): Assembly Ref addref Xamarin.MP4Transcoder[0x7b3300e380] -> Java.Interop[0x7b33018780]: 21
11-12 15:41:48.826 D/Mono (25148): Requesting loading reference 3 (of 4) of /storage/emulated/0/Android/data/com.sampleapp/files/.override/Xamarin.MP4Transcoder.dll
11-12 15:41:48.826 D/Mono (25148): Loading reference 3 of /storage/emulated/0/Android/data/com.sampleapp/files/.override/Xamarin.MP4Transcoder.dll asmctx DEFAULT, looking for System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
11-12 15:41:48.827 D/Mono (25148): Assembly Ref addref Xamarin.MP4Transcoder[0x7b3300e380] -> System[0x7b4ee37f80]: 22
11-12 15:41:48.866 W/FileSource(25148): offset/length adjusted from 0/576460752303423487 to 0/23666879
11-12 15:41:48.973 D/MediaTranscoderEngine(25148): Duration (us): 9548000
11-12 15:41:49.026 D/ExportPreset960x540Strategy(25148): inputFormat: 1920x1080 => outputFormat: 960x540
11-12 15:41:49.041 I/OMXClient(25148): IOmx service obtained
11-12 15:41:49.116 I/ExtendedACodec(25148): setupVideoEncoder()
11-12 15:41:49.116 W/OMXUtils(25148): do not know color format 0x7fa30c06 = 2141391878
11-12 15:41:49.117 W/OMXUtils(25148): do not know color format 0x7fa30c04 = 2141391876
11-12 15:41:49.117 W/OMXUtils(25148): do not know color format 0x7fa30c00 = 2141391872
11-12 15:41:49.117 W/OMXUtils(25148): do not know color format 0x7fa30c09 = 2141391881
11-12 15:41:49.118 W/OMXUtils(25148): do not know color format 0x7fa30c0a = 2141391882
11-12 15:41:49.118 W/OMXUtils(25148): do not know color format 0x7fa30c08 = 2141391880
11-12 15:41:49.118 W/OMXUtils(25148): do not know color format 0x7fa30c07 = 2141391879
11-12 15:41:49.119 W/OMXUtils(25148): do not know color format 0x7f000789 = 2130708361
11-12 15:41:49.141 I/ACodec (25148): setupAVCEncoderParameters with [profile: High] [level: Level1]
11-12 15:41:49.147 I/ACodec (25148): [OMX.qcom.video.encoder.avc] cannot encode HDR static metadata. Ignoring.
11-12 15:41:49.147 I/ACodec (25148): setupVideoEncoder succeeded
11-12 15:41:49.148 I/ExtendedACodec(25148): [OMX.qcom.video.encoder.avc] configure, AMessage : AMessage(what = 'conf', target = 1) = {
11-12 15:41:49.148 I/ExtendedACodec(25148): int32_t color-format = 2130708361
11-12 15:41:49.148 I/ExtendedACodec(25148): int32_t i-frame-interval = 1
11-12 15:41:49.148 I/ExtendedACodec(25148): string mime = "video/avc"
11-12 15:41:49.148 I/ExtendedACodec(25148): int32_t width = 960
11-12 15:41:49.148 I/ExtendedACodec(25148): int32_t bitrate = 5500000
11-12 15:41:49.148 I/ExtendedACodec(25148): int32_t frame-rate = 30
11-12 15:41:49.148 I/ExtendedACodec(25148): int32_t height = 540
11-12 15:41:49.148 I/ExtendedACodec(25148): int32_t encoder = 1
11-12 15:41:49.148 I/ExtendedACodec(25148): }
11-12 15:41:49.149 W/OMXUtils(25148): do not know color format 0x7f000789 = 2130708361
11-12 15:41:49.300 I/OMXClient(25148): IOmx service obtained
11-12 15:41:49.335 D/SurfaceUtils(25148): connecting to surface 0x7b2a73e010, reason connectToSurface
11-12 15:41:49.335 I/MediaCodec(25148): [OMX.qcom.video.decoder.avc] setting surface generation to 25751553
11-12 15:41:49.335 D/SurfaceUtils(25148): disconnecting from surface 0x7b2a73e010, reason connectToSurface(reconnect)
11-12 15:41:49.335 D/SurfaceUtils(25148): connecting to surface 0x7b2a73e010, reason connectToSurface(reconnect)
11-12 15:41:49.336 I/ExtendedACodec(25148): setupVideoDecoder()
11-12 15:41:49.338 I/ExtendedACodec(25148): Decoder will be in frame by frame mode
11-12 15:41:49.367 D/SurfaceUtils(25148): set up nativeWindow 0x7b2a73e010 for 1920x1080, color 0x7fa30c06, rotation 0, usage 0x20002900
11-12 15:41:49.432 D/SurfaceUtils(25148): set up nativeWindow 0x7b2a73e010 for 1920x1088, color 0x7fa30c06, rotation 0, usage 0x20002900
11-12 15:41:49.505 D/ACodec (25148): dataspace changed to 0x10c10000 (R:2(Limited), P:3(BT601_6_625), M:3(BT601_6), T:3(SMPTE170M)) (R:2(Limited), S:2(BT601_625), T:3(SMPTE_170M))
11-12 15:41:49.550 D/SurfaceUtils(25148): disconnecting from surface 0x7b2a73e010, reason disconnectFromSurface
11-12 15:41:49.594 E/MediaTranscoder(25148): Fatal error while transcoding, this might be invalid format or bug in engine or Android.
11-12 15:41:49.594 E/MediaTranscoder(25148): net.ypresto.androidtranscoder.engine.InvalidOutputFormatException: Non-baseline AVC video profile is not supported by Android OS, actual profile_idc: 100
11-12 15:41:49.594 E/MediaTranscoder(25148): at net.ypresto.androidtranscoder.engine.MediaFormatValidator.validateVideoOutputFormat(MediaFormatValidator.java:40)
11-12 15:41:49.594 E/MediaTranscoder(25148): at net.ypresto.androidtranscoder.engine.MediaTranscoderEngine$1.onDetermineOutputFormat(MediaTranscoderEngine.java:162)
11-12 15:41:49.594 E/MediaTranscoder(25148): at net.ypresto.androidtranscoder.engine.QueuedMuxer.onSetOutputFormat(QueuedMuxer.java:66)
11-12 15:41:49.594 E/MediaTranscoder(25148): at net.ypresto.androidtranscoder.engine.QueuedMuxer.setOutputFormat(QueuedMuxer.java:61)
11-12 15:41:49.594 E/MediaTranscoder(25148): at net.ypresto.androidtranscoder.engine.VideoTrackTranscoder.drainEncoder(VideoTrackTranscoder.java:207)
11-12 15:41:49.594 E/MediaTranscoder(25148): at net.ypresto.androidtranscoder.engine.VideoTrackTranscoder.stepPipeline(VideoTrackTranscoder.java:105)
11-12 15:41:49.594 E/MediaTranscoder(25148): at net.ypresto.androidtranscoder.engine.MediaTranscoderEngine.runPipelines(MediaTranscoderEngine.java:191)
11-12 15:41:49.594 E/MediaTranscoder(25148): at net.ypresto.androidtranscoder.engine.MediaTranscoderEngine.transcodeVideo(MediaTranscoderEngine.java:97)
11-12 15:41:49.594 E/MediaTranscoder(25148): at net.ypresto.androidtranscoder.MediaTranscoder$4.call(MediaTranscoder.java:184)
11-12 15:41:49.594 E/MediaTranscoder(25148): at net.ypresto.androidtranscoder.MediaTranscoder$4.call(MediaTranscoder.java:166)
11-12 15:41:49.594 E/MediaTranscoder(25148): at java.util.concurrent.FutureTask.run(FutureTask.java:266)
11-12 15:41:49.594 E/MediaTranscoder(25148): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
11-12 15:41:49.594 E/MediaTranscoder(25148): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
11-12 15:41:49.594 E/MediaTranscoder(25148): at java.lang.Thread.run(Thread.java:764)
[0:] Fail: Non-baseline AVC video profile is not supported by Android OS, actual profile_idc: 100 Net.Ypresto.Androidtranscoder.Engine.InvalidOutputFormatException: Non-baseline AVC video profile is not supported by Android OS, actual profile_idc: 100
at Xamarin.MP4Transcoder.Transcoder.ConvertAsync (Java.IO.File inputFile, Java.IO.File outputFile, System.Action1[T] progress) [0x00131] in C:\projects\xamarin-android-ffmpeg\Xamarin.MP4Transcoder\Transcoder.cs:110 --- End of managed Net.Ypresto.Androidtranscoder.Engine.InvalidOutputFormatException stack trace --- net.ypresto.androidtranscoder.engine.InvalidOutputFormatException: Non-baseline AVC video profile is not supported by Android OS, actual profile_idc: 100 at net.ypresto.androidtranscoder.engine.MediaFormatValidator.validateVideoOutputFormat(MediaFormatValidator.java:40) at net.ypresto.androidtranscoder.engine.MediaTranscoderEngine$1.onDetermineOutputFormat(MediaTranscoderEngine.java:162) at net.ypresto.androidtranscoder.engine.QueuedMuxer.onSetOutputFormat(QueuedMuxer.java:66) at net.ypresto.androidtranscoder.engine.QueuedMuxer.setOutputFormat(QueuedMuxer.java:61) at net.ypresto.androidtranscoder.engine.VideoTrackTranscoder.drainEncoder(VideoTrackTranscoder.java:207) at net.ypresto.androidtranscoder.engine.VideoTrackTranscoder.stepPipeline(VideoTrackTranscoder.java:105) at net.ypresto.androidtranscoder.engine.MediaTranscoderEngine.runPipelines(MediaTranscoderEngine.java:191) at net.ypresto.androidtranscoder.engine.MediaTranscoderEngine.transcodeVideo(MediaTranscoderEngine.java:97) at net.ypresto.androidtranscoder.MediaTranscoder$4.call(MediaTranscoder.java:184) at net.ypresto.androidtranscoder.MediaTranscoder$4.call(MediaTranscoder.java:166) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764) [0:] at System.Diagnostics.DefaultTraceListener.Fail (System.String message, System.String detailMessage) [0x00008] in <fcd36fbef7584569b5beddca89355f05>:0 at System.Diagnostics.TraceInternal.Fail (System.String message, System.String detailMessage) [0x000a2] in <fcd36fbef7584569b5beddca89355f05>:0 at System.Diagnostics.Debug.Fail (System.String message, System.String detailMessage) [0x00000] in <fcd36fbef7584569b5beddca89355f05>:0 at Xamarin.MP4Transcoder.Transcoder.ConvertAsync (Java.IO.File inputFile, Java.IO.File outputFile, System.Action1[T] progress) [0x001c4] in <42779b9b5f8746e58eadc53ed143c59f>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext (System.Object stateMachine) [0x00000] in <0381c8f952fb47759ee29160e807b17d>:0
at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00071] in <0381c8f952fb47759ee29160e807b17d>:0
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <0381c8f952fb47759ee29160e807b17d>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00024] in <0381c8f952fb47759ee29160e807b17d>:0
at System.Threading.Tasks.AwaitTaskContinuation.InvokeAction (System.Object state) [0x00000] in <0381c8f952fb47759ee29160e807b17d>:0
at System.Threading.Tasks.AwaitTaskContinuation.RunCallback (System.Threading.ContextCallback callback, System.Object state, System.Threading.Tasks.Task& currentTask) [0x0000f] in <0381c8f952fb47759ee29160e807b17d>:0
at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.Run (System.Threading.Tasks.Task ignored, System.Boolean canInlineContinuationTask) [0x00010] in <0381c8f952fb47759ee29160e807b17d>:0
at System.Threading.Tasks.Task.FinishContinuations () [0x000a3] in <0381c8f952fb47759ee29160e807b17d>:0
at System.Threading.Tasks.Task.FinishStageThree () [0x0003c] in <0381c8f952fb47759ee29160e807b17d>:0
at System.Threading.Tasks.Task.FinishStageTwo () [0x000a1] in <0381c8f952fb47759ee29160e807b17d>:0
at System.Threading.Tasks.Task.Finish (System.Boolean bUserDelegateExecuted) [0x00003] in <0381c8f952fb47759ee29160e807b17d>:0
at System.Threading.Tasks.Task.TrySetException (System.Object exceptionObject) [0x00023] in <0381c8f952fb47759ee29160e807b17d>:0
at System.Threading.Tasks.TaskCompletionSource`1[TResult].TrySetException (System.Exception exception) [0x0000a] in <0381c8f952fb47759ee29160e807b17d>:0
at Xamarin.MP4Transcoder.Transcoder+<>c__DisplayClass7_1.b__0 (Java.Lang.Exception e) [0x00001] in <42779b9b5f8746e58eadc53ed143c59f>:0
at Xamarin.MP4Transcoder.TranscoderListener.Net.Ypresto.Androidtranscoder.MediaTranscoder.IListener.OnTranscodeFailed (Java.Lang.Exception p0) [0x00001] in <42779b9b5f8746e58eadc53ed143c59f>:0
at Net.Ypresto.Androidtranscoder.MediaTranscoder+IListenerInvoker.n_OnTranscodeFailed_Ljava_lang_Exception_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_p0) [0x00012] in <42779b9b5f8746e58eadc53ed143c59f>:0
at Android.Runtime.DynamicMethodNameCounter.128 (System.IntPtr , System.IntPtr , System.IntPtr ) [0x00000] in <7d2292394f8c488b97f5bc2a0ac0240d>:0

@ackava
Copy link
Contributor

ackava commented Dec 15, 2020

If you read line Non-baseline AVC video profile is not supported by Android OS in the exception, the video profile is not supported by Android OS. However, I have found new ffmpeg library version and I will be adding it soon, watch out for updates.

@architpoddar
Copy link
Author

Thank you so much @ackava. Will be waiting for the update. Really need to get this working.

@cfyasar
Copy link

cfyasar commented Jan 4, 2021

Hi, i have the same issue. i'm using android 9.0. And the code always gives me a 0 kb output file. Could you help me?

public async Task ConvertFileAsync(string path)
{
try
{

            Action<string> logger = null;
            Action<int, int> onProgress = null;

            File inputFile = new File(path);


            FileHandling fileHandling = new FileHandling();
            File ouputFile = fileHandling.SaveSentVideos(inputFile);
            ouputFile.DeleteOnExit();

            List<string> cmd = new List<string>();
            cmd.Add("-y");
            cmd.Add("-i");
            cmd.Add(inputFile.CanonicalPath);

            MediaMetadataRetriever m = new MediaMetadataRetriever();
            m.SetDataSource(inputFile.CanonicalPath);

            string rotate = m.ExtractMetadata(Android.Media.MetadataKey.VideoRotation);

            int r = 0;

            if (!string.IsNullOrWhiteSpace(rotate))
            {
                r = int.Parse(rotate);
            }

            cmd.Add("-b:v");
            cmd.Add("1M");

            cmd.Add("-b:a");
            cmd.Add("128k");


            switch (r)
            {
                case 270:
                    cmd.Add("-vf scale=-1:480,transpose=cclock");
                    break;
                case 180:
                    cmd.Add("-vf scale=-1:480,transpose=cclock,transpose=cclock");
                    break;
                case 90:
                    cmd.Add("-vf scale=480:-1,transpose=clock");
                    break;
                case 0:
                    cmd.Add("-vf scale=-1:480");
                    break;
                default:

                    break;
            }

            cmd.Add("-f");
            cmd.Add("mpeg");

            cmd.Add(ouputFile.CanonicalPath);

            string cmdParams = string.Join(" ", cmd);


            if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
            {

                //MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
                //metaRetriever.SetDataSource(inputFile.AbsolutePath);
                //int height = Convert.ToInt32(metaRetriever.ExtractMetadata(Android.Media.MetadataKey.VideoHeight));
                //int width = Convert.ToInt32(metaRetriever.ExtractMetadata(Android.Media.MetadataKey.VideoWidth));


                //For640x360Format for640X360Format = new For640x360Format(height, width);
                //await Transcoder.For(for640X360Format).ConvertAsync(inputFile, ouputFile, f =>
                //{
                //    onProgress?.Invoke((int)(f * (double)100), 100);
                //});


                await Xamarin.MP4Transcoder.Transcoder.For720pFormat().ConvertAsync(inputFile, ouputFile, f =>
                {
                    onProgress?.Invoke((int)(f * (double)100), 100);
                });
            }
            else
            {
                int total = 0;
                int current = 0;

                await FFMpeg.Xamarin.FFMpegLibrary.Run(
               (Android.App.Activity)Forms.Context,
               cmdParams
               , (s) =>
               {
                   logger?.Invoke(s);
                   int n = Extract(s, "Duration:", ",");
                   if (n != -1)
                   {
                       total = n;
                   }
                   n = Extract(s, "time=", " bitrate=");
                   if (n != -1)
                   {
                       current = n;
                       onProgress?.Invoke(current, total);
                   }
               });
            }

            byte[] bytes = Files.ReadAllBytes(ouputFile.ToPath());
            //long size = ouputFile.Length() / 1000000;

            //return ouputFile.Path;

            ImageSource source = new ThumbImage().GenerateThumbImage(ouputFile.Path, 3);

            VideoCompressionVM model = new VideoCompressionVM() { ImgSource = source, VideoData = bytes };

            return model;
        }
        catch (Exception ex)
        {
            return new VideoCompressionVM();
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants