neurospeech / xamarin-android-ffmpeg

Xamarin Android FFMpeg binding
MIT License
30 stars 6 forks source link

Can't get this program to work #5

Closed DavidBarishev closed 8 years ago

DavidBarishev commented 8 years ago

Hello! I Hope this repository isn't dead, it a very useful one. I have been trying to test if its working, and i cant seem to get the code to run.

Here is the code in the UI thread:

VideoConverter vc = new VideoConverter();
            File x = vc.ConvertFile(
                BaseContext,
                new File(filename),
                (msg) => { System.Diagnostics.Debug.WriteLine(msg); },
                (d,y) => { System.Diagnostics.Debug.WriteLine("{0} {1}",d,y); }
            ).Result;

I pasted the code on the readme file to another one and edited it a bit to get it compiling.

    public class VideoConverter
    {

        public VideoConverter()
        {

        }

        public async Task<File> ConvertFile(
            Context contex,
            File inputFile,
            Action<string> logger = null,
            Action<int, int> onProgress = null

            ){

            List<string> cmd = new List<string>();
            //Override Files
            cmd.Add("-y");

            //Input path
            cmd.Add("-i");
            cmd.Add(inputFile.CanonicalPath);

            // Output format
            cmd.Add("-f");
            cmd.Add("mp3");

            // Output File path
            File ouputFile = new File(inputFile.CanonicalPath.Replace(inputFile.Name.Split('.')[1],"mp3"));
            ouputFile.DeleteOnExit();
            cmd.Add(ouputFile.CanonicalPath);

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

            int total = 0;
            int current = 0;

            await FFMpeg.Xamarin.FFMpegLibrary.Run(
                contex,
                 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;
        }

        int Extract(String text, String start, String end)
        {
            int i = text.IndexOf(start);
            if (i != -1)
            {
                text = text.Substring(i + start.Length);
                i = text.IndexOf(end);
                if (i != -1)
                {
                    text = text.Substring(0, i);
                    return parseTime(text);
                }
            }
            return -1;
        }

        public static int parseTime(String time)
        {
            time = time.Trim();
            String[] tokens = time.Split(':');
            int hours = int.Parse(tokens[0]);
            int minutes = int.Parse(tokens[1]);
            float seconds = float.Parse(tokens[2]);
            int s = (int)seconds * 100;
            return hours * 360000 + minutes * 60100 + s;
        }
    }

Am im doing something wrong ? I'm trying to convert a mp4 file to mp3.I am not so familiar with async, so it might be it, but i'm not getting any output. The debugger never prints anything.

neurospeech commented 8 years ago

First of all, do not use asyncMethod().Result. Asynchronous tasks must not be waited on main UI thread.

Instead, you have to use as follow...

  someButton.Click += async (s,e) => {
        try{

             VideoConverter vc = new VideoConverter();
             File x = await vc.ConvertFile(BaseContext,
                                         new File(filename),
                                         (msg) => System.Diagnostics.Debug.WriteLine(msg) ,
                                         (d,y) =>  System.Diagnostics.Debug.WriteLine("{0} {1}",d,y));

             // do something with file... 
        }catch(Exception ex){
            // display an error...
        }
  };
DavidBarishev commented 8 years ago

That did it, thanks!