示例#1
0
        private async Task <string> DownloadVideoAsync(string url, string dlPath)
        {
            string videoDataFilename;
            //download the youtube video data (usually .mp4 or .webm)
            var video = await _ytDownloader.GetVideoAsync(url);

            var videoData = await video.GetBytesAsync();

            //rename the video if it does not have a useful name
            string videoTitleName = video.FullName;

            if (string.IsNullOrEmpty(videoTitleName) || videoTitleName.ToLower() == "youtube.mp4" || videoTitleName.ToLower() == "youtube.webm")
            {
                videoTitleName = await GetVideoTitle(url);
            }

            //remove suffix added by library
            videoTitleName = videoTitleName.Replace(" - YouTube", string.Empty);

            //write the downloaded media file to the temp assets dir
            videoDataFilename = Path.Combine(dlPath, videoTitleName);
            File.WriteAllBytes(videoDataFilename, videoData);

            return(videoDataFilename);
        }
        public async Task Plus(string Url)
        {
            try
            {
                YouTube yt    = YouTube.Default;
                var     video = await yt.GetVideoAsync(Url);

                if (!Directory.Exists(Environment.CurrentDirectory + "\\musics\\"))
                {
                    Directory.CreateDirectory(Environment.CurrentDirectory + "\\musics\\");
                }

                File.WriteAllBytes(Environment.CurrentDirectory + "\\musics\\" + video.FullName.Replace(" ", ""), video.GetBytes());
                using (var engine = new Engine(@"C:\ffmpeg\bin\ffmpeg.exe"))
                {
                    var inputFile = new MediaFile {
                        Filename = Environment.CurrentDirectory + "\\musics\\" + video.FullName.Replace(" ", "")
                    };
                    var outputFile = new MediaFile {
                        Filename = Environment.CurrentDirectory + "\\musics\\" + video.FullName.Replace(" ", "").Replace(video.FileExtension, ".mp3")
                    };
                    engine.Convert(inputFile, outputFile);
                    File.Delete(Environment.CurrentDirectory + "\\musics\\" + video.FullName.Replace(" ", ""));
                    Console.WriteLine("음악 파일 다운로드 완료");
                }
                cts = new CancellationTokenSource();
                await SendAsync(audio, Environment.CurrentDirectory + "\\musics\\" + video.FullName.Replace(" ", "").Replace(video.FileExtension, ".mp3"), cts.Token);
            }
            catch (Exception Ex)
            {
                Console.WriteLine(Ex.StackTrace);
            }
        }
示例#3
0
        public async Task <string> GetVideo(string videoId, string savePath)
        {
            try
            {
                var youTube    = new YouTube();
                var youtubeUrl = ConfigurationManager.AppSettings["MainYoutubeURL"];
                var url        = youtubeUrl + videoId;
                var video      = await youTube.GetVideoAsync(url);

                var bytes = await video.GetBytesAsync();

                Directory.CreateDirectory(Path.Combine(savePath, "SpotifyToMp3"));
                var fullPath = Path.Combine(savePath, "SpotifyToMp3", video.FullName);
                File.WriteAllBytes(fullPath, bytes);
                return(fullPath);
            }
            catch (Exception e)
            {
                using (var sw = new StreamWriter("ExceptionLog.txt", true))
                {
                    sw.WriteLine(DateTime.Now);
                    sw.WriteLine(e.ToString());
                    sw.WriteLine();
                    sw.Flush();
                    sw.Close();
                }
                return(null);
            }
        }
示例#4
0
        //********
        //* Helper classes
        //*************

        private async Task <Tuple <string, string> > GetTitle(string url, int index)
        {
            Tuple <string, string> result;
            YouTube ytb   = YouTube.Default;
            var     video = await ytb.GetVideoAsync(url);

            string wwwRoot = _hostEnvironment.WebRootPath;
            //Directory.CreateDirectory(Path.Combine(wwwRoot, "room"));
            await File.WriteAllBytesAsync(Path.Combine(wwwRoot, "room", "Playlist_Room_" + index.ToString() + video.FileExtension), await video.GetBytesAsync());

            result = new Tuple <string, string>(video.Title, "Playlist_Room_" + index.ToString() + video.FileExtension);

            return(result);
        }
示例#5
0
        public async void setSourceAsync(string link, MediaPlayer mediaElement)
        {
            YouTube      youTube = YouTube.Default;
            YouTubeVideo video   = null;

            video = await youTube.GetVideoAsync(link);

            Debug.WriteLine("Download Display: " + video.Resolution + ", AudioBit: " + video.AudioBitrate + ", 포맷: " + video.Format);
            var memStream = new MemoryStream();

            await(await video.StreamAsync()).CopyToAsync(memStream);
            memStream.Position  = 0;
            mediaElement.Source = MediaSource.CreateFromStream(memStream.AsRandomAccessStream(), video.FileExtension);
            mediaElement.Play();
        }
示例#6
0
        public async Task <string> GetVideo(string videoId, string savePath)
        {
            try
            {
                var youTube    = new YouTube();
                var youtubeUrl = ConfigurationManager.AppSettings["MainYoutubeURL"];
                var url        = youtubeUrl + videoId;
                var video      = await youTube.GetVideoAsync(url);

                var bytes = await video.GetBytesAsync();

                Directory.CreateDirectory(Path.Combine(savePath, "SpotifyToMp3"));
                var fullPath = Path.Combine(savePath, "SpotifyToMp3", video.FullName);
                File.WriteAllBytes(fullPath, bytes);
                return(fullPath);
            }
            catch (Exception e)
            {
                Log.Error("Fel när en video skulle laddas ner.", e);
                return(null);
            }
        }
示例#7
0
        private async Task downloadURLAsync(string url, YouTube youtube, int counter)
        {
            File.AppendAllText(logPath, DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + ";Downloading;" + (counter + 1) + ";" + url + Environment.NewLine);
            var video = await youtube.GetVideoAsync(url);

            var bytes = await video.GetBytesAsync();

            string path = downloadsPath + @"\" + video.FullName;

            File.WriteAllBytes(path, bytes);
            lbl_status.Content = "Downloaded: " + video.FullName + "\nLocation: " + downloadsPath;
            File.AppendAllText(logPath, DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + ";Downloaded;" + (counter + 1) + ";" + video.FullName + Environment.NewLine);

            //convert file to mp3
            File.AppendAllText(logPath, DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + ";Converting;" + video.FullName + Environment.NewLine);
            var ffMpeg   = new FFMpegConverter();
            var settings = new ConvertSettings();

            settings.CustomOutputArgs = "-b:a 320k";
            settings.AudioSampleRate  = 44100;

            ffMpeg.ConvertMedia(path, Format.mp4, downloadsPath + @"\" + video.Title + ".mp3", "mp3", settings);
            File.AppendAllText(logPath, DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + ";Converted;" + video.Title + ".mp3" + Environment.NewLine);
        }
示例#8
0
        public virtual async Task <YouTubeVideo> DownloadYouTubeAsync(string input)
        {
            YouTube youtube = YouTube.Default;

            return(await youtube.GetVideoAsync(input));
        }
示例#9
0
        private async void ConvertVideo(string url, string outdir)
        {
            if (!Directory.Exists(outdir))
            {
                MessageBox.Show("Directory: " + outdir + " does not exist", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            try
            {
                YouTube _youtube       = YouTube.Default;
                Video   requestedVideo = await _youtube.GetVideoAsync(url);

                File.WriteAllBytes(string.Format("{0}\\convert.tmp", outdir), await requestedVideo.GetBytesAsync());

                string inputFile  = string.Format("{0}\\convert.tmp", outdir);
                string outFileDir = string.Format("{0}\\{1}.mp3", outdir, GetFileName(requestedVideo.FullName));

                outFileDir = outFileDir.Replace(".mp4", "");
                outFileDir = outFileDir.Replace("YouTube", "");

                MediaFile Src = new MediaFile {
                    Filename = inputFile
                };
                MediaFile Dst = new MediaFile {
                    Filename = outFileDir
                };

                Engine converter = new Engine();
                converter.GetMetadata(Src);
                converter.Convert(Src, Dst);

                //Add ID3 Tags if we can.
                if (requestedVideo.FullName.Contains("-"))
                {
                    string fullname = requestedVideo.FullName;
                    fullname = fullname.Replace(".mp4", "");
                    fullname = fullname.Replace("YouTube", "");
                    string[] details = fullname.Split('-');
                    string   artist  = null;
                    string   title   = null;
                    bool     doTag   = false;

                    if (details[0] != null)
                    {
                        artist = details[0];
                    }

                    if (details[1] != null)
                    {
                        title = details[1];
                    }

                    if (artist == null || title == null)
                    {
                        doTag = true;
                    }

                    if (doTag)
                    {
                        //To-do later when I can be arsed.
                    }
                }

                File.Delete(inputFile);

                Dispatcher.Invoke(() =>
                {
                    progress.Visibility          = Visibility.Hidden;
                    progress.IsIndeterminate     = false;
                    button_convert.IsEnabled     = true;
                    button_browse.IsEnabled      = true;
                    input_video.IsEnabled        = true;
                    input_video.Text             = "";
                    input_outputfolder.IsEnabled = true;
                });

                MessageBox.Show("Video Conversion Completed!", "Done", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception e)
            {
                MessageBox.Show("Failed to convert video: " + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
示例#10
0
        public async Task <YouTubeVideoDto> GetVideoAsync(string youtubeUrl)
        {
            var videoOrAudio = await youtube.GetVideoAsync(youtubeUrl);

            return(new YouTubeVideoDto(videoOrAudio, youtubeUrl));
        }