Пример #1
0
        public static void CompressVideo(string inputPath, string outputPath, Action <string> callback)
        {
            Activity activity = new Activity();

            _callback = callback;
            ProgressDialog progress = new ProgressDialog(Forms.Context);

            progress.Indeterminate = true;
            progress.SetProgressStyle(ProgressDialogStyle.Spinner);
            progress.SetMessage("Compressing Video. Please wait...");
            progress.SetCancelable(false);
            progress.Show();

            Task.Run(() =>
            {
                var _workingDirectory            = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
                var sourceMp4                    = inputPath;
                var destinationPath1             = outputPath;
                FFMpeg ffmpeg                    = new FFMpeg(Android.App.Application.Context, _workingDirectory);
                TransposeVideoFilter vfTranspose = new TransposeVideoFilter(TransposeVideoFilter.NINETY_CLOCKWISE);
                var filters = new List <VideoFilter>();
                filters.Add(vfTranspose);

                var sourceClip = new Clip(System.IO.Path.Combine(_workingDirectory, sourceMp4))
                {
                    videoFilter = VideoFilter.Build(filters)
                };
                var br         = System.Environment.NewLine;
                var onComplete = new MyCommand((_) =>
                {
                    _callback(destinationPath1);
                    progress.Dismiss();
                });

                var onMessage = new MyCommand((message) =>
                {
                    System.Console.WriteLine(message);
                });

                var callbacks = new FFMpegCallbacks(onComplete, onMessage);
                string[] cmds = new string[] {
                    "-y",
                    "-i",
                    sourceClip.path,
                    "-strict", "experimental",
                    "-vcodec", "libx264",
                    "-preset", "ultrafast",
                    "-crf", "30", "-acodec", "aac", "-ar", "44100",
                    "-q:v", "20",
                    "-vf", sourceClip.videoFilter,
                    // "mp=eq2=1:1.68:0.3:1.25:1:0.96:1",

                    destinationPath1,
                };
                ffmpeg.Execute(cmds, callbacks);
            });
        }
        void Start()
        {
            _workingDirectory = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
            var sourceMp4 = "cat1.mp4";
            var destinationPathAndFilename  = System.IO.Path.Combine(_workingDirectory, "cat1_out.mp4");
            var destinationPathAndFilename2 = System.IO.Path.Combine(_workingDirectory, "cat1_out2.mp4");
            var destinationPathAndFilename4 = System.IO.Path.Combine(_workingDirectory, "cat1_out4.wav");

            if (File.Exists(destinationPathAndFilename))
            {
                File.Delete(destinationPathAndFilename);
            }
            CreateSampleFile(Resource.Raw.cat1, _workingDirectory, sourceMp4);


            var ffmpeg = new FFMpeg(this, _workingDirectory);

            var sourceClip = new Clip(System.IO.Path.Combine(_workingDirectory, sourceMp4));

            var result = ffmpeg.GetInfo(sourceClip);

            var br = System.Environment.NewLine;

            // There are callbacks based on Standard Output and Standard Error when ffmpeg binary is running as a process:

            var onComplete = new MyCommand((_) => {
                RunOnUiThread(() => _logView.Append("DONE!" + br + br));
            });

            var onMessage = new MyCommand((message) => {
                RunOnUiThread(() => _logView.Append(message + br + br));
            });

            var callbacks = new FFMpegCallbacks(onComplete, onMessage);

            // 1. The idea of this first test is to show that video editing is possible via FFmpeg:
            // It results in a 150x150 movie that eventually zooms on a cat ear. This is desaturated, and there's a fade in.

            var filters = new List <VideoFilter> ();

            filters.Add(new FadeVideoFilter("in", 0, 100));
            filters.Add(new CropVideoFilter("150", "150", "0", "0"));
            filters.Add(new ColorVideoFilter(1.0m, 1.0m, 0.0m, 0.5m, 1.0m, 1.0m, 1.0m, 1.0m));
            var outputClip = new Clip(destinationPathAndFilename)
            {
                videoFilter = VideoFilter.Build(filters)
            };

            outputClip.H264_CRF = "18";             // It's the quality coefficient for H264 - Default is 28. I think 18 is pretty good.
            ffmpeg.ProcessVideo(sourceClip, outputClip, true, new FFMpegCallbacks(onComplete, onMessage));

            //2. This is a similar version version in command line only:
            string[] cmds = new string[] {
                "-y",
                "-i",
                sourceClip.path,
                "-strict",
                "-2",
                "-vf",
                "mp=eq2=1:1.68:0.3:1.25:1:0.96:1",
                destinationPathAndFilename2,
                "-acodec",
                "copy",
            };
            ffmpeg.Execute(cmds, callbacks);

            // 3. This lists codecs:
            string[] cmds3 = new string[] {
                "-codecs",
            };
            ffmpeg.Execute(cmds, callbacks);

            // 4. This convers to WAV
            // Note that the cat movie just has some silent house noise.
            ffmpeg.ConvertToWaveAudio(sourceClip, destinationPathAndFilename4, 44100, 2, callbacks, true);

            // Etc...

            // Rules of thumb:
            // a) Provide the minimum of info to ffmpeg to not mix it up
            // b) These helpers are cool to test capabilities, but useless otherwise, and crashy: Use command lines.
            // c) Try to compile a newer FFmpeg :)
        }