diff --git a/.gitignore b/.gitignore index 94420dc..5836cbe 100644 --- a/.gitignore +++ b/.gitignore @@ -234,3 +234,4 @@ _Pvt_Extensions # FAKE - F# Make .fake/ +*.bak diff --git a/FFMpeg.Xamarin/FFMpegLibrary.cs b/FFMpeg.Xamarin/FFMpegLibrary.cs index d2a9bdb..4fbbd4b 100644 --- a/FFMpeg.Xamarin/FFMpegLibrary.cs +++ b/FFMpeg.Xamarin/FFMpegLibrary.cs @@ -22,7 +22,8 @@ public class FFMpegLibrary private Java.IO.File ffmpegFile; - public void Init(Context context) { + public void Init(Context context) + { if (_initialized) return; @@ -31,22 +32,26 @@ public void Init(Context context) { ffmpegFile = new Java.IO.File(filesDir + "/ffmpeg"); - if (ffmpegFile.Exists()) { - if(ffmpegFile.CanExecute()) + if (ffmpegFile.Exists()) + { + if (ffmpegFile.CanExecute()) ffmpegFile.SetExecutable(false); ffmpegFile.Delete(); System.Diagnostics.Debug.WriteLine($"ffmpeg file deleted at {ffmpegFile.AbsolutePath}"); } - using (var s = context.Assets.Open("ffmpeg", Android.Content.Res.Access.Streaming)) { - using (var fout = System.IO.File.OpenWrite(ffmpegFile.AbsolutePath)) { + using (var s = context.Assets.Open("ffmpeg", Android.Content.Res.Access.Streaming)) + { + using (var fout = System.IO.File.OpenWrite(ffmpegFile.AbsolutePath)) + { s.CopyTo(fout); } } System.Diagnostics.Debug.WriteLine($"ffmpeg file copied at {ffmpegFile.AbsolutePath}"); - if (!ffmpegFile.CanExecute()) { + if (!ffmpegFile.CanExecute()) + { ffmpegFile.SetExecutable(true); System.Diagnostics.Debug.WriteLine($"ffmpeg file made executable"); } @@ -54,83 +59,75 @@ public void Init(Context context) { _initialized = true; } - public static Task Run( - Context context, - string cmd, - Action logger = null) { + public static async Task Run(Context context, string cmd, Action logger = null) { - TaskCompletionSource task = new TaskCompletionSource(); + TaskCompletionSource source = new TaskCompletionSource(); - Task.Run( async () => - { - Instance.Init(context); + await Task.Run(() => { + try { + int n = _Run(context, cmd, logger); + source.SetResult(n); + } catch (Exception ex) { + source.SetException(ex); + } + }); + + return await source.Task; + } - bool success = false; + private static int _Run( + Context context, + string cmd, + Action logger = null) + { + TaskCompletionSource task = new TaskCompletionSource(); - System.Diagnostics.Debug.WriteLine($"ffmpeg initialized"); + Instance.Init(context); - var process = Java.Lang.Runtime.GetRuntime().Exec( Instance.ffmpegFile.CanonicalPath + " " + cmd ); - System.Diagnostics.Debug.WriteLine($"ffmpeg started"); + System.Diagnostics.Debug.WriteLine($"ffmpeg initialized"); - var startTime = DateTime.UtcNow; + //var process = Java.Lang.Runtime.GetRuntime().Exec( Instance.ffmpegFile.CanonicalPath + " " + cmd ); - do - { + var startInfo = new System.Diagnostics.ProcessStartInfo(Instance.ffmpegFile.CanonicalPath, cmd); - await Task.Delay(100); + startInfo.RedirectStandardError = true; + startInfo.RedirectStandardOutput = true; + startInfo.UseShellExecute = false; + var process = new System.Diagnostics.Process(); - var now = DateTime.UtcNow; + process.StartInfo = startInfo; - if ((now - startTime).TotalSeconds > 60) { - throw new TimeoutException(); - } - startTime = now; + bool finished = false; + string error = null; - try - { - success = process.ExitValue() == 0; + process.Start(); - // seems process is completed... - System.Diagnostics.Debug.WriteLine($"ffmpeg finished"); - break; - } - catch (Java.Lang.IllegalThreadStateException e) - { - // do nothing... - } - using (var br = new Java.IO.BufferedReader(new Java.IO.InputStreamReader(process.ErrorStream))) - { - string line = null; - while ((line = br.ReadLine()) != null) { - System.Diagnostics.Debug.WriteLine(line); - logger?.Invoke(line); - } - } - - } while (true); - - using (var br = new Java.IO.BufferedReader( - new Java.IO.InputStreamReader( - success ? process.InputStream : process.ErrorStream))) + Task.Run(() => + { + using (var reader = process.StandardError) { - string line = null; - while ((line = br.ReadLine()) != null) + string processOutput = ""; + do { - System.Diagnostics.Debug.WriteLine(line); + var line = reader.ReadLine(); + if (line == null) + break; logger?.Invoke(line); - } + processOutput += line; + } while (!finished); + error = processOutput; } + }); + process.WaitForExit(); - task.SetResult(0); - }); + return process.ExitCode; - return task.Task; } diff --git a/FFMpeg.Xamarin/Properties/AssemblyInfo.cs b/FFMpeg.Xamarin/Properties/AssemblyInfo.cs index 6b4ce7d..ae865ce 100644 --- a/FFMpeg.Xamarin/Properties/AssemblyInfo.cs +++ b/FFMpeg.Xamarin/Properties/AssemblyInfo.cs @@ -26,5 +26,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.3.*")] +[assembly: AssemblyVersion("1.0.6.*")] diff --git a/README.md b/README.md index e9feef9..ba3435e 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ You can download Xamarin.Android.FFmpeg package from Nuget Package manager or ru } - public File convertFile( + public File ConvertFile(Context contex, File inputFile, Action logger = null, Action onProgress = null) @@ -83,28 +83,21 @@ You can download Xamarin.Android.FFmpeg package from Nuget Package manager or ru int total = 0; int current = 0; - Com.Github.Hiteshsondhi88.Libffmpeg.FFmpeg - .GetInstance(Xamarin.Forms.Forms.Context) - .ExecuteAndWait(cmdParams, - new VideoConverterListener { - OnFailure = (f) => { - logger?.Invoke(f); - }, - OnProgress = (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); - } - } - - } - ); + await FFMpeg.Xamarin.FFMpegLibrary.Run( + 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); + } + }); return ouputFile; } @@ -138,41 +131,3 @@ You can download Xamarin.Android.FFmpeg package from Nuget Package manager or ru } - public class VideoConverterListener : - Java.Lang.Object, - Com.Github.Hiteshsondhi88.Libffmpeg.IFFmpegExecuteResponseHandler - { - public Action OnFailure { get; set; } - - void IFFmpegExecuteResponseHandler.OnFailure(string p0) - { - OnFailure?.Invoke(p0); - } - - public Action OnFinish { get; set; } - - void IResponseHandler.OnFinish() - { - OnFinish?.Invoke(); - } - - public Action OnProgress { get; set; } - void IFFmpegExecuteResponseHandler.OnProgress(string p0) - { - OnProgress?.Invoke(p0); - } - - public Action OnStart { get; set; } - - void IResponseHandler.OnStart() - { - OnStart?.Invoke(); - } - - public Action OnSuccess { get; set; } - - void IFFmpegExecuteResponseHandler.OnSuccess(string p0) - { - OnSuccess?.Invoke(p0); - } - } diff --git a/Xamarin.FFmpeg.sln b/Xamarin.FFmpeg.sln index 8fa8f0b..98bf6a3 100644 --- a/Xamarin.FFmpeg.sln +++ b/Xamarin.FFmpeg.sln @@ -7,6 +7,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xamarin.FFmpeg.Nuget", "nug EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FFMpeg.Xamarin", "FFMpeg.Xamarin\FFMpeg.Xamarin.csproj", "{1049745B-3D4C-4DFF-AD8B-DC46F460B5D4}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A2C28ED0-0741-4CDE-B9FA-604C145DE4D9}" + ProjectSection(SolutionItems) = preProject + README.md = README.md + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/nuget/Package.nuspec b/nuget/Package.nuspec index ab42f4d..4108966 100644 --- a/nuget/Package.nuspec +++ b/nuget/Package.nuspec @@ -2,7 +2,7 @@ Xamarin.Android.FFmpeg - 1.0.2 + 1.0.6 FFmpeg binding for Xamarin Android akash.kava