Ejemplo n.º 1
0
        //https://ffmpeg.xabe.net/docs.html
        public async Task <IConversionResult> ConverVideoAsync(string videoPath, string outputPath)
        {
            if (string.IsNullOrEmpty(videoPath))
            {
                throw new ArgumentException($"'{nameof(videoPath)}' cannot be null or empty", nameof(videoPath));
            }
            if (string.IsNullOrEmpty(outputPath))
            {
                throw new ArgumentException($"'{nameof(outputPath)}' cannot be null or empty", nameof(outputPath));
            }

            string outputName = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(videoPath) + ".mp4");

            try {
                IMediaInfo  mediaInfo = FFmpeg.GetMediaInfo(videoPath).Result;
                IConversion converter = FFmpeg.Conversions.New();

                IVideoStream videoStream = mediaInfo.VideoStreams.FirstOrDefault();
                if (videoStream == null)
                {
                    throw new NullReferenceException("Failed to extract video stream from the source file.");
                }
                converter.AddStream(videoStream);

                IAudioStream audioStream = mediaInfo.AudioStreams.FirstOrDefault(ii => !string.IsNullOrEmpty(ii.Language) && ii.Language.ToLower().Equals("jpn"));
                if (audioStream == null)
                {
                    //Could not find a Japanese audio stream lets take the default
                    audioStream = mediaInfo.AudioStreams.FirstOrDefault();
                    if (audioStream == null)
                    {
                        throw new NullReferenceException("Failed to extract audio stream from the source file.");
                    }
                }
                audioStream.SetCodec(AudioCodec.aac);
                converter.AddStream(audioStream);

                IConversionResult subsResult = await ExtractSubsAsync(videoPath, outputPath, mediaInfo.SubtitleStreams);

                converter.SetOutputFormat(Format.mp4);
                converter.SetOutput(outputName);
                converter.OnProgress += Converter_OnProgress;

                IConversionResult result = await converter.Start(cancellationTokenSource.Token);

                return(result);
            } catch (Exception ex) {
                throw;
            }
        }
Ejemplo n.º 2
0
        public async Task ProcessMediaAsync(MediaPipeline.MediaFile media, bool restore)
        {
            string filePath = media.FilePath;

            Console.WriteLine("Processing Transcode");
            m_mediaFile   = media;
            media.Message = "Transcoding";
            string newFilePath = Path.ChangeExtension(filePath, ".mp4");

            if (File.Exists(newFilePath))
            {
                File.Delete(newFilePath);
            }
            //await Task.Run(async () => { while (File.Exists(newFilePath)) { await Task.Delay(100); } });
            Console.WriteLine("Transcoding");
            IMediaInfo mediaInfo = await Xabe.FFmpeg.FFmpeg.GetMediaInfo(filePath);

            IStream     videoStream = mediaInfo.VideoStreams.FirstOrDefault()?.SetCodec(VideoCodec.libx264);
            IStream     audioStream = mediaInfo.AudioStreams.FirstOrDefault()?.SetCodec(AudioCodec.aac);
            IConversion conversion  = Xabe.FFmpeg.FFmpeg.Conversions.New();

            conversion.OnProgress += OnProgress;
            await conversion.AddStream(videoStream, audioStream)
            .AddParameter("-preset medium")
            .AddParameter("-crf 22")
            .SetOutput(newFilePath).Start();

            File.Delete(filePath);
            media.FilePath = newFilePath;
            m_mediaFile.FinishedProcessing();
        }
Ejemplo n.º 3
0
        private async void Deepfry(String inputPath, String outputPath)
        {
            running = true;

            IMediaInfo mediaInfo = await FFmpeg.GetMediaInfo(inputPath).ConfigureAwait(true);

            IStream videoStream = mediaInfo.VideoStreams.FirstOrDefault();
            IStream audioStream = mediaInfo.AudioStreams.FirstOrDefault();

            IConversion c = FFmpeg.Conversions.New();

            c.AddStream(audioStream, videoStream);
            c.SetOutput(outputPath);
            c.SetOverwriteOutput(true);
            c.SetVideoBitrate((long)VideoBitrateSlider.Value);
            c.SetAudioBitrate((long)AudioBitrateSlider.Value);
            c.AddParameter("-filter:v fps=fps=" + FramerateSlider.Value);
            c.OnProgress += (sender, args) => { this.Dispatcher.Invoke(() => {
                    DoubleAnimation anim = new DoubleAnimation(args.Percent, TimeSpan.FromSeconds(0.5));
                    ConversionProgressBar.BeginAnimation(ProgressBar.ValueProperty, anim);
                }); };

            await c.Start().ConfigureAwait(true);

            running = false;
            NotificationBar.MessageQueue.Enqueue("Complete!", "OPEN", new Action(() => Process.Start("explorer.exe", outputPath)));
        }