private void BeforeConversion(Mp3Model model)
        {
            model.IsConvertingLabelVisible = Visibility.Visible;
            model.IsPercentLabelVisible    = Visibility.Hidden;
            model.IsIndeterminate          = true;

            DispatchService.Invoke(() =>
            {
                shortToastMessage.ShowInformation("Converting...");
            });
        }
        private void AfterConversion(Mp3Model model, FileHelper fileHelper)
        {
            DispatchService.Invoke(() =>
            {
                longToastMessage.ShowSuccess(fileHelper.PrepareTrackForNotification(fileHelper.DefaultTrackName));
            });

            fileHelper.RenameFile(fileHelper.TmpTrackPath, fileHelper.DefaultTrackPath, FormatModel.Format);
            fileHelper.RemoveFile(fileHelper.DefaultTrackHiddenPath);

            model.IsProgressDownloadVisible   = Visibility.Hidden;
            model.IsPercentLabelVisible       = Visibility.Hidden;
            model.IsConvertingLabelVisible    = Visibility.Hidden;
            model.IsOperationDoneLabelVisible = Visibility.Visible;
            model.ConvertingLabelText         = Consts.ConvertingPleaseWait;
            model.IsOperationDone             = Consts.OperationDone;
            model.IsIndeterminate             = false;
        }
Example #3
0
 private void OpenMp3Location(Mp3Model mp3Model)
 {
     FileHelper.OpenInExplorer(mp3Model.Path);
 }
Example #4
0
        private void SaveVideoToDisk(string youtubeLinkUrl)
        {
            Task.Factory.StartNew(() =>
            {
                var tempPath = FileHelper.GetTempFileName();

                Mp3Model mp3Model;

                using (var outFile = File.OpenWrite(tempPath))
                {
                    using (var videoDownloader = new VideoDownloader(youtubeLinkUrl, outFile))
                    {
                        var destPath = FileHelper.GetMp3FilePath(videoDownloader.CurrentVideo.FullName);

                        mp3Model = new Mp3Model
                        {
                            Name  = Path.GetFileNameWithoutExtension(destPath),
                            Path  = destPath,
                            Url   = youtubeLinkUrl,
                            State = Mp3ModelState.Downloading,
                        };

                        if (File.Exists(mp3Model.Path))
                        {
                            shortToastMessage.ShowInformation(Consts.FileAlreadyExistsInfo);
                            return;
                        }

                        FileHelper.EnsureDirectoryExist(mp3Model.Path);

                        Application.Current.Dispatcher.BeginInvoke(new Action(() => _mp3List.Add(mp3Model)));

                        videoDownloader.ProgressChanged += (s, a) =>
                        {
                            mp3Model.CurrentProgress = a.CurrentProgress;
                            Debug.WriteLine($"{a.CurrentProgress}% of video downloaded");
                        };

                        videoDownloader.Download();
                    }
                }


                mp3Model.State = Mp3ModelState.Converting;
                DispatchService.Invoke(() => shortToastMessage.ShowInformation("Converting..."));

                var converter = new Converter();

                converter.ProgressChanged += (s, a) => mp3Model.CurrentProgress = a.CurrentProgress;


                converter.ExtractAudioMp3FromVideo(tempPath, mp3Model.Path, QualityModel.Quality);

                File.Delete(tempPath);

                DispatchService.Invoke(() =>
                {
                    longToastMessage.ShowSuccess(mp3Model.Name);
                });

                mp3Model.State = Mp3ModelState.Done;
            });
        }
        private void SaveVideoToDisk()
        {
            Task.Factory.StartNew(() =>
            {
                var CurrentFile = new FileHelper();
                var Mp3Model    = new Mp3Model();

                using (var service = Client.For(YouTube.Default))
                {
                    if (IsWholeListChecked)
                    {
                        var youtubePlaylist = new YoutubePlaylist();
                        var playlist        = youtubePlaylist.GetVideosFromPlaylist(YoutubeLinkUrl);

                        if (playlist != null)
                        {
                            foreach (var audio in playlist)
                            {
                                using (var video = service.GetVideo(audio))
                                {
                                    CurrentFile.DefaultTrackName       = video.FullName;
                                    CurrentFile.DefaultTrackPath       = CurrentFile.Path + "\\" + CurrentFile.DefaultTrackName;
                                    CurrentFile.DefaultTrackHiddenPath = CurrentFile.HiddenPath + "\\" + CurrentFile.DefaultTrackName;
                                    CurrentFile.TmpTrackPath           = CurrentFile.PreparePathForFFmpeg(CurrentFile.DefaultTrackHiddenPath);

                                    Mp3Model = new Mp3Model()
                                    {
                                        Name = CurrentFile.CheckVideoFormat(video.FullName, FormatModel.Format),
                                        IsProgressDownloadVisible   = Visibility.Visible,
                                        IsPercentLabelVisible       = Visibility.Visible,
                                        IsConvertingLabelVisible    = Visibility.Hidden,
                                        IsOperationDoneLabelVisible = Visibility.Hidden,
                                        ConvertingLabelText         = Consts.ConvertingPleaseWait,
                                        CurrentProgress             = 0,
                                    };

                                    Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                                    {
                                        this._mp3List.Add(Mp3Model);
                                    }));

                                    using (var outFile = File.OpenWrite(CurrentFile.TmpTrackPath))
                                    {
                                        using (var progressStream = new ProgressStream(outFile))
                                        {
                                            var streamLength = (long)video.StreamLength();

                                            progressStream.BytesMoved += (sender, args) =>
                                            {
                                                Mp3Model.CurrentProgress = args.StreamLength * 100 / streamLength;
                                                Debug.WriteLine($"{Mp3Model.CurrentProgress}% of video downloaded");
                                            };

                                            video.Stream().CopyTo(progressStream);
                                        }
                                    }
                                    BeforeConversion(Mp3Model);
                                    ExtractAudioFromVideo(CurrentFile);
                                    AfterConversion(Mp3Model, CurrentFile);
                                }
                            }
                        }
                    }
                    else
                    {
                        using (var video = service.GetVideo(YoutubeLinkUrl))
                        {
                            CurrentFile.DefaultTrackName       = video.FullName;
                            CurrentFile.DefaultTrackPath       = CurrentFile.Path + "\\" + CurrentFile.DefaultTrackName;
                            CurrentFile.DefaultTrackHiddenPath = CurrentFile.HiddenPath + "\\" + CurrentFile.DefaultTrackName;
                            CurrentFile.TmpTrackPath           = CurrentFile.PreparePathForFFmpeg(CurrentFile.DefaultTrackHiddenPath);

                            Mp3Model = new Mp3Model()
                            {
                                Name = CurrentFile.CheckVideoFormat(video.FullName, FormatModel.Format),
                                IsProgressDownloadVisible   = Visibility.Visible,
                                IsPercentLabelVisible       = Visibility.Visible,
                                IsConvertingLabelVisible    = Visibility.Hidden,
                                IsOperationDoneLabelVisible = Visibility.Hidden,
                                ConvertingLabelText         = Consts.ConvertingPleaseWait,
                                CurrentProgress             = 0,
                            };

                            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                            {
                                this._mp3List.Add(Mp3Model);
                            }));

                            using (var outFile = File.OpenWrite(CurrentFile.TmpTrackPath))
                            {
                                using (var progressStream = new ProgressStream(outFile))
                                {
                                    var streamLength = (long)video.StreamLength();

                                    progressStream.BytesMoved += (sender, args) =>
                                    {
                                        Mp3Model.CurrentProgress = args.StreamLength * 100 / streamLength;
                                        Debug.WriteLine($"{Mp3Model.CurrentProgress}% of video downloaded");
                                    };

                                    video.Stream().CopyTo(progressStream);
                                }
                            }
                            BeforeConversion(Mp3Model);
                            ExtractAudioFromVideo(CurrentFile);
                            AfterConversion(Mp3Model, CurrentFile);
                        }
                    }
                }
            });
        }