Skip to content

Commit

Permalink
Version 1.0.6 published
Browse files Browse the repository at this point in the history
  • Loading branch information
Akash Kava authored and Akash Kava committed May 9, 2016
1 parent 908deba commit cde7387
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 122 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,4 @@ _Pvt_Extensions

# FAKE - F# Make
.fake/
*.bak
115 changes: 56 additions & 59 deletions FFMpeg.Xamarin/FFMpegLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,106 +32,102 @@ 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");
}

_initialized = true;
}

public static Task Run(
Context context,
string cmd,
Action<string> logger = null) {
public static async Task<int> Run(Context context, string cmd, Action<string> logger = null) {

TaskCompletionSource<int> task = new TaskCompletionSource<int>();
TaskCompletionSource<int> source = new TaskCompletionSource<int>();

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<string> logger = null)
{

TaskCompletionSource<int> task = new TaskCompletionSource<int>();

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;

}

Expand Down
2 changes: 1 addition & 1 deletion FFMpeg.Xamarin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.*")]

77 changes: 16 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> logger = null,
Action<int,int> onProgress = null)
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<string> OnFailure { get; set; }

void IFFmpegExecuteResponseHandler.OnFailure(string p0)
{
OnFailure?.Invoke(p0);
}

public Action OnFinish { get; set; }

void IResponseHandler.OnFinish()
{
OnFinish?.Invoke();
}

public Action<string> OnProgress { get; set; }
void IFFmpegExecuteResponseHandler.OnProgress(string p0)
{
OnProgress?.Invoke(p0);
}

public Action OnStart { get; set; }

void IResponseHandler.OnStart()
{
OnStart?.Invoke();
}

public Action<string> OnSuccess { get; set; }

void IFFmpegExecuteResponseHandler.OnSuccess(string p0)
{
OnSuccess?.Invoke(p0);
}
}
5 changes: 5 additions & 0 deletions Xamarin.FFmpeg.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion nuget/Package.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Xamarin.Android.FFmpeg</id>
<version>1.0.2</version>
<version>1.0.6</version>
<title>FFmpeg binding for Xamarin Android</title>
<authors>akash.kava</authors>
<owners></owners>
Expand Down

0 comments on commit cde7387

Please sign in to comment.