neurospeech / xamarin-android-ffmpeg

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

How I can extract the audio from a video MP4 #18

Closed gwen14123 closed 6 years ago

gwen14123 commented 6 years ago

I have try your sample code and it works !

But after i have try several combination of arguments like this to extract audio in mp3 format :

it represents the command

ffmpeg -i file.mp4 file.mp3

`

        File ouputFile = new File(inputFile.CanonicalPath.Substring(0, inputFile.CanonicalPath.Length-4) + ".mp3");

        ouputFile.DeleteOnExit();

        List<string> cmd = new List<string>();

        cmd.Add("-i");
        cmd.Add(inputFile.CanonicalPath);
        cmd.Add(ouputFile.CanonicalPath);

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

        Log.Debug(TAG, "PARAMETERS : " + cmdParams);
        int total = 0;
        int current = 0;
        await FFMpeg.Xamarin.FFMpegLibrary.Run(...)`

other tentative :

ffmpeg -i file.mp4 -q:a 0 -map a file.mp3

`

       File ouputFile = new File(inputFile.CanonicalPath.Substring(0, inputFile.CanonicalPath.Length-4) + ".mp3");

        ouputFile.DeleteOnExit();

        List<string> cmd = new List<string>();
        cmd.Add("-i");
        cmd.Add(inputFile.CanonicalPath);
        cmd.Add("-q:a");
        cmd.Add("0");
        cmd.Add("-map");
        cmd.Add("a");

        cmd.Add(ouputFile.CanonicalPath);

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

        Log.Debug(TAG, "PARAMETERS : " + cmdParams);
        int total = 0;
        int current = 0;
        try { 
        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);
                }
            });
        }
        catch(Exception e)
        {
            Log.Debug(TAG, "" + e.StackTrace);
            Log.Debug(TAG, "" + e.Message);
        }
        return ouputFile;
    }`

It's me who executes the bad commands or the project can't do this or it's not normal ?

Regards,

Gwen Maupilé

gwen14123 commented 6 years ago

I think it's because there are not mp3 encoder. How i can add it ?

gwen14123 commented 6 years ago

I have found it's because the movie is encoded in mp4 with format aac audio. So i have try this command : ffmpeg -i file.mp4 -vn -acodec copy file.aac and it works !